Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

The `box-sizing` Property

Recommended resources

Welcome to our next lesson. In our previous session, we built a solid understanding of how to create accessible HTML forms, a critical skill for gathering user input. Now, we're shifting our focus back to CSS to tackle a fundamental concept that can be a major source of confusion when you're starting out, but once mastered, makes laying out web pages dramatically simpler and more predictable.

This lesson is all about the CSS box model. You've mentioned you're already familiar with the basics of margin and padding, which is a great start. We'll build on that by exploring how browsers calculate an element's size by default, why this often leads to unexpected results, and how a single line of CSS can fix it.

By the end of this lesson, you will be able to apply box-sizing: border-box to create robust and predictable layouts, a core practice for building the polished, professional websites and app interfaces you're aiming for.

Every Element is a Box

The first principle of CSS layout is that every element on the page—from a <div> to a <p> to an <img>—is treated as a rectangular box. This box is composed of four layers:

  1. Content: The area where your text, images, or other content appears.
  2. Padding: The transparent space between the content and the border.
  3. Border: A line that goes around the padding and content.
  4. Margin: The transparent space outside the border, separating the element from other elements.

The distinction between padding and margin is crucial. Padding is inside the box, and its background color will be the same as the element's. Margin is outside the box and is always transparent.

Let's watch a short video that provides a clear recap of these components and how to inspect them using your browser's DevTools—a skill that will be indispensable for you, especially given your QA background.

The CSS Box Model, Display, and Box-Sizing

This video from TechSquidTV provides a concise overview of the box model's components and, importantly, shows you how to visualize them in Chrome DevTools.

Watch the introduction to the box model from the beginning. Pay close attention to the explanation of the difference between margin and padding. Finally, follow the excellent guide on how to inspect the box model in DevTools. Feel free to pause and try this yourself on any webpage.


The Problem with the Default Box Model

Now for the tricky part. By default, when you set an element's width and height in CSS, you are only setting the size of the content area. The padding and border are then added on top of that width, making the element's final rendered size larger than what you specified.

This is the default box-sizing: content-box behavior.

Imagine you have two boxes, each with width: 50%;. You'd expect them to sit side-by-side, perfectly filling a container. But what happens if you add padding: 20px; to them? Each box now takes up 50% of the container's width plus 40px of horizontal padding. The total width exceeds 100%, and the layout breaks, with the second box wrapping onto a new line.

This constant mental math (width + padding + border) is counter-intuitive and a common source of layout bugs.

The following image perfectly illustrates the issue. On the left, with content-box, you set width: 300px, but the padding and border are added on, making the box's total visible width 300px + (15px * 2) + (5px * 2) = 340px.

A comparison of the default `content-box` model (left) and the more intuitive `border-box` model (right). Notice how `border-box` maintains the specified 300px width even after padding and border are applied.

For a dynamic demonstration of this problem, let's turn to a video by Kevin Powell, a well-known CSS educator.

box-sizing: border-box explained

In this clip, Kevin Powell clearly demonstrates how the default content-box sizing works and why it can be frustrating.

Watch the first part of the video, from the introduction, where he adds padding and a border to an element and shows how it grows beyond its specified width.


The Solution: box-sizing: border-box

To fix this, we can change the box model calculation using the box-sizing property. By setting box-sizing: border-box;, you tell the browser to adopt a more logical model.

With border-box, the width and height you set apply to the entire visible box, including the padding and border. If you add padding or a border, the browser automatically shrinks the content area to make space for them, but the element's overall dimensions remain exactly what you defined.

Look at the right side of the "content-box vs border-box" image again. The div has width: 300px and box-sizing: border-box. The padding and border are now drawn inside that 300px space, and the content area is reduced accordingly. The element takes up exactly the space you told it to.

This makes creating layouts, especially grid-like structures with precise percentages or dimensions, infinitely easier. Let's see it in action.

box-sizing: border-box explained

This part of Kevin Powell's video demonstrates the predictable and powerful behavior of border-box.

Continue watching from this section. Notice how the padding and border are now contained within the element's specified width, preventing it from overflowing.

For a detailed textual explanation and another great interactive demo, the Piccalilli blog has an excellent article.

The box model and box sizing - Piccalilli

This article provides clear diagrams and explanations for both box models, reinforcing what you've just seen in the video.

Read the section Padding, borders and the content box for a review of the default behavior. Then, read the next section, which starts just after that paragraph, continuing to the code pen demo. Focus on how the border-box value changes the calculation. You can also play with the embedded CodePen demo to toggle the effect on and off.


A Modern Best Practice: The Universal Reset

The border-box model is so much more intuitive that it has become a standard best practice to apply it to every element on a page. This is typically done at the very top of a stylesheet with a small "CSS reset" rule.

While you could use a universal selector (*), a more robust and flexible method is to use the inherit keyword. This sets the behavior on the root <html> element and tells all other elements to inherit it.

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

This snippet ensures that every element and pseudo-element (::before, ::after) uses the border-box model, giving you a consistent and predictable foundation for all your styling. It's a piece of code you will likely include in every project you build from now on.

Let's see the reasoning behind this specific snippet.

box-sizing: border-box explained

Kevin Powell explains the different ways to apply border-box globally and why the inherit method is often preferred.

Watch the final part of the video from this segment to understand how to set up this universal rule in your projects.


Conclusion

In this lesson, we've dissected one of the most fundamental and historically confusing parts of CSS. By understanding the difference between the two box models, you've gained a crucial tool for building layouts that behave exactly as you expect. This is a significant step toward creating polished, professional digital products.

Here are the key takeaways:

  • Default is content-box: By default, an element's width applies only to the content. Padding and border are added on top, increasing its final size.
  • border-box is predictable: With box-sizing: border-box;, the width you set is the final width of the element, including padding and border. This makes layout math simple and intuitive.
  • Inspect with DevTools: Your browser's developer tools are your best friend for debugging layouts. Use the box model visualizer to see the exact dimensions, padding, border, and margin of any element.
  • Use a universal reset: It is a modern best practice to apply box-sizing: border-box; to all elements in your project using the inherit reset rule.

Now that you have control over how elements are sized, our next lesson will explore how you can control where they are placed. We'll dive into the CSS position property, learning how to take elements out of the normal document flow with values like absolute, relative, and fixed.