Leaderboard
Css Jul 30, 2026
Css Box Sizing Content Box Total Width Calculation

CSS Box Model — What is the Rendered Width?

This is a daily Css challenge from the CodeShot archive. Practice your knowledge of Box Sizing Content Box Total Width Calculation and improve your technical interview readiness.

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid black;
  box-sizing: content-box;
}
A 200px
B 250px
C 245px
D 270px

Detailed Explanation

Why This Question Matters

If you've ever spent an hour wondering why your "100% width" sidebar is pushing your main content off the screen, you've fought with the CSS Box Model. It's one of those things that seems intuitive until you actually start adding padding and borders, and suddenly your layout breaks for no apparent reason.

The confusion usually stems from how browsers calculate the actual space an element occupies. There isn't just one "width"; there's the width you define in CSS and the width the browser actually renders on the screen. Understanding the difference is the gap between "guessing and checking" your CSS and actually knowing how to build a stable layout.

Understanding the Code

Let's look at the snippet:

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

At first glance, you see width: 200px and assume that's the answer. But the magic (or the headache) is in the box-sizing property.

In this example, we're using box-sizing: content-box. This is the default setting for almost every browser. When you use content-box, the width property applies only to the content area.

Think of it like a picture frame. The width is the size of the photo itself. The padding is the matting around the photo, and the border is the wooden frame. If you want to know how much wall space the whole thing takes up, you can't just look at the photo size; you have to add the matting and the frame to the total.

Finding the Correct Answer

To get the actual rendered width, we have to do some basic math. Since we are using content-box, the browser calculates the total horizontal space like this:

Total Width = Left Border + Left Padding + Content Width + Right Padding + Right Border

Plugging in our numbers:
- Content Width: 200px
- Padding: 20px (left) + 20px (right) = 40px
- Border: 5px (left) + 5px (right) = 10px

200 + 40 + 10 = 250px

The correct answer is 250px.

If the answer were 200px, we would have needed box-sizing: border-box. In that case, the browser would have "shrunk" the content area to 150px to make room for the padding and borders, keeping the total outer width at exactly 200px.

Common Mistakes Developers Make

The most common mistake is forgetting that padding and borders apply to both sides. A lot of juniors see padding: 20px and just add 20 to the width once. Remember: if you have padding on the left, you almost always have it on the right too.

Another trip-up is assuming that width: 100% behaves predictably. If you set an element to width: 100% and then add padding: 20px with content-box, your element will be 100% + 40px wide. This is the primary cause of those annoying horizontal scrollbars on mobile views.

Lastly, people often forget that box-sizing is inherited (or rather, not inherited by default, but usually set globally). If you change it for one element, it doesn't change for its children unless you've set up a global reset.

Real-World Usage

In modern professional development, you will rarely see content-box used for layout elements. It's simply too unpredictable.

Most engineering teams start their projects with a CSS reset that looks something like this:

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

By forcing everything to border-box, we make the CSS behave the way most humans actually think. If I tell a developer a button needs to be 200px wide, they expect the *entire button* to be 200px, not the text inside it.

However, content-box is still useful in specific edge cases, like when you're building a very specific graphic element where the internal content size must remain absolute regardless of the border thickness.

Key Takeaways

- content-box (Default): Width = Content only. Padding and borders are added on top.
- border-box (Standard for Pros): Width = Content + Padding + Border. The content area shrinks to fit.
- Always account for both sides of the element when calculating total width.
- Use a global border-box reset to save yourself from layout headaches.

Why this matters

Understanding Box Sizing Content Box Total Width Calculation is crucial for passing technical interviews. In real-world applications, this concept often leads to subtle bugs if not handled correctly. For more details, you can always refer to the official MDN Documentation.

📝
Reviewed by CodeShot Editorial
Every challenge is code-reviewed by senior developers to ensure accuracy and real-world relevance. Learn more.

Ready for your shot?

Join thousands of developers solving one logic puzzle every morning.

Solve Today's Challenge →