CSS Box Sizing: The Layout Fix

Stop fighting with widths, padding, and borders. Learn how one CSS property, `box-sizing`, makes layouts predictable, intuitive, and robust.

Lesson ProgressStep 1 of 8
Content
0 EXP

Welcome! Every HTML element is a rectangular box. But how its size is calculated can be... tricky.

.box {
  /* This is the box we are styling */
}

The Default: `content-box`

By default, all HTML elements use `box-sizing: content-box;`. This means the `width` and `height` properties you set apply **only** to the content area of the element.

The element's total on-screen width is calculated by adding the `width`, horizontal `padding`, and horizontal `border` together.

/* This element is 250px wide! */
.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
}
/* Total Width = 200px (width) + 40px (padding) + 10px (border) */

This behavior is why layouts often "break" when you add padding to elements that have a percentage-based width (like `width: 50%;`).

System Check

A box has 'width: 100px', 'padding: 10px', and 'border: 2px solid'. With 'content-box', what is its total rendered width?

Advanced Holo-Simulations

0 EXP

Apply your new knowledge to fix broken layouts and earn achievements.


Achievements

📐
Box Model Analyst

Calculate the total width of a 'content-box' element.

📦
Border-Box Advocate

Apply 'border-box' to fix a common layout problem.

🌍
Universal Reset Pro

Demonstrate the use of the universal 'box-sizing' reset.

Mission: Fix the Broken Layout

We have two `.column` elements that should sit side-by-side (each 50% width). But their `padding` is making them wrap. Add one CSS property to the `.column` rule to fix the layout.

A.D.A. Feedback:

> System integrity looks stable. Layout fixed!

Challenge: Assemble the Universal Reset

Drag the pieces into the correct order to create the universal `border-box` reset rule.

box-sizing: border-box;
*
}
{

Challenge: Complete the Rule

Fill in the missing property and value to make the element's total width exactly 200px.

.box {width: 200px;padding: 25px;border: 5px solid red;
:;
}

Consult A.D.A.

Community Holo-Net

Beyond the Box: A Deep Dive into CSS Box Sizing

In CSS, every element is a rectangular box. This concept is the foundation of all layout, known as the **Box Model**. This model is composed of four parts, from the inside out: the **content**, **padding**, **border**, and **margin**.

For decades, CSS had a quirk that baffled beginners and frustrated experts: when you set an element's `width`, you were only setting the width of the *content* area. This meant your element's *actual* on-screen width was that `width`... **plus** its padding, **plus** its border. This is the default behavior, called `content-box`.

1. The Default: `content-box` (The Confusing Way)

With `box-sizing: content-box;` (the default), the calculation for an element's total width is:

Total Width = width + padding-left + padding-right + border-left + border-right
Total Height = height + padding-top + padding-bottom + border-top + border-bottom

This is why creating two columns with `width: 50%` would fail the moment you added any `padding`. 50% (content) + 20px (padding) is *more* than 50%, causing the layout to break.

2. The Solution: `border-box` (The Intuitive Way)

The `box-sizing: border-box;` property was introduced to fix this. It changes the box model calculation entirely. When you set an element's `width`, you are setting the final, visible width **including** padding and borders.

Total Width = width
Total Height = height

With this model, the *content area* automatically shrinks to make room for the `padding` and `border` you specify. Now, you can set two columns to `width: 50%`, add any padding you want, and they will always fit perfectly side-by-side.

❌ `content-box` (Default)

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid;
}

Actual Width: 250px
(200 + 20 + 20 + 5 + 5)

✔️ `border-box` (Recommended)

.box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid;
}

Actual Width: 200px
(Content area becomes 150px)

3. The Universal Reset: A Modern Best Practice

Because `border-box` is so much more intuitive, it has become a standard best practice to apply it to *every* element on the page. This is done with a "universal reset" at the very top of your CSS file.

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

This approach is slightly more robust than just `* box-sizing: border-box;`. It sets the default on the `html` element and then tells every other element (including pseudo-elements) to `inherit` that behavior. This makes it easier to override `box-sizing` on a specific component if you ever (rarely) need to.

4. The Other Values: `padding-box` and `margin`

  • `padding-box`: This is a rarely used value. It tells the browser that the `width` should include padding, but **not** the border. Its use cases are very niche, and it was once only supported by Firefox. It's best to avoid it unless you have a specific reason.
  • What about `margin`?: It is crucial to remember that `margin` is never included in any `box-sizing` calculation. Margins are always *outside* the box and are used to create space *between* elements.
Key Takeaway: Use `box-sizing: border-box;` on all your elements. It simplifies layout math, prevents common bugs, and makes your CSS more maintainable. It's the sizing model that "just works" the way you expect it to.

Glosario de CSS Box Sizing

Box Model (Modelo de Cajas)
El concepto fundamental de CSS que trata a cada elemento HTML como una caja rectangular. Esta caja se compone de cuatro partes: contenido, padding (relleno), border (borde) y margin (margen).
box-sizing
La propiedad de CSS que define cómo se calculan el ancho (`width`) y alto (`height`) totales de un elemento, es decir, qué partes del modelo de caja se incluyen en esas dimensiones.
content-box
Valor por defecto de `box-sizing`. El `width` y `height` especificados se aplican solo al área de contenido. El `padding` y el `border` se añaden *por fuera* de esas dimensiones, aumentando el tamaño total del elemento.
border-box
El valor recomendado para `box-sizing`. El `width` y `height` especificados definen el tamaño total de la caja, incluyendo el `padding` y el `border`. El área de contenido se encoge automáticamente para hacerles espacio.
Content (Contenido)
La parte más interna de la caja, donde se muestra el texto, las imágenes u otro contenido.
Padding (Relleno)
El espacio transparente que rodea al contenido, situado dentro del borde.
Border (Borde)
La línea que se dibuja alrededor del contenido y el padding. Tiene su propio grosor y estilo.
Margin (Margen)
El espacio transparente que rodea al borde, situado fuera de la caja. Se usa para crear separación entre elementos. Nunca se incluye en el cálculo de `box-sizing`.
Universal Reset (Reseteo Universal)
Una práctica común en CSS que consiste en aplicar `box-sizing: border-box;` a todos los elementos de la página (usando el selector `*`) para asegurar un comportamiento de layout predecible.
* (Selector Universal)
Un selector de CSS que selecciona absolutamente todos los elementos en el documento HTML.
::before & ::after (Pseudo-elementos)
Pseudo-elementos de CSS que permiten insertar contenido (o estilos) antes o después del contenido de un elemento. A menudo se incluyen en el reseteo universal (`*, *::before, *::after`).
inherit
Un valor de CSS que instruye a una propiedad para que adopte el mismo valor que tiene en su elemento padre. Se usa en el reseteo universal más robusto (`box-sizing: inherit;`).
padding-box
Un valor de `box-sizing` muy poco común. El `width` y `height` incluyen el contenido y el `padding`, pero no el `border`. Su uso no es recomendado.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust, maintainable, and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications (CSS Box Model Module Level 3) and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!