CSS3 box-sizing attribute

CSS3 is going to include a new attribute called box-sizing so we can choose whether the width set on an element will include borders and padding or whether borders and paddings will be added to the width.

The difference is basically the difference between the IE5 and W3C box models. The default will still be the W3C way we’re all used to where the padding and borders will be added to the width, it’ll just be possible to opt into the other way when it makes things easier.

How it works

Set the box-sizing style to content-box to get the traditional W3C box model. In this case the total width of the element will be the width set on the element plus the width of the borders and padding. If you had an element with a width of 30 pixels and a border of one pixel, the total width of the element would be 32 pixels:

  1. #div1{ box-sizing: content-box;}

It can also be set to border-box to have borders and paddings included in the width. If you had an element with a width of 30 pixels and a border of one pixel the total width of the element would still be 30 pixels:

  1. #div2{ box-sizing: border-box; }

Here’s how it would look in the browser. The top element has box-sizing set to content-box and the bottom has it set to border-box:

content-box fits inside the parent container while border-box overlaps
See code

Why’s this useful?

I’ve always found the way the browser works out dimensions (the CSS box model) by adding the borders to the width set on the box a little frustrating when working with percentage widths.

Imagine you’re creating a two column layout. The left column should be 30% and the right 70%:

  1. .left{
  2.     float: left;
  3.     width: 30%;
  4.     background-color: pink;
  5.     border: 3px red dotted;
  6.     height: 150px;
  7. }
  8. .right{
  9.     float: left;
  10.     background-color: lightgreen;
  11.     width: 70%;
  12.     border: 3px green dotted;
  13.     height: 150px;
  14. }
  1. <div class="left">content-box</div>
  2. <div class="right">box-sizing</div>

If you add a border to either box, the total with of the elements will be greater than 100% so the second box will be pushed onto the next line:

But if we switch the box-sizing mode to border-box, we can set a border and the box will still be exactly the width that we want:

When’s it coming?

When IE8 comes out this will be supported by all four of the big browsers.

Safari – supported with -webkit-box-sizing since Safari 3.

Opera – supported in the latest version of Opera (9.5)

Firefox – supported with the -moz-box-sizing since Firefox 1.1.

Internet Explorer – supported with -ms-box-sizing in the IE8 beta 1 and without the prefix in IE8 beta 2.

More information

Posted on 20 Oct 08 by Helen Emerson (last updated on 20 Mar 13).
Filed under CSS

Comments

James Hopkins 27 Oct 2008

Helen,

As of IE8 Beta 2, the property can be used without the vendor-specific prefix (http://msdn.microsoft.com/en-gb/library/cc304082(VS.85).aspx)

Lensco 27 Oct 2008

Good, comprehensive article. I have always found the box model awkward, having to recalculate widths whenever I edit the padding, margin or border. Looking forward to the days I can leave those troubles behind for good.

Helen 27 Oct 2008

Thanks for the IE8 beta 2 tip. I’ve updated the article. :)

Tom 28 Oct 2008

I hope Safari and Firefox will leave soon the vendor-specific prefix: we have web standards, why don’t use them?

Jason 29 Oct 2008

YAY! I agree Helen the W3C way is a bit, and sometimes HUGELY frustrating when working with percentage boxes. This is finally something so small but will make a huge impact on the way we do things.

James Hopkins 05 Nov 2008

@Tom: Apple won’t drop the the prefix because doing so “actually broke real-world Web sites” (Dave Hyatt – http://idreamincode.co.uk/browsers/yet-more-innovation-from-the-webkit-guys#comment-44).

There is a ticket (https://bugzilla.mozilla.org/show_bug.cgi?id=243412) in Mozilla’s Bugzilla relating to this issue. Judging by the timestamp of the last comment made, it could be a bit of time before the ‘-moz-’ prefix is removed.

shereen 27 Dec 2010

i read articles about this attribute put never understand it put know i understand it now thank you very much

Evan 14 Jan 2011

You might need to know that, even with the latest versions of Chrome, I had to use proprietary CSS to get this working consistently:

box-sizing: border-box;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;

Edd Turtle 23 Jul 2012

I think we have differing views on which one is the traditional box model, when I wrote my blog post about it (http://wp.me/p1V1Ia-f2) I called the border-box, IE5 version the traditional one – But great post nonetheless!

Trackbacks