Feb 20 2013

CSS3 Lifelike: A Slightly Different Box Model with {box-sizing:border-box}

Advertisement

The classical box model in CSS has never been the most intuitive. Thanks to CSS3, we now have an alternative to consider: {box-sizing:border-box}. This model has lots of advantages, especially when it comes to flexible layouts.

real-world-box-w550

The Classical Box Model: {box-sizing: content-box}

Think real life: If you want to know the measurements of a box in the analog world, you will always refer to the outer dimensions of this box. You will not measure such dimensions without padding and border ;-)

In the digital world, especially concerning the classical box model, things are different. Here, width only defines the width of the content area, measures for padding or border have to be added. Visually this model looks like this (in case you forgot):

boxmodell_content-box-w640-w550

Fiddling and calculations are needed to fit everything into place. In layouts with fixed pixel widths this is annoying, but achievable.

HTML Squared as Stopgap

Difficulties, real problems arise, when the measures for width, padding, border or margin are defined using different units. If done this way, the outer width of our box is not reliably calculable:

  • A layout column created with <aside class="sidebar"> is desired to receive a width of 20% and a padding, both left and right of 10px each.
  • Question: How much space do we need to reserve for the sidebar in our layout?
  • Answer: No idea. This is not reliably computable.

To not have flexible layouts become as reliable as your favorite lottery, we found a work-around, in which we double the HTML element and spread the box model properties over both elements:

  • Inside aside we put in an additional div.
  • aside is declared width:20%.
  • The inner div is equipped with optional values for padding, border or margin.

That way, the sidebar is not influenced by padding and other values. It will stay at a width of 20% no matter what and is calculable once again.

This doubling of HTML elements has become so common these days, that we almost forgot about the fact, that this is only a stopgap and intentionally thought out as that.

Lifelike Boxes with CSS3: {box-sizing: border-box}

Almost hidden from public sight, browsers took to supporting another, a second box model, with a slightly different mode of operation. In this model, padding and border are already included in the value given for width. It answers to the name of border-box, as its width gets calculated from border to border. This is how it looks:

boxmodell_border-box_kraken-w640-w550

In a web designer’s everyday life this mousy change in layouting comes in very handy. Looking back at our sidebar example we can easily save the double inner HTML element:

  • We start with defining .sidebar { box-sizing:border-box}.
  • Then we e.g. add a width:20% and some random padding or border to the element.

Finished.

Using border-box we need not care about how much padding or border an element gets, as these values are simply subtracted from the whole width and not added to it. This proves reasonable in almost all thinkable cases. Or sidebar is calculable without the need for the doubling of elements.

By the way: If you add an outer margin, this will still have to be added to the value, which makes no difference to lifelike boxes either.

The border-box in Daily Use

Once you’ve understood the whole practical value of border-box, you will anxiously ask the inevitable question of how browsers support it. This is the answer:

caniuse-com-border-box-kraken-w640-w550

Hey hey, even IE8 understands that. Unbelievable. Mozilla does need a prefix, though, but he who is able to omit IE7, could start the stylesheet with the following:

1
2
3
4
* { 
  -moz-box-sizing: border-box; 
  box-sizing: border-box; 
}

This is exactly what Paul Irish recommends in his blog post from February 2012.

Conclusion

border-box is more than just a welcome alternative to the classical box model, but before you change your coding tactics in one fell swoop to border-box, make sure to read the articles below. I predict that, for responsive layouts, border-box will become self-evident rather sooner than later.

Related Links:

As you can see, this topic is not exactly new, but has been discussed throughout the last few years. The following articles show the evolution from first attempts to a recommendation:

And now for some references:
* W3C, CSS3-ui box-sizing
* Mozilla Developer Network, box-sizing

[photo credits wooden box: 16 Miles of String via photopin cc]

Article originally written by Peter Müller for our German Dr. Web Magazin

(dpe)

About the Author

The jungle is alive: Be it a collaboration between two or more authors or an article by an author not contributing regularly. In these cases you find the Noupe Editorial Team as the ones who made it. Guest authors get their own little bio boxes below the article, so watch out for these.

Homepage

Comments and Discussions
  • John, 22 February 2013

    I like the Classical Box Model. Nice explanation.

  • Rob Sawyer, 22 February 2013

    Nice explanation of how border-box works. Have been using this on all projects for a while now and it makes life much easier!

  • Ryan, 24 February 2013

    When using the border-box property, I prefer the following code at the top of a normalize or reset stylesheet:

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

  • Get Logo, 04 April 2013

    Wow amazing tutorial…

search form
 
image description image description