CSS Box Sizing

Box sizing is an important concept in CSS that controls how the total width and height of an element are calculated. It helps you manage layouts more easily and avoid unexpected sizing issues.

What is Box Sizing

By default, CSS uses the content box model. This means that width and height only apply to the content area, and padding and border are added outside of it. This can make elements larger than expected.

Box sizing allows you to change this behavior so that padding and border are included inside the defined width and height.

Types of Box Sizing

There are two main values used in box sizing.

Content box
This is the default value. The width and height only include the content. Padding and border increase the total size of the element.

Example
If width is 200 pixels and padding is 20 pixels, the total width becomes 240 pixels.

Border box
This value includes content, padding, and border within the specified width and height. It keeps the layout more predictable.

Example
If width is 200 pixels and padding is 20 pixels, the total width remains 200 pixels.

Syntax

box sizing property is applied like this

box sizing colon value

Example in CSS

box sizing border box

Why Use Border Box

Using border box makes layout design easier and more consistent. It prevents elements from growing beyond their assigned size when padding or borders are added. This is especially useful in responsive design.

Global Best Practice

Most developers apply border box to all elements to simplify layout control.

Example

star selector
box sizing border box

This ensures every element follows the same sizing rule.

Practical Example

Without border box, two elements with the same width may appear different because of padding and borders. With border box, they stay equal in size and align properly.

When to Use Box Sizing

Use border box for most layout designs and responsive websites.
Use content box only when you specifically need the default behavior.

Summary

Box sizing controls how element dimensions are calculated.
Content box adds padding and border outside the width.
Border box includes everything inside the width.
Using border box makes layouts easier and more predictable.

Home » CSS Fundamentals (Beginner) > Box Model > Box Sizing