We’re moving to a new home! Our website is currently in test mode while we update and transfer our content. Some pages and resources may be temporarily unavailable. We’ll be back with all resources shortly. Thank you for your patience.

HTML & CSS | 03.4 The Box Model: Margin, Border and Padding

Lesson objective

Explain the four parts of the CSS box model. Calculate box dimensions, use spacing shorthands and compare content-box with border-box sizing.

Learn

03.4 | THE BOX MODEL: MARGIN, BORDER AND PADDING

01 | EVERY BOX HAS LAYERS

You add width: 300px to a project card, then give it padding and a border. Suddenly it takes up more space than you expected. The box model explains why.

For the simple block boxes in this lesson, think from the inside out: content, padding, border, margin.

  • Content: the area holding text, images or other content.
  • Padding: space between the content and border.
  • Border: the line around the padding and content.
  • Margin: space outside the border, separating the box from its surroundings.

The box model is a sizing model. It does not mean every element needs a visible border.

02 | PADDING IS INSIDE, MARGIN IS OUTSIDE

.project-card {
  padding: 20px;
  border: 2px solid #24988f;
  margin: 24px;
}

Padding keeps a card’s text away from its border. Margin separates the card from neighbouring content. The border sits between them.

By default, an element’s background paints through its padding to the border area. Margin is outside that background. This helps you see why padding and margin can look different even when they use the same value.

Do not add repeated spaces or empty paragraphs to push content around. Use CSS spacing on the elements.

03 | USE FOUR SIDES OR SHORTHAND

/* One value: all four sides */
padding: 16px;

/* Two values: top/bottom, left/right */
padding: 12px 20px;

/* Three values: top, left/right, bottom */
margin: 8px 16px 24px;

/* Four values: top, right, bottom, left */
margin: 8px 12px 16px 20px;

For four values, go clockwise from the top. Margin and padding use the same shorthand order.

.project-card {
  padding-top: 12px;
  padding-right: 20px;
  padding-bottom: 12px;
  padding-left: 20px;
}

Individual side properties can be clearer when changing just one side. Padding cannot be negative. Margins can be negative, but begin with ordinary positive spacing until you understand the layout.

04 | BORDERS NEED A STYLE

.project-card {
  border: 2px solid #24988f;
  border-radius: 12px;
}

The border shorthand sets width, style and colour. solid creates a continuous line; dashed and dotted produce other styles. A colour and width alone may not produce a visible border when the border style remains none.

border-radius rounds corners. It does not create padding, change the content width or automatically clip every child element to the rounded shape.

Use borders to clarify grouping, alongside headings and spacing. Keep contrast and readability in mind.

05 | CONTENT-BOX: WIDTH DESCRIBES CONTENT

.project-card {
  box-sizing: content-box;
  width: 300px;
  padding: 20px;
  border: 2px solid #24988f;
  margin: 10px;
}

For this fixed-width example, content-box means the specified width is the content width. Padding and borders add to the border-box width.

Content width:                    300px
Left and right padding:       20 + 20px
Left and right border:          2 + 2px
Border-box width:                  344px
Including both horizontal margins: 364px

Margin is never included in the CSS width. Here the sum with margins is useful for spacing calculations, but actual layout can depend on constraints and neighbouring boxes.

06 | BORDER-BOX: INCLUDE PADDING AND BORDER

.project-card {
  box-sizing: border-box;
  width: 300px;
  padding: 20px;
  border: 2px solid #24988f;
  margin: 10px;
}

With border-box, the specified width includes the padding and border. In this example the border-box width remains 300px.

Specified border-box width:        300px
Subtract horizontal padding:       40px
Subtract horizontal border:         4px
Content width:                     256px
Including both horizontal margins: 320px

Many sites apply border-box consistently:

*, *::before, *::after {
  box-sizing: border-box;
}

box-sizing does not normally inherit, so this rule targets elements and their pseudo-elements explicitly. It makes common width calculations easier without removing padding or borders.

07 | WIDTH, MAX-WIDTH AND CENTERING

.reading-card {
  box-sizing: border-box;
  width: 100%;
  max-width: 640px;
  padding: 20px;
  border: 2px solid #24988f;
  margin: 24px auto;
}

The card can fill its containing area but is capped at 640px. border-box keeps its padding and border inside that width. In ordinary block layout, auto left and right margins share available space, centring a narrower box.

If the box already fills the available width, there is no spare horizontal space to distribute. margin: auto is not a universal way to centre everything vertically.

text-align: center centres inline content within a box. It does not centre the outer block box itself.

Let text containers grow naturally where possible. Fixed heights can cause text to overflow when content changes or the user enlarges it.

08 | VERTICAL MARGINS CAN COLLAPSE

In ordinary block layout, some vertical margins collapse into one margin. Two adjacent positive margins of 20px and 30px can create a 30px gap rather than 50px.

This does not mean all margins collapse. Horizontal margins do not, and padding never collapses. Parent-child relationships can also produce collapsing margins in appropriate conditions.

Flex and grid layouts have different spacing behaviour, which you will learn next. For now, if a vertical gap surprises you, inspect the boxes rather than assuming every margin simply adds.

09 | TRY IT: MEASURE THE LAYERS

Change the width, padding, border and margin. The preview highlights the margin area in pale pink and padding in pale green. These background colours are demonstration aids; margin itself has no background.

Content area

The preview can scroll horizontally on a small screen. These calculations use a fixed-width box with symmetric pixel spacing and no other size constraints.

10 | INSPECT AND TEST YOUR SPACING

  1. Use developer tools to inspect content, padding, border and margin.
  2. Check box-sizing before calculating the total width.
  3. Distinguish padding inside a box from margin outside it.
  4. Test at a narrow width and with enlarged text.
  5. Remove fixed heights that unnecessarily clip content.

Consistent spacing helps people recognise groups of related information. Choose a few useful spacing values and reuse them rather than adding a different number to every element.

MATCH THE BOX MODEL TERM

Choose the appropriate term.

Terminology

Terminology

Box model

The model describing content, padding, border and margin.

Content area

The area where text, images and other content are laid out.

Padding

Space between the content and border.

Border

The boundary surrounding padding and content.

Margin

Space outside the border.

content-box

A sizing model in which width describes the content box.

border-box

A sizing model in which width includes padding and border.

box-sizing

The property selecting the sizing model.

Spacing shorthand

A compact syntax setting several sides of margin or padding.

max-width

A property limiting a box’s width.

Auto horizontal margins

Margins that can share spare horizontal space in ordinary block layout.

Margin collapse

The combining of certain vertical margins in ordinary block layout.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. Which space lies between content and border?
2. What does padding: 10px 20px; mean?
3. A content-box has width 200px, padding 10px and a 2px border on each side. What is its border-box width?
4. A border-box has width 200px, padding 10px and a 2px border on each side. What is its content width?
5. Which statements are true?
6. Which can centre a narrower block in ordinary block layout?
7. Which statements about margin collapse are true?
8. Which help adaptable cards?

CALCULATE AND EXPLAIN

Show your working before revealing each sample.

1. A content-box has width 240px, padding 16px and borders 3px on each side. Calculate its border-box width.

2. Use the same values with border-box. Calculate the content width.

3. Explain margin: 8px 12px 16px 20px;.

4. Explain why padding and margin are not interchangeable.

5. Write CSS for a full-width card capped at 600px, centred in ordinary block layout, with 20px padding and a 2px border.

6. Two adjacent block margins of 20px and 30px produce a 30px gap. Explain one possible cause.

BUILD | GIVE YOUR PROJECT CARDS SPACE

Improve the project cards in your club website. Use a consistent border, internal padding and external spacing. Add border-box sizing and an appropriate maximum width.

Extension: inspect a card in developer tools. Record its content width, padding, border and margin, then verify the border-box width using your own calculation.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 03.4 The Box Model: Margin, Border and Padding is coming soon. Complete the project-card spacing task and keep your HTML and CSS files.