Sikau

CSS · Day 5 · about 30 min

The box model

Content, padding, border, margin.

Everything is a box

Every element is a rectangle with four layers: the content, padding around it, a border, and margin outside. This is the box model, and it explains almost every spacing puzzle.

The four layers

  • Shorthand: padding: 8px 16px = top/bottom 8, left/right 16. Four values go clockwise: top, right, bottom, left.
  • box-sizing: border-box makes width include padding and border — far easier. Most sites set it on everything with * { box-sizing: border-box; }.
  • Vertical margins between blocks collapse: two 12px margins make a 12px gap, not 24px.
  • margin: 0 auto centres a block that has a width.

The rest of this lesson

  • See the boxesexplain
  • Spacing like a designerexplain
  • How wide is it?predict
  • Inside outorder
  • Your turnwrite code
  • Badge quizquiz
  • Unit 1 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 19 lessons of CSS