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:
- Content โ The actual text, images, or child elements.
- Padding โ Space inside the element, between the content and the border.
- Border โ A frame around the padding.
- Margin โ Space outside the border, between this element and others.
Content area โ width and height
The content area holds the actual content: text, images, child elements. You control its size with width and height.
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.
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 โ
Margin โ space outside
Margin adds space outside the element, between this element and its neighbors. Unlike padding, margin is outside the background color.
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.
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.
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; }
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!
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.
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.
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.
All spacing properties
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-boxso 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).
widthandheightsize the content area.- Padding is space inside, part of the background.
padding: 20pxorpadding: 10px 20px(vertical, horizontal). - Border is the frame.
border: 2px solid #000. - Margin is space outside.
margin: 20pxormargin: 0 auto(center). box-sizing: border-boxmakes width include padding and border. Use it everywhere.- Vertical margins collapse โ the larger margin wins. Horizontal margins add up.
margin: 0 autowith a width centers block elements horizontally.
Test your knowledge
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 โ