CSS Box Sizing
* {
box-sizing: border-box;
}
.box {
width: 300px;
padding: 24px;
}
Let me ask you something.
Have you ever set the width of an element to 300px, added some padding to it, and then wondered why it was suddenly wider than 300px? You told it to be 300px. Why is it not 300px?
I remember being genuinely confused by this for longer than I would like to admit. It felt like CSS was lying to me.
It was not lying. It was just using a sizing model I did not fully understand yet. Once I learned about box sizing everything clicked and layouts stopped behaving unexpectedly.
Let me explain it properly.
How CSS Calculates Size by Default
By default CSS calculates the size of an element like this.
The width and height you set only apply to the content inside the element. Any padding or border you add gets added on top of that.
So if you write this:
This is a box
You told the box to be 300px wide. But what you actually get is:
- 300px content width
- plus 24px padding on the left
- plus 24px padding on the right
- plus 2px border on the left
- plus 2px border on the right
Total actual width — 352px.
That is the default behavior. The width you set is just for the content. Everything else gets added on top. This is called content-box sizing and it is the CSS default.
The Problem This Causes
Imagine you are building a layout with two columns sitting side by side. You want each column to be exactly 50% wide so they fill the page perfectly.
You would expect them to sit perfectly side by side. But they do not. They break onto separate lines because each column is actually wider than 50% once the padding is added.
This is the kind of layout bug that makes beginners want to give up. But there is a simple fix.
The Fix — border-box
CSS gives you a different sizing model called border-box. When you use it the width and height you set include the padding and border — not just the content.
So if you set width to 300px and add 24px of padding the element stays 300px total. The browser shrinks the content area to make room for the padding instead of expanding the whole element.
This is a box
Now the box is exactly 300px wide. The padding fits inside that 300px. No surprises.
Apply It to Everything
Here is what you should do on every single project you build. Right at the very top of your CSS file before anything else write this:
This is a box
The universal selector targets every single element on the page and sets them all to border-box. Now every element you create will behave predictably. The width you set is the width you get.
Adding box-sizing: border-box to the universal selector is something every professional developer does
on every project. It is so universally used that some people consider it a bug that content-box is still the CSS
default. Just add it at the top of every CSS file you ever write and never think about it again.
Why This Matters for Layouts
Let us go back to that two column example from before. With border-box it now works perfectly.
Each column is exactly 50% of the page width including its padding. They sit perfectly side by side the way you intended.
This is why box sizing matters. It is not just a theoretical concept. It directly affects how your layouts behave in practice.
If you ever build a layout and things are not lining up the way you expect, the first thing to check is whether
box-sizing: border-box is applied. It solves a surprisingly large number of layout mysteries.
Building the Netflix Clone
Let us add box sizing to our Netflix clone reset at the top of the CSS. This is just good practice and we want to make sure everything behaves predictably as the layout gets more complex.
Netflix
Unlimited movies, TV shows, and more.
Starts at USD 2.99. Cancel anytime.
Ready to watch? Enter your email to create or restart your membership.
* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: "Roboto", Arial, sans-serif; font-size: 16px; padding: 40px; background-image: linear-gradient(to bottom, rgba(0,0,0,0.7), rgba(0,0,0,0.4)), url("https://assets.nflxext.com/ffe/siteui/vlv3/9ba9f0e2-b246-47f4-bd1f-3e84c23a5db8/web/NP-en-20251020-TRIFECTA-perspective_774f89dc-b75d-4cfc-b1fe-52086741fae3_large.jpg"); background-size: cover; background-position: center; background-repeat: no-repeat; text-align: center; } #logo { font-family: "Bebas Neue", sans-serif; font-size: 3rem; font-weight: 700; color: #E50914; letter-spacing: 4px; line-height: 1; margin-bottom: 16px; } h1 { font-family: "Bebas Neue", sans-serif; font-size: 3rem; font-weight: 700; color: white; letter-spacing: 2px; line-height: 1.2; margin: 60px 0 16px 0; } p { font-size: 1.1rem; font-weight: 300; color: white; line-height: 1.6; margin-bottom: 12px; } button { background-color: #E50914; color: white; font-size: 1rem; font-weight: 600; text-transform: uppercase; letter-spacing: 2px; padding: 12px 32px; margin-top: 24px; border: none; border-radius: 4px; box-shadow: 0 4px 12px rgba(229, 9, 20, 0.4); } .signin { background-color: transparent; border: 2px solid white; color: white; font-size: 0.9rem; font-weight: 600; padding: 8px 20px; margin-top: 0; border-radius: 4px; box-shadow: none; } input { border: 2px solid #cccccc; padding: 12px 16px; font-size: 1rem; border-radius: 4px; box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1); }The reset at the top now includes box-sizing: border-box alongside the margin and padding reset. Every
element on the page will now size predictably. As we build more complex layouts in the coming lessons this will
quietly save us from a lot of headaches.
Have anything to say about this lesson?
Your feedback helps improve these tutorials. If something was confusing or missing, let us know.