Every element is a box

In CSS, every HTML element โ€” paragraphs, divs, buttons, images โ€” is treated as a rectangular box. Understanding the box model is the foundation of web layout.

The box model has four layers, working outward from the content:

  1. Content โ€” The actual text, images, or child elements.
  2. Padding โ€” Space inside the element, between the content and the border.
  3. Border โ€” A frame around the padding.
  4. Margin โ€” Space outside the border, between this element and others.
Mental model:
Think of a framed photo. The photo is the content, padding is the mat around the photo, the border is the frame, and margin is the space between this frame and the next one on the wall.

Content area โ€” width and height

The content area holds the actual content: text, images, child elements. You control its size with width and height.

box-model.css Content area sizing

width: 100% means "100% of the parent's width." max-width is a responsive pattern โ€” the element grows to fit the parent, but never exceeds the max.

Padding โ€” space inside

Padding adds space inside the element, pushing the content away from the border. It's part of the element's background color.

padding.css Padding shorthand

The four-value shorthand follows a clockwise pattern: top, right, bottom, left (remember: TRouBLe). The two-value form is the most common in practice โ€” vertical and horizontal.

Border โ€” the frame

A border wraps around the padding and content. It's visible and takes up space. For detailed border styling (colors, styles, rounded corners), see Lesson 21: Borders & Outlines โ†’

border.css Basic border syntax

Margin โ€” space outside

Margin adds space outside the element, between this element and its neighbors. Unlike padding, margin is outside the background color.

margin.css Margin shorthand

Margin can be negative, which pulls an element closer to (or over) its neighbors. This is rarely used but useful for advanced layouts.

Try it yourself โ€” Visual box model

Edit the CSS to change padding, border, and margin. Watch the box expand and shrink. Notice how padding changes the background, while margin creates space outside.

โœ๏ธ Try It Yourself โ€” Edit & Run
Preview

box-sizing: content-box vs border-box

By default, width and height apply only to the content area. This can be confusing โ€” a 300px box with padding becomes wider than 300px. The box-sizing property fixes this.

box-sizing.css Two sizing models

With box-sizing: border-box, the 300px width includes padding and border. This is the modern standard โ€” almost every website uses it. Add this to the top of every stylesheet:

* { box-sizing: border-box; }

Why border-box?
It makes math easier. If you set width: 300px, the element is exactly 300px wide, no surprises from padding or borders.

Try it yourself โ€” content-box vs border-box

Below are two identical boxes. The left uses content-box (default), the right uses border-box. Both are set to 300px width, but notice the difference!

โœ๏ธ Try It Yourself โ€” box-sizing comparison
Preview

Centering with margin: auto

One of the most powerful uses of margin is centering. For block elements with a fixed width, margin: 0 auto centers them horizontally on the page.

centering.css Center a block element

margin: 0 auto means "0 margin on top/bottom, auto (equal on both sides) on left/right." This centers the element.

Margin collapse โ€” a gotcha!

When two vertical margins meet (between sibling elements or parent/child), the larger margin "wins" and the smaller one is ignored. This is called margin collapse, and it surprises many developers.

margin-collapse.css Vertical margins collapse

Vertical margins collapse; horizontal margins add up. This is why spacing can feel unpredictable until you understand it. Horizontal margins (on inline or inline-block elements) always add together.

Pro tip:
When margins aren't working as expected, check if vertical margins are collapsing. Add padding or a border to the parent to prevent collapse.

All spacing properties

spacing.css Complete reference
Challenge: Build Pricing Cards

Task: Create three pricing cards side by side on a centered page. Each card should have:

  • A fixed width (e.g., 250px)
  • Internal padding (20px)
  • A visible border (2px)
  • Margin between cards (20px)
  • All using box-sizing: border-box so width is exact

The container should be centered on the page using margin: 0 auto.

Hint: Create a container div with a max-width (e.g., 850px) and margin: 0 auto. Inside, create three card divs with class="card". Set each card to width: 250px, padding: 20px, border: 2px solid, margin-right: 20px. Use * { box-sizing: border-box; } at the top of your CSS. Remove the last card's margin-right with :last-child { margin-right: 0; }

Key takeaways

  • Every element is a box with four layers: content, padding, border, margin (inside to outside).
  • width and height size the content area.
  • Padding is space inside, part of the background. padding: 20px or padding: 10px 20px (vertical, horizontal).
  • Border is the frame. border: 2px solid #000.
  • Margin is space outside. margin: 20px or margin: 0 auto (center).
  • box-sizing: border-box makes width include padding and border. Use it everywhere.
  • Vertical margins collapse โ€” the larger margin wins. Horizontal margins add up.
  • margin: 0 auto with a width centers block elements horizontally.

Test your knowledge

Question 1 of 5
In the box model, which layer is between content and border?
Question 2 of 5
What does box-sizing: border-box do?
Question 3 of 5
How do you horizontally center a block element with a fixed width?
Question 4 of 5
What happens when two vertical margins meet?
Question 5 of 5
What does padding: 10px 20px mean?
0
/5
Great work! You understand the box model.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You just mastered the box model.
The layout world is yours.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’