Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

CSS Box Sizing — border-box vs content-box

content-box vs border-box

The box-sizing property changes how the browser calculates an element's final size. This is one of the most useful CSS properties for predictable layouts because it decides whether padding and border are added on top of the width, or included inside it.

ValueHow Width/Height WorkResult
content-boxWidth applies only to contentPadding and border make the element bigger
border-boxWidth includes content, padding, and borderSize stays easier to control

Why border-box Is Popular

Most modern projects switch every element to border-box because it makes layouts simpler. A tl-card with width: 300px actually stays 300 pixels wide even if you add padding and borders.

Comparing the two sizing models
.card-a {
    box-sizing: content-box;
    width: 300px;
    padding: 20px;
    border: 5px solid #2563eb;
}

/* Final rendered width: 350px */

.card-b {
    box-sizing: border-box;
    width: 300px;
    padding: 20px;
    border: 5px solid #2563eb;
}

/* Final rendered width: still 300px */

Global Reset Pattern

A very common pattern is to apply box-sizing: border-box to every element. This reduces layout surprises and keeps spacing easier to reason about.

Recommended project-wide setup
html {
    box-sizing: border-box;
}

*,
*::before,
*::after {
    box-sizing: inherit;
}

When to Use Each Value

  • Use border-box for nearly all layout containers, cards, grids, and forms.
  • Use content-box only when you specifically want padding and borders to add extra size.
  • Keep your entire project consistent to avoid confusing mixed behavior.

FAQ

Frequently Asked Questions

Key Takeaways
  • box-sizing changes how width and height are calculated.
  • content-box excludes padding and border from the declared width.
  • border-box includes padding and border inside the declared width.
  • Most modern projects use a global border-box reset for predictable layouts.
  • Understanding box-sizing helps prevent overflow and layout bugs.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.