Book a free demo

Web Developer Path ยท lesson 11 of 18

The box model

Every element is a box. Learn how its content, padding, border and margin add up so you can space and size anything.

What you will learn

  • the four layers: content, padding, border, margin
  • shorthand order: top, right, bottom, left
  • box-sizing: border-box and why it is the sensible choice
  • centring a box with margin: 0 auto

Understand the idea

To the browser every element is a rectangular box with four layers. In the middle is the content. Around it is padding, space inside the box. Then comes the border, a line around the padding. Outside is the margin, empty space that keeps other boxes away. Padding takes the background colour of the box, but margin is always transparent.

Shorthand sets all four sides together. margin: 10px 20px means 10px top and bottom and 20px left and right. With four values they go clockwise: top, right, bottom, left (margin: 1px 2px 3px 4px). The same works for padding. A border needs a width, a style and a colour: border: 2px solid navy;.

A trap: by default width is the width of the content only, so padding and border make the box wider than you asked. box-sizing: border-box changes that so width includes the padding and border, which makes sizes predictable. Many developers set it on every element.

To centre a box horizontally give it a width and set margin: 0 auto. The two automatic side margins share the leftover space equally.

Example

A centred, predictable card
.card {
  box-sizing: border-box;
  width: 240px;
  padding: 16px;
  border: 3px solid royalblue;
  margin: 12px auto;
}

With border-box the card is exactly 240px wide, padding and border included. The automatic side margins centre it.

Your turn: code to type and practise

Style .card: give it a width, some padding, a border, a margin of 12px auto, and box-sizing: border-box.

Stretch challenge

Remove box-sizing and measure how much wider the card becomes in the preview.

โ† Combinators, pseudo-classes and pseudo-elementsDisplay and position โ†’

CSS

More in this module

Keep going in Web Developer Path

Practise the idea

๐Ÿ—๏ธ Play Block Views

Count hidden cubes, match front, top and side views, then build a stack from three plans.

Play now

FAQ

The box model: common questions

What is the CSS box model?

The idea that every element is a box made of content, padding, border and margin, from the inside out.

What does box-sizing: border-box do?

It makes the width and height include padding and border, so the box is the size you set.

How do I centre a div horizontally?

Give the div a width, then set margin: 0 auto. The automatic left and right margins share the leftover space equally, so the box sits in the middle.

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try