CSS box‑sizing: Controlling the Element Box Model

Hero image for CSS box‑sizing: Controlling the Element Box Model. Image by Klára Vernarcová.
Hero image for 'CSS box‑sizing: Controlling the Element Box Model.' Image by Klára Vernarcová.

As I work with a new boot camp junior developer who's joined our team, I'm gradually going back over CSS basics with them. So, it makes sense I think to write some of this down, even if it is relatively common knowledge for any seasoned frontend developer.

In CSS, boxsizing property is a simple tool that every developer should understand. It allows us to control how an element's dimensions are calculated (when defining these via width or height for example), and it can save us from lots of layout headaches and unexpected renderings in our interfaces.


What Is boxsizing?

Starting at the very beginning... In CSS, every element is represented within the document flow as a rectangular box, which is defined by the box model. The box model consists of four areas:

  • Content

    : The actual content inside the box (e.g., text, images).
  • Padding

    : The space between the content and the border.
  • Border

    : The border surrounding the padding.
  • Margin

    : The space outside the border that separates the element from others.

It looks a little like this:

A diagram of the CSS Box Model showing how content is surrounded by padding, then border, and then margin.

The boxsizing property determines whether the padding and border are included when rendering an element's width and height.


The Default Box Model

By default, most browsers use the contentbox value for boxsizing. This means the specified width and height of an element apply only to the content area, so padding and borders fall outside (and are rendered in addition to, the defined width and height).

Take this div, for example:

div {  width: 200px;  height: 100px;  padding: 20px;  border: 10px solid #336699;}

Here's how the total size is calculated:

  • Content area

    : 200px (width) x 100px (height)
  • Padding

    : 20px on each side
  • Border

    : 10px on each side
  • Total size

    :
    • Width

      : 200px + 20px + 20px + 10px + 10px = 260px
    • Heigh

      t: 100px + 20px + 20px + 10px + 10px = 160px

So, despite the width being defined as 200px in the CSS, it will actually take up 260px of real estate in the document flow. Likewise, the height is defined as 100px but will actually take up 160px. This default behaviour can lead to unexpected layout issues, especially if you are relatively new to frontend development or you're working with fixedwidth or responsive layouts.

This is made doubly confusing when some browsers render the box model differently by default.


boxsizing Values

There are two main values in CSS for boxsizing:

1. contentbox

This is the default. With boxsizing: contentbox, the width and height apply only to the content area of the element and exclude padding and border values.

div {  box-sizing: content-box;  width: 200px;  padding: 20px;  border: 10px solid #336699;}

As we discussed above, the total size of this div will be 260px x 160px.

2. borderbox

With boxsizing: borderbox, the width and height values include padding and border. This makes the element's overall size match the specified width and height.

div {  box-sizing: border-box;  width: 200px;  padding: 20px;  border: 10px solid #336699;}

Total size: 200px x 100px (padding and border are included in the width and height values, so the content inside it is smaller).


Why Use boxsizing: borderbox?

The borderbox value is now a bestpractice in modern web development and ships with moreorless every CSS Reset available. The reasons behind this are simply because it:

  1. Simplifies Layouts

    : Total size is predictable and easier to calculate.
  2. Improves Consistency

    : Ensures elements align correctly in complex layouts.
  3. Enhances Responsiveness

    : Works seamlessly with percentagebased dimensions.

To apply borderbox universally, many developers include this snippet at the start of their CSS:

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

This then ensures that all elements use the borderbox model, including their pseudoelements.


Wrapping up

The boxsizing property may seem fairly basic in the grand scheme of frontend development, but it plays a vital role in creating maintainable and predictable layouts. By switching to always using borderbox, we can streamline our interface development and avoid the common pitfalls of the box model.

Key Takeaways

  • boxsizing determines whether padding and border are included in an element's width and height values.
  • Use boxsizing: borderbox for consistent and predictable layouts.
  • A universal borderbox rule is a common best practice in modern CSS.

Mastering boxsizing is essential for building responsive and visually balanced designs, helping us avoid unnecessary headaches in our CSS layouts.


Categories:

  1. CSS
  2. Development
  3. Front‑End Development
  4. Guides