Skip to content

The CSS Box Model: margin, padding, border and box-sizing Explained

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

To a browser, every element is a rectangle. A heading, a paragraph, a button and even a single link are all boxes, and the space inside and around them is what makes a page feel cramped or calm. Once you can see those boxes and control their four layers, spacing stops being trial and error, and layouts in later lessons such as Flexbox and Grid make sense.

Quick answer

Each box has four layers from the inside out: content, padding, border and margin. By default width sets only the content, so padding and border are added on top; box-sizing: border-box makes width include them. Vertical margins between blocks collapse into the larger one, and margin-inline: auto centres a block that has a width.

20 min read and practice · Content checked on 21 October 2026

In this lesson you will

  • Name the four layers of a box and set each one
  • Calculate how wide a box really is, with and without border-box
  • Explain margin collapsing and centre a block with auto margins
  • Tell block, inline and inline-block boxes apart

Four layers: content, padding, border, margin

The content area holds the text or image. padding is space inside the box, between the content and the border, and it shows the box’s background colour. border is the line around the padding. margin is space outside the border that pushes other boxes away; it is always transparent.

Each layer has four sides. You can set them one by one (padding-top, margin-left and so on) or with a shorthand. One value applies to all sides, two values mean top-and-bottom then left-and-right, and four values go clockwise from the top: margin: 10px 20px 30px 40px is top, right, bottom, left. A border needs a width, a style and a colour: border: 2px solid #0f766e. Without a style such as solid, no border appears.

Developer tools draw these layers for you. Inspect any element and you will see a diagram with the content size in the middle and padding, border and margin around it, each with its numbers. It is the fastest way to find out where an unexpected gap comes from.

Try it: See the layers
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Change padding to 40px and watch the white area grow. Change margin to 0 and the cards touch.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between margin and padding?

Padding is space inside the border and takes the box’s background. Margin is space outside the border, always transparent, and it pushes neighbouring boxes away. Use padding to give content breathing room and margin to separate boxes.

Should I always use box-sizing: border-box?

For almost every project, yes. Set it once on all elements at the start of your stylesheet. Widths then include padding and border, which is how most people expect a size to work.

How do I centre a div horizontally?

Give it a max-width (or width) smaller than its container and set margin-inline: auto. To centre the text inside it, use text-align: center instead. Flexbox and Grid, covered in the next lessons, can centre in both directions.

Why is there a gap I did not add?

Usually it is a default margin from the browser, on body, headings, paragraphs or lists. Inspect the element in developer tools: the box model diagram shows which element owns the margin, and you can set it to 0.

Can margins be negative?

Yes. A negative margin pulls the box, or its neighbours, closer and can make boxes overlap. It has real uses, but it is easy to create confusing layouts with it, so reach for padding, gap in Flexbox or Grid, or positioning first.

Sources

Facts in this lesson were checked against these official pages on 21 October 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.