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.
Console
- 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
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.