Mudzinga

Confused by layouts that break when you add padding or borders? Learn box-sizing step-by-step, see clear code examples, and fix your layouts with ease. Read more!

5 Minute Read

Box Sizing: Border-Box vs Content-Box

Box Sizing: Border-Box vs Content-Box

If you’ve ever tried to style a box in a web page and thought, “Why is this wider than I asked for?”, you’re not alone. The secret often comes down to one small CSS property: box-sizing.

In this article, you’ll learn the difference between content-box and border-box, how they affect your layouts, and how to use them step-by-step. We’ll walk through short, practical code examples you can try right away.


First, what is a “box” in CSS?

Every element on a web page (like a <div>, a button, or a paragraph) is treated as a box. This box is made up of four parts:

  1. Content – The actual stuff inside (text, images, etc.)
  2. Padding – Space between the content and the border
  3. Border – A line around the padding and content
  4. Margin – Space outside the border, separating this box from others

box-sizing controls how the width and height of that box are calculated.


The two main box-sizing values

There are two common values you’ll see:

  • content-box (the default in CSS)
  • border-box (the one many developers prefer for layouts)

1. content-box (default behavior)

With content-box, the width and height you set apply only to the content. Padding and border are added on top of that width and height.

So if you say:

  • width: 200px;
  • padding: 20px;
  • border: 10px solid;

The total width becomes: 200 (content) + 40 (left + right padding) + 20 (left + right border) = 260px.

2. border-box

With border-box, the width and height include content + padding + border. That means the total width stays exactly what you set.

Using the same numbers:

  • width: 200px;
  • padding: 20px;
  • border: 10px solid;

The total width is still 200px. The content area shrinks a bit to make room for padding and border inside that 200px.

This is why border-box is often much easier for beginners.


Example 1: See content-box in action

Let’s start with a simple page using the default content-box behavior.

Copy this into an index.html file and open it in your browser:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Box Sizing: content-box</title>
  <style>
    /* No box-sizing set, so it uses the default: content-box */
    .box {
      width: 200px;              /* Content width only */
      padding: 20px;             /* Adds to total width */
      border: 5px solid #333;    /* Also adds to total width */
      background: lightblue;
      margin-bottom: 20px;
    }
  </style>
</head>
<body>
  <h1>content-box example</h1>
  <div class="box">This is a box using content-box.</div>
</body>
</html>

What’s happening here?

  • We create a .box with width: 200px.
  • We add padding: 20px and border: 5px.

Total width calculation:

  • Content: 200px
  • Padding: 20px left + 20px right = 40px
  • Border: 5px left + 5px right = 10px
  • Total width = 200 + 40 + 10 = 250px

The box will be wider than 200px, even though you asked for 200.

Try it yourself

In the same file, change padding: 20px; to padding: 40px; and refresh the page.

  • Notice how the box gets even wider.
  • With content-box, changing padding or border changes the total size of the box.

Example 2: Switch to border-box

Now let’s see what happens when we use box-sizing: border-box.

Update the CSS in the same file to this:

<style>
  .box {
    box-sizing: border-box;      /* Key line: use border-box */
    width: 200px;                /* Total width, including padding + border */
    padding: 20px;               /* Stays inside the 200px width */
    border: 5px solid #333;      /* Also stays inside 200px */
    background: lightgreen;
    margin-bottom: 20px;
  }
</style>

What’s happening now?

  • box-sizing: border-box; tells the browser: “Width includes everything except margin.”
  • The total width of .box is exactly 200px, no matter what padding or border you add.

Try changing padding: 20px; to padding: 40px; again.

  • With border-box, the outside width stays 200px.
  • The padding increases, and the content area shrinks to fit inside.

This makes layout work much more predictable, especially when you want elements to fit in a row or match a grid.


Example 3: Comparing side-by-side

Let’s compare content-box and border-box next to each other.

Replace your previous CSS and HTML body with this:

<style>
  .wrapper {
    display: flex;           /* Put boxes side-by-side */
    gap: 20px;               /* Space between boxes */
  }

  .content-box-example,
  .border-box-example {
    width: 200px;            /* Same width value for both */
    padding: 20px;
    border: 5px solid #333;
    background: #f0f0f0;
  }

  .content-box-example {
    box-sizing: content-box; /* Explicitly set content-box */
    background: lightcoral;
  }

  .border-box-example {
    box-sizing: border-box;  /* Use border-box */
    background: lightseagreen;
  }
</style>

<body>
  <h1>content-box vs border-box</h1>
  <div class="wrapper">
    <div class="content-box-example">
      content-box (total width > 200px)
    </div>
    <div class="border-box-example">
      border-box (total width = 200px)
    </div>
  </div>
</body>

What you should see

Open or refresh your page in the browser:

  • The content-box box (red) will look wider.
  • The border-box box (green) will be narrower, even though both have width: 200px.

This visual comparison makes it clear: with content-box, padding and border are extra. With border-box, they are included.

Try it yourself

Play with these values in the code:

  • Change padding: 20px; to padding: 40px; for both boxes.
  • Change border: 5px solid #333; to border: 15px solid #333;.

Watch how the red box’s outer size changes more than the green box’s. This is why border-box is easier for consistent layouts.


Example 4: A common pattern – set border-box for everything

Most modern websites use border-box for all elements. This makes layout calculations simpler and more beginner-friendly.

You can do this with a small CSS “reset” at the top of your stylesheet:

/* Apply border-box to all elements and their pseudo-elements */
*, *::before, *::after {
  box-sizing: border-box;
}

Add this inside your <style> tag or main CSS file, before other styles.

Why this helps

  • Every element uses border-box by default.
  • When you say width: 300px;, you know that’s the total width.
  • You don’t have to keep re-calculating padding and border.

This tiny change can save you a lot of confusion and frustration as you build more complex layouts.


When should you use each one?

For most beginners (and even many pros):

  • Use border-box for almost everything. It’s easier to reason about and keeps layouts stable.
  • You might use content-box if you have a special case where the content area must be a fixed size, and you don’t mind the total box growing.

If you’re just starting out, it’s perfectly fine to set border-box globally and forget about content-box until you’re more advanced.


Try this mini practice task

  1. Create a new HTML file.
  2. Add three boxes in a row using display: flex;.
  3. Give each box:
    • width: 150px;
    • padding: 20px;
    • border: 5px solid;
  4. First, use content-box and see how they fit.
  5. Then switch to border-box by adding:
*, *::before, *::after {
  box-sizing: border-box;
}

Notice how the layout becomes more predictable when using border-box.

Remember: every time you experiment, you’re building real skills. It’s okay if it feels confusing at first—that’s part of the learning process.


Quick recap: key takeaways

  • Every element in CSS is a box made of content, padding, border, and margin.
  • box-sizing controls how width and height are calculated.
  • content-box (default): width/height apply to content only; padding and border are added on top.
  • border-box: width/height include content + padding + border; total size stays what you set.
  • Using border-box for all elements makes layouts simpler and more beginner-friendly.
  • You can set it globally with:
*, *::before, *::after {
  box-sizing: border-box;
}

As you keep practicing, try building small layouts—cards, buttons, or simple grids—and pay attention to how box-sizing affects them. Every small project you finish is a win, and you’ll be surprised how quickly this starts to feel natural.

About Percy Mudzinga

This article was automatically generated by an AI-powered blog system built by Percy.
Percy Mudzinga is a Senior Full-Stack Software Engineer based in Harare, Zimbabwe, with nearly a decade of experience building enterprise web and mobile applications. He specializes in React, Vue.js, Flutter, and Node.js.

Never Miss an Update

Subscribe to our newsletter and get the latest articles delivered directly to your inbox every week.

No spam, unsubscribe anytime. We respect your privacy.

© 2025 Mudzinga. All rights reserved.