Skip to content
skillbld
HTML & CSS · 14-day planAll days
Day 8 of 14 25 min

Every element is a rectangular box made of content, padding, border and margin. Learn to control each layer, set widths and heights, switch to box-sizing border-box, understand block and inline elements, round corners and add shadows, and make images fit their container.

By the end of today you can

  • Name the four layers of the box model
  • Set padding, border and margin, with shorthand for each side
  • Use box-sizing border-box so widths include padding and border
  • Tell block, inline and inline-block elements apart
  • Centre a box with margin auto and keep images inside their container

Look closely at any web page and you'll see boxes everywhere: buttons, cards, paragraphs, images. In CSS, every element is a box. Understanding that box is the key to spacing and sizing everything.

Mind map · click a dot to fold, drag to pan

The four layers

Sketch
The CSS box model as nested rectangles. In the centre is the content: the text or image, sized by width and height. Around it is padding, space inside the border that takes the background colour. Then the border, a line around the padding. Outermost is margin: transparent space outside the border that pushes other elements away.
index.html · edit it, the preview updates
Page preview
  • Content: the text or image.
  • Padding: space inside the border. It takes the background colour.
  • Border: a line around the padding.
  • Margin: space outside the border, pushing neighbours away. Always transparent.

Change the numbers and watch each layer grow. In the browser's DevTools (right-click → Inspect), the Computed tab draws this exact diagram for any element.

Shorthand: one to four values

index.html · edit it, the preview updates
Page preview

Four values go clockwise from the top: top, right, bottom, left. The same works for margin. Borders are written as border: width style colour, e.g. border: 2px solid teal, with styles like solid, dashed and dotted.

box-sizing: border-box

By default, width sets only the content width; padding and border are added on top. That makes sizes hard to reason about:

index.html · edit it, the preview updates
Page preview

With box-sizing: border-box, width includes padding and border. Almost every modern site starts its CSS with:

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

Block, inline and inline-block

index.html · edit it, the preview updates
Page preview
displayBehaves likeWidth & height?
blockdiv, p, h1: new line, full widthYes
inlinespan, a, strong: flows inside textNo
inline-blockInline, but sized like a boxYes
noneHidden completely

Later you'll meet display: flex and display: grid, which lay out a box's children.

Sizing and centring

index.html · edit it, the preview updates
Page preview
  • max-width lets a box shrink on small screens but stops it growing too wide. Prefer it to a fixed width.
  • margin: 0 auto (or 20px auto) centres a block horizontally.
  • img { max-width: 100%; height: auto; } keeps images inside their container: the fix for Day 3's oversized image. Put it in every stylesheet.
  • border-radius rounds corners; 50% on a square makes a circle.
  • box-shadow: x offset, y offset, blur, colour.

Margins collapse

When two vertical margins meet (say, a paragraph's bottom margin and the next one's top margin), they collapse into the larger one instead of adding up. It surprises everyone once. Flexbox and Grid layouts don't do this.

Recap

  • Every element is a box: content → padding → border → margin.
  • Shorthand: 1 value = all sides; 2 = vertical/horizontal; 4 = top, right, bottom, left.
  • Use box-sizing: border-box everywhere.
  • block (new line, sizable), inline (in text, no size), inline-block (both).
  • max-width + margin: 0 auto centres; max-width: 100% keeps images inside.

Tomorrow: typography, colour and CSS variables.

Knowledge check

Test what you learned

5 questions · pick an answer to see if you're right

0/5
  1. 1

    From the inside out, what are the layers of the box model?

  2. 2

    A box has width: 300px; padding: 20px; border: 5px solid. Without box-sizing: border-box, how wide is it on screen?

  3. 3

    What does padding: 10px 20px; mean?

  4. 4

    Why does setting width on a <span> do nothing?

  5. 5

    How do you centre a block of max-width: 600px horizontally?

Was this lesson helpful?

Need a breather? Take a two-minute break

Keep going