Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Have you ever tried placing two boxes side-by-side on a webpage, each with width: 50%, and then were shocked to see the second box drop onto a new line as soon as you added padding: 10px? 🤯
Why does adding padding make a box wider than the width you explicitly set in CSS?
This is because of how CSS calculates box dimensions by default. The solution to this frustrating mystery is a single, magical property: box-sizing: border-box;.
Let us learn why box-sizing is considered the most important layout property in modern CSS!
Imagine you are packing a travel suitcase for a school trip to Shimla:
+-------------------------------------------------------------------------+ | THE TWO WAYS TO MEASURE A BOX | +-------------------------------------------------------------------------+ | 1. content-box (The Confusing Airline): | | • You buy a suitcase labeled "50 cm". | | • The airline says: "50 cm is ONLY for your clothes! | | Now add 10 cm for the foam padding and 4 cm for the wheels! | | Your suitcase is now 64 cm and cannot fit in the airplane cabin!" | | | | 2. border-box (The Sensible Modern Airline): | | • You buy a suitcase labeled "50 cm". | | • The airline says: "50 cm is the TOTAL outer size! | | Your clothes, padding, and wheels all fit INSIDE that 50 cm limit."| +-------------------------------------------------------------------------+
content-box (Browser Default)Historically, web browsers default to box-sizing: content-box.
In content-box, the width property only applies to the content area. Any padding and border you add are added on top of the width!
What is the actual width of this card on your screen?
300px20px + 20px = 40px5px + 5px = 10pxEven though you wrote width: 300px;, your card is actually 350px wide! If you had a 300px container, this card would overflow and break your layout.
box-sizing: border-boxWhen you switch to box-sizing: border-box, the width you specify is the FINAL total outer width of the box (including content, padding, and borders)!
border-box:What is the actual width on your screen now?
250px (300 - 40 - 10) so the total box stays exactly 300px!content-box vs border-boxImagine you want two equal columns side by side:
content-box: Each column is 50% + 40px. Together, they equal 100% + 80px. Because the sum exceeds 100%, the second column drops to the next line!border-box: Each column remains strictly 50%. Together, they equal exactly 100%. Both columns sit perfectly side-by-side without overflowing!Because border-box makes layout math predictable and intuitive, almost all professional developers, along with frameworks like Bootstrap and Tailwind CSS, put this universal reset at the very top of their stylesheets:
It forces every single element, icon, and pseudo-element on your website to use border-box. Once this rule is in place, you will never have to worry about padding pushing boxes out of alignment again!
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
Doing manual subtraction math: writing width: 260px because you want 20px padding. | Use box-sizing: border-box; width: 300px; padding: 20px;. | Let the browser calculate inner spacing automatically. |
Forgetting to apply border-box to pseudo-elements (*::before, *::after). | Use *, *::before, *::after { box-sizing: border-box; }. | Ensures decorative icons and badges also calculate dimensions correctly. |
Applying border-box only to a single card instead of globally. | Place the box-sizing reset at the very top of your global CSS file. | Consistent box-sizing across all components prevents layout glitches. |
content-box, which adds padding and borders on top of the specified width.content-box, Total Width = width + padding + border.border-box, Total Width = width. Padding and borders are absorbed inside the box.border-box makes responsive multi-column layouts (like 50% + 50%) reliable and overflow-free.*, *::before, *::after { box-sizing: border-box; } at the top of every web project.Test your understanding with these multiple-choice questions:
box-sizing property?<div> has box-sizing: content-box, width: 200px, padding: 15px, and border: 2px solid black, what is its total rendered width on the screen?<div> has box-sizing: border-box; width: 200px; padding: 15px; border: 2px solid black;, what is its total rendered width?width: 50% and padding: 10px drop onto separate lines under default content-box?*, *::before, *::after { box-sizing: border-box; } at the top of a stylesheet?box-sizing-demo.html.content-box and border-box with your own eyes:content-box pushes past the dashed container edge, while the green border-box aligns with millimeter precision! 🎯In the next lesson, we will continue your web styling journey with Font Families and Web-Safe Fonts (Text and Fonts).
👉 Continue to Next Lesson: Font Families and Web-Safe Fonts →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Width, Height, Max-Width, and Min-Width | Font Families and Web-Safe Fonts |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.