Nishaglobal Education logo

The box model: spacing, borders and sizes

Every element is a box. Control its padding, border, margin, width and height, use box-sizing border-box, centre a page with margin auto, and understand block and inline display.

Lesson 8 of 1335 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Name the four layers of the box model: content, padding, border and margin
  • Set widths with max-width and box-sizing: border-box
  • Centre a block and choose between block, inline and inline-block display
Outline
  1. What you'll learn and why it matters
  2. The four layers of a box
  3. One value, two values, four values
  4. Widths and box-sizing
  5. Block, inline and inline-block
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

What you'll learn and why it matters

To the browser, every element is a rectangular box. A heading, a paragraph, a link: all boxes. Spacing, borders and sizes all come from one idea, the box model. Once you understand it, cards, buttons and page layouts stop being a mystery.

The four layers of a box

margin: space outside the borderborderpadding: space insidecontentyour text or imageWith box-sizing: border-box, width includescontent + padding + border (not margin).
Nested layers from outside in: margin (space outside the border), border, padding (space inside the border) and content. With box-sizing: border-box, the width includes content, padding and border but not margin.
LayerPropertyWhat it is
Contentwidth, heightThe text or image itself
PaddingpaddingSpace between the content and the border. The background colour fills it.
BorderborderA line around the padding
MarginmarginSpace outside the border that pushes other boxes away. Always transparent.
CSS
.card {
  background-color: #fef3c7;
  padding: 16px;
  border: 3px solid #b45309;
  margin: 24px;
}
Show the HTML this CSS styles
<div class="card">
  <h2>Masala chai</h2>
  <p>Black tea simmered with milk, ginger and cardamom.</p>
</div>

Browser preview

Try picturing it: the yellow background fills the content and the padding, the brown line is the border, and the 24px margin is the empty space around the card.

One value, two values, four values

padding and margin can take one to four values. With four, they go clockwise from the top: top, right, bottom, left.

You writeMeans
padding: 16px;16px on all four sides
padding: 8px 16px;8px top and bottom, 16px left and right
padding: 4px 8px 12px 16px;top 4, right 8, bottom 12, left 16
margin-top: 32px;Only one side. Also -right, -bottom, -left

border usually takes three values in any order: width, style and colour, for example border: 2px solid #94a3b8;. Styles include solid, dashed and dotted. border-radius rounds the corners.

Widths and box-sizing

By default, width sets only the content width, and padding and border are added on top. So a box with width: 300px, 20px padding and a 5px border is really 350px wide. That surprises everyone. The fix is one rule that nearly every website uses:

Put this at the top of every stylesheet
*,
*::before,
*::after {
  box-sizing: border-box;
}

With border-box, width: 300px means the whole visible box is 300px, padding and border included. Much easier.

CSS
* {
  box-sizing: border-box;
}

.card {
  max-width: 320px;
  padding: 20px;
  border: 2px solid #b45309;
  border-radius: 12px;
  background-color: #fef3c7;
  margin: 24px auto;
}
Show the HTML this CSS styles
<div class="card">
  <h2>Masala chai</h2>
  <p>Black tea simmered with milk, ginger and cardamom.</p>
</div>

Browser preview

Two new ideas in that example:

CodeEffect
max-width: 320pxThe box is never wider than 320px, but it can shrink on a narrow phone screen. Better than a fixed width.
margin: 24px auto24px above and below, and auto left and right: the browser shares the leftover space equally, which centres the box.

Block, inline and inline-block

The display property decides how a box behaves in the flow of the page:

ValueStarts on a new line?Width and height work?Default for
blockYes, and fills the widthYesdiv, p, h1, section, li
inlineNo, sits inside textNo; vertical padding does not push lines apartspan, a, strong, em
inline-blockNo, sits in the lineYesButtons; useful for tags and badges
noneNot shown at all-Hidden content
CSS
.tag {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 999px;
  background-color: #dbeafe;
  color: #1e3a8a;
  margin: 0 4px 8px 0;
}
Show the HTML this CSS styles
<p>Skills:
  <span class="tag">HTML</span>
  <span class="tag">CSS</span>
  <span class="tag">Accessibility</span>
</p>

Browser preview

Practice

Guided exercise

A notice box

Turn the paragraph with class notice into a box: light blue background, 16px padding, a 4px solid blue border on the left side only, rounded corners, and 24px of space above and below.

  1. Select .notice.
  2. Add background-color and padding: 16px.
  3. Use border-left: 4px solid #2563eb; for one side only.
  4. Add border-radius: 8px and margin: 24px 0.
Show the solution
Solution
.notice {
  background-color: #eff6ff;
  padding: 16px;
  border-left: 4px solid #2563eb;
  border-radius: 8px;
  margin: 24px 0;
}
Show the HTML this CSS styles
<p>Our new term starts on Monday.</p>
<p class="notice">Bring your ID card on the first day.</p>
<p>See you soon!</p>

Browser preview

How to get there: Padding makes room inside the box so the text does not touch the edges. border-left sets just one side, and margin: 24px 0 adds space above and below but none at the sides.

Your turn

Centre a button-style link

Make the link with class button look like a button: inline-block, white text on dark green (#166534), 12px top and bottom padding, 24px left and right, rounded corners and no underline.

Show the solution
Solution
.button {
  display: inline-block;
  color: white;
  background-color: #166534;
  padding: 12px 24px;
  border-radius: 8px;
  text-decoration: none;
}
Show the HTML this CSS styles
<p>Ready to order?</p>
<p><a class="button" href="order.html">Order now</a></p>

Browser preview

How to get there: Links are inline, so vertical padding would overlap nearby lines. inline-block makes the padding count, giving a solid button shape while it stays in the line of text.

Common mistakes

Forgetting box-sizing: border-box

You see: Boxes are wider than the width you set, and layouts overflow

Fix: Add the box-sizing rule at the top of your stylesheet.

Using a fixed width like width: 800px

You see: Sideways scrolling on phones

Fix: Use max-width so the box can shrink.

Trying margin: auto to centre text

You see: The text stays on the left

Fix: margin: auto centres a block box that has a width; use text-align: center to centre text inside a box.

Setting width or vertical padding on an inline element

You see: Nothing changes, or the padding overlaps other lines

Fix: Use display: inline-block or block first.

Mixing up padding and margin

You see: Background colour does not reach the space you added, or vice versa

Fix: Padding is inside the border (coloured); margin is outside (transparent).

Recap

  • Every element is a box: content, padding, border, margin.
  • Shorthand values go top, right, bottom, left (clockwise).
  • Use box-sizing: border-box and max-width; margin: 0 auto centres a block.
  • display decides behaviour: block, inline, inline-block or none.

Key terms

Box model
The way CSS treats every element as content surrounded by padding, border and margin.
Padding
Space inside an element's border, filled by its background.
Margin
Transparent space outside an element's border.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. Which layer of the box is filled by the background colour?

  2. 2. `padding: 10px 20px;` gives how much padding on the left?

  3. 3. With box-sizing: border-box, a box has width: 300px, padding: 20px and a 5px border. How wide is it on screen?

  4. 4. Which rule centres a block with max-width: 600px horizontally?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.