This is the single most important idea in CSS layout: every element is a rectangular box. Headings, paragraphs, images and links are all boxes, even when they don't look like it. Once you understand the four layers of that box, spacing and sizing stop being mysterious.
Most layout problems beginners run into, such as an element that is wider than expected, a gap that won't go away, or text spilling out of a card, come down to the box model. This lesson gives you the mental picture you'll use in every layout you build.
The four layers of the box model
From the inside out:
- Content is the text or image itself. You size it with
widthandheight. - Padding is space inside the box, between the content and the border. It takes on the element's background colour.
- Border is the line around the padding.
- Margin is space outside the border, pushing other boxes away. It's always transparent.
Open your browser's DevTools (F12), select any element and look for the box model diagram. It shows these four layers with their exact sizes, which is the quickest way to see why an element is the size it is.
Why elements end up wider than you expect
By default, width sets only the content width. Padding and border are added on top:
width: 200px + padding: 20px (×2) + border: 5px (×2) = 250px wide!That's confusing, so almost every modern stylesheet starts with this rule:
*, *::before, *::after { box-sizing: border-box; }With border-box, width: 200px means the whole box is 200px wide, and the padding and border fit inside. This makes sizes predictable, especially when you mix percentages with padding.
Sizing helpers and overflow
max-width: 600pxkeeps an element from growing wider than 600px while letting it shrink. Combine it withwidth: 100%for boxes that fit any screen.min-height: 200pxmakes an element at least 200px tall while letting it grow with its content.overflowdecides what happens when content is too big for the box:visible(the default),hidden,scroll, orauto, which shows scrollbars only when needed.aspect-ratio: 16 / 9keeps a shape in proportion as the width changes.
Prefer min-height to a fixed height for anything containing text. A fixed height looks fine with your sample text, then overflows when real content or a larger font size arrives.
CSS Box Model Syntax
Box model properties
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 4px solid;
margin: 16px;
}CSS Box Model Summary
- Every element is a box made of content, padding, border and margin.
- Padding is inside the border and shows the background; margin is outside and transparent.
- By default, padding and border are added to the
widthyou set. box-sizing: border-boxmakeswidthinclude padding and border.- Use
max-width,min-heightandoverflowto keep boxes flexible.
CSS Box Model Examples
Each example runs in the page. Edit the code and press Run to see what changes.
See every layer
The browser DevTools (F12) show exactly this diagram when you inspect an element.
<div class="box">Content</div>
<p>Next element, pushed down by the margin.</p>.box {
width: 200px;
padding: 20px;
border: 6px solid #4467ff;
margin: 24px;
background: #e1e6ff;
font-family: sans-serif;
}Line-by-line explanation
- Line 2The content area is 200px wide.
- Line 320px of padding inside. Notice the background fills it.
- Line 4A 6px border around the padding.
- Line 524px of empty space outside, on every side.
- Lines 2 to 4Total visible width: 200 + 20 + 20 + 6 + 6 = 252px.
content-box vs border-box
Both boxes say width: 200px. Only one is actually 200px wide.
<div class="a">content-box (default)</div>
<div class="b">border-box</div>
<div class="ruler">200px ruler</div>.a, .b {
width: 200px;
padding: 20px;
border: 5px solid #15161b;
margin-bottom: 8px;
font-family: sans-serif;
}
.a { box-sizing: content-box; background: #ffe3d7; }
.b { box-sizing: border-box; background: #d6f4e4; }
.ruler { width: 200px; background: #15161b; color: white; font: 12px monospace; }Line-by-line explanation
- Lines 2 to 4Same width, padding and border on both.
- Line 9content-box: 200 + 40 + 10 = 250px wide.
- Line 10border-box makes it exactly 200px, with padding and border squeezed inside.
Overflow and aspect ratio
<div class="scroll">
This box is only 80px tall but has far too much text in it. With overflow: auto, a scrollbar appears so nothing spills out over the rest of the page. Scroll me!
</div>
<div class="video">16 : 9</div>.scroll {
height: 80px;
overflow: auto;
border: 2px dashed #f0592b;
padding: 8px;
}
.video {
width: 100%;
max-width: 320px;
aspect-ratio: 16 / 9;
margin-top: 12px;
display: grid;
place-items: center;
background: #15161b;
color: white;
}Line-by-line explanation
- Lines 2 to 3Fixed height +
overflow: auto= scroll inside the box when needed. - Line 11The box always keeps a 16:9 shape, like a video.
Common CSS Box Model Mistakes
width: 100% plus padding
.input { width: 100%; padding: 12px; }
/* overflows its parent by 24px */* { box-sizing: border-box; }
.input { width: 100%; padding: 12px; }With the default content-box, 100% + padding is wider than the parent. border-box fixes it.
Fixed heights on text boxes
.card { height: 150px; }.card { min-height: 150px; }Text grows (translations, bigger fonts, zoom). A fixed height makes it overflow; min-height lets the box grow.
CSS Box Model in Real Projects
The universal reset
Open the CSS of almost any modern website or framework (Bootstrap, Tailwind, this site) and near the top you'll find box-sizing: border-box applied to everything. It's that important.
CSS Box Model Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Make width include padding and border.
* {
: ;
}
border-box makes width/height describe the whole box.Which layer of the box model is always transparent?
With the default content-box, how wide does this box appear?
.box { width: 100px; padding: 10px; border: 2px solid; }
The search input overflows its 300px container. Make it fit exactly without removing the padding or border.
<div class="bar"><input class="search" placeholder="Search…"></div>
<style>.bar { width: 300px; background: #eee; }</style>.search {
width: 100%;
padding: 12px;
border: 2px solid #15161b;
}- The input is exactly 300px wide
- Padding is still 12px
CSS Box Model Quiz
3 questions to check what you learned.
What's the order of the box model layers from inside out?
- margin, border, padding, content
- content, padding, border, margin
- content, margin, border, padding
- padding, content, border, margin
Which overflow value adds scrollbars only when needed?
- auto
- scroll
- hidden
- visible
Does a background colour fill the padding area?
- No, only the content
- Only with border-box
- Yes
- Only with overflow: hidden
CSS Box Model Challenge
A perfectly sized card
Make .card:
- Use
box-sizing: border-box - Exactly
260pxwide in total, with24pxpadding and a3pxborder - At least
140pxtall (usemin-height) - Have
20pxof margin below it
<div class="card">Exactly 260px wide, including padding and border.</div>
<p>Next thing on the page</p>- Total width is 260px
- Uses border-box
- Padding is 24px
- Border is 3px
- At least 140px tall
- 20px margin below