Chapter 5 of 15

The CSS Box Model: Padding, Border and Margin

In CSS, every element on a page is a box. When you can see the boxes, layout gets much easier.

Four layers of every box

Box model: margin outside, then border, then padding, then content in the middle
  • Content: the text or image.
  • Padding: space inside the box, around the content.
  • Border: a line around the padding.
  • Margin: space outside the box, between this box and others.
CSS
.card {
  padding: 20px;
  border: 3px solid black;
  margin: 30px;
}
Result in the browser
I am a card with padding and a border.

Short ways to write spacing

CSS
margin: 10px;                 /* all four sides */
margin: 10px 20px;            /* top+bottom, left+right */
margin: 10px 20px 30px 40px;  /* top, right, bottom, left (clockwise) */
padding-top: 5px;             /* only one side */
Easy memory trickFour values go clockwise from the top, like a clock: top → right → bottom → left.

Width and height

CSS
.box {
  width: 100%;
  max-width: 600px;  /* never wider than 600px */
  min-height: 200px; /* at least 200px tall, can grow */
}

Avoid fixed heights like height: 200px on boxes with text. On a small phone, the text needs more lines, and it spills out of the box. min-height gives the same look but lets the box grow when needed.

When content is too big: overflow

CSS
.box { height: 100px; overflow: auto; }  /* add a scrollbar if needed */
Result in the browser
This box is only 100px tall, but it has a lot of text. Because of overflow: auto, a scrollbar appears instead of the text spilling out. Keep scrolling to read the rest. This is useful for code blocks and long tables.
ValueWhat happens to extra content
visibleSpills outside (default)
hiddenIs cut off
autoGets a scrollbar only when needed

Block and inline boxes

Boxes come in two main types:

Left: three block boxes stacked full width. Right: inline words highlighted inside a paragraph
Block boxes start on a new line. Inline boxes stay inside the line of text.
BlockInline
Examplesdiv, p, h1, ula, strong, em, span
Starts on a new line?YesNo, stays inside the text
WidthFull width by defaultOnly as wide as its content
Top/bottom margin works?YesNo

You can change the type with display: display: block;, display: inline; or display: inline-block; (inline, but margins work).

<div> and <span> have no meaning. They are empty boxes for styling: div is block, span is inline.

Buttons with inline-block

Links are inline, so top and bottom padding does not push other things away. For a button-style link, use inline-block:

CSS
.btn {
  display: inline-block;
  padding: 12px 24px;
  background: #2563eb;
  color: white;
  border-radius: 8px;
  text-decoration: none;
}
Result in the browserGet started Learn more

Fix widths with box-sizing

By default, padding and border are added to the width. A box with width: 300px and padding: 20px becomes 340px wide. This confuses everyone. Add this at the top of every stylesheet:

Two boxes with 20px padding: content-box becomes 260px wide, border-box stays 220px
With box-sizing: border-box, the width you write is the width you get.
CSS
* {
  box-sizing: border-box;
}

Now width: 300px means the full box is 300px, padding and border included.

Center a box

CSS
.page {
  max-width: 900px;
  margin: 0 auto;   /* auto left and right = centered */
}

max-width says "never wider than 900px, but smaller is fine on phones". margin: 0 auto splits the empty space equally on both sides.

A blue box in the middle of a wider area with equal auto margins on both sides
margin: 0 auto centers a box that has a width.
Margins can mergeWhen two block boxes sit on top of each other, their top and bottom margins merge into one. The bigger margin wins. This is called margin collapse. It is normal behaviour.

See the boxes in DevTools

Right-click an element and choose Inspect. Scroll to the bottom of the Styles panel (or open the Computed tab). You will see a coloured picture of the box model with the exact numbers for margin, border, padding and content.

Hover over elements in the HTML panel and the page highlights them: blue for content, green for padding, orange for margin. When spacing looks wrong, this tells you which layer is causing it.

Mini project: a product card

HTML
<div class="card">
  <h3>Cotton T-shirt</h3>
  <p>Soft, light and perfect for summer.</p>
  <a class="btn" href="#">Buy now</a>
</div>
CSS
* { box-sizing: border-box; }

.card {
  max-width: 300px;
  margin: 20px auto;
  padding: 24px;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
}
.card h3 { margin: 0 0 8px; }
.card p  { margin: 0 0 16px; color: #4b5563; }
Result in the browser
Cotton T-shirt

Soft, light and perfect for summer.

Buy now

Every space in this card comes from padding or margin. Try changing each number and see which gap moves.

Common mistakes

ProblemFix
The page scrolls sideways a littleSomething is wider than the screen. Add box-sizing: border-box and use max-width instead of width.
margin: 0 auto does not centerThe box needs a width or max-width, and it must be a block box
Padding on a link does not push lines apartLinks are inline. Use display: inline-block.
A gap appears above the first headingHeadings have a default top margin. Set margin-top: 0.

Cheat sheet

PropertyWhat it does
paddingSpace inside the border
marginSpace outside the border
borderLine around the box
box-sizing: border-boxWidth includes padding and border
max-widthNever wider than this
min-heightAt least this tall, can grow
displayblock, inline or inline-block
overflowWhat to do with extra content

Practice time

  1. Make a .card box with padding, a border and rounded corners (border-radius: 12px).
  2. Center your main content with max-width and margin: 0 auto.
  3. Use Inspect in your browser: hover an element and look at the coloured box-model picture.
  4. Build the product card, then make three of them.
  5. Use DevTools to find out where the space above a heading comes from.

Quick summary

  • Every element is a box: content, padding, border, margin.
  • Block boxes stack; inline boxes flow inside text.
  • Always use box-sizing: border-box.