Quick Tip: Did Internet Explorer get the Box Model Right?
videos

Quick Tip: Did Internet Explorer get the Box Model Right?

Tutorial Details
  • Completion Time: 5 Minutes

The CSS standard states that borders and padding should be applied on top of the specified width of an element. As such, if I have a 200px div, and apply 40px worth of borders and padding, total, the width will then be 240px. This makes perfect sense; however, Internet Explorer actually did things differently. They adopted a model where the maximum width is what you specified. The borders and padding are then factored into that width, reducing the content area. As a result, the width of the element never exceeds the stated width of 200px.

As we mostly work with extremely sensitive floated layouts, where even the addition of a 1px border can break the design, I wonder: did Internet Explorer get it right?


Box Sizing

“The box-sizing CSS property is used to alter the default CSS box model used to calculate widths and heights of elements. It is possible to use this property to emulate the behavior of browsers that do not correctly support the CSS box model specification.”

This means that, if you should decide that you want to mimic Internet Explorer’s original interpretation of the box model, you can. The default value for box-sizing is “content-box.” This simply means that the width and height of an element do not include the borders and padding (or margins).

By changing this value to “border-box,” the width and height values then include the borders and padding.

#box {
 width: 200px;
 height: 200px;
 background: red;

 padding: 10px;
 border: 10px solid black;

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

Because we’ve declared box-sizing with a value of “border-box,” the final width of the #box element, styled above, will be 200px.

Especially for floated layouts, this can save you a lot of headaches! But with that said, I’m still undecided. What are your thoughts on Internet Explorer’s interpretation of the box model?

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://net.tutsplus.com Jeffrey Way

    Another alternative will be Mozilla’s upcoming “-moz-calc” – which I’m excited about. Google it if you’re not familiar with it yet. :)

    • James

      I agree! -moz-calc is an awesome addition to the browser-specific standards. The ability to calculate your CSS sizes inline is great.

  • Huckleberry

    Never thought I’d say this, but IE may have gotten the box model right. You bring up some interesting points. I think it’s easy to calculate your widths the IE way, though it’s caused me tons of headache b/c I didn’t calculate that way. While I’m tempted to switch to the IE box-model, I can only imagine the headache it would cause future designers updating the site to be aware of the change. I’ll stick with what’s been set for now, though this is interesting.
    Huck

  • http://www.enome.be Pickels

    Very cool tip. I’ve been wondering about this for a long time myself. Does anybody know a senario where it would be bad to use border box-model?

    Also you speak about IE in the tutorial but which version are we talking about?

  • http://twitter.com/Digital_Misfit Digital_Misfit

    I usually set a to the width for the entire box and then apply padding to a child or child elements. Usually this is in the form of a body without a framework. For something like 960gs, though, this could be useful. One more line of code to help make it work in other browsers. And yes, floats are very touchy.

    Nice tip.

  • Brandon

    As much as it pains me to say it, it does seem to make sense…I hate having to start calculating the width or height of what something should be because I decided I needed some padding later. When I set a width, I probably did that for a reason so I would like it respected. Thanks for the tips.

  • Stephen

    That’s a handy little tip to keep in mind. I can understand both interpretations but (I can’t believe I’m saying this) IE’s makes a little more sense to me. If I set a specific width to an element, I want it that size regardless of any other styling (hence the specifically stated width). I don’t want to be adding and subtracting borders/padding in order to get the correct width/height when the width and height properties seem to imply that it should already be happening.

  • http://www.ignitedgeek.com Joshua Gdovin

    I am also kind of mixed on my feeling about the box model. In a way, for things like floated layouts, I think it would make life much simpler. Also to be able to force mozilla and webkit to act like IE seems like it would be much easier than getting IE to play nice with the other layout with special ie style sheets.

    Very interesting, love your quick tips and screencasts Jeffrey!

  • http://fezj.net Fez

    Hmm, what versions of FF and Chrome/Safari are supported by those properties?

    Do you think it’s better to just compensate for IE via a specific style sheet for IE?
    Just for better cross browser support that is.

  • http://davidbecerra.com David B

    Adjusting the width to factor in the padding and border is a bit redundant. I’m also on the fence about this.

  • http://deweyhulsey.com Dewey Hulsey

    I think that IE’s box model definitely makes more sense for more rapid development. Changing the overall size and your borders and padding being adjusted. But for those don’t mind some quick mental math, I feel that the other model gives you more control. It is, however, a bit inconvenient that your total size isn’t defined anywhere really, and that you have to ‘remember’ that value in relation to other elements.

  • http://www.edwardmargallo.com Edward

    The way the box model hack measures witdth, border and padding makes sense, just because you should be seeing that entire div as a whole. I think IE may have gotten it right.

    That’s my 2 cents.

  • Steven

    This is great, i hate those pesky 1px borders breaking my layouts!

  • jack

    am i the only one whos waiting for the other shoe to drop with ie

  • http://wheremy.feethavebeen.com Angelo R.

    What is support like for this property? I haven’t looked into that portion of it, but I prefer the IE way of setting interpreting widths and heights. If I say it’s 10px with 5px padding, I still WANT the box to be 10px.

  • http://www.keisermedia.com Lucas

    Regardless if they are right or wrong, they are not conforming to a standard. And, yet again, we are forced to design an additional stylesheet to cater to IE. They go about change the wrong way.

  • http://www.dezigner.dk Kim Kristensen

    This is brilliant Jeffrey – i see that as a nice way to uphold your layout, so that the current browser all display correct, even if your using floating layouts. Thanks.

  • Kramer

    Great tip. I did not know about box-sizing and would hack together solutions to deal with floating layouts. This makes things much easier.

  • Alexander Trefz

    I think that it makes much more sense if an element with width 200px has a width of 200px always. for example you can add width in float layouts much more easily.

  • http://www.flickr.com/armin_san Armin

    I’ve been thinking about this for some time too, but why not include the padding as well?! It’s really annoying ahving to change 2 attributes whenever I want to increase padding/width/height.

    This would be especially helpful when styling form elements.

  • http://blog.blackfruit.de risetothesky

    Hey! This seems to be very helpful sometimes. Thank you very much!

  • http://www.webandflowdesign.com Des

    I think I like the way the non-ie others do it best. Yeah, it can make sense in a world where a 200px element is always 200px wide regardless of padding, borders, etc. But you’re still having to calculate how much room is inside the box, repositioning things, etc. As long as your CSS is put together clearly, I don’t think this is a real problem. I can define my content area to be 200px wide and know that no matter what I do to my borders, I’ve always got those 200px to work with. If I’ve got a div with a 200px image inside, I KNOW that all of that image is going to show, no matter if I have 1px or 10px borders.

    • csulok

      you forgot that width can be defined as a percentage too.

      • Mike

        ZING.

        Debate. Over. ;)

  • Jeeee

    I find that really usefull. I think I’ll use it quite a lot! Thank you Jeffrey

  • http://www.casimirfornalski.com Casimir

    This is awesome! I feel like I’ve been waiting forever for this type of functionality. Thanks a lot!

  • http://srcoley.com Stephen

    Back when I was learning the box model, I always thought that it SHOULD be like this. The width of the box as a whole shouldn’t be altered by increases the padding… The padding is contained IN the box, after all.

  • http://eichefam.net Paul

    In a community where IE is by and large shunned (for good reason, to be sure), I expect you’ll get a lot of flames for merely suggesting that IE (IE6, no less, if memory serves) got something right, while more modern and compliant browsers got it wrong.

    That said, it’s a thought-provoking topic indeed. All too often I myself have failed to correctly calculate the proper widths of child elements inside a parent width, and so on. I think I might prefer this way: no matter what kind of messed up math you may have on the inside of your styled elements, at least your page would have the outer dimensions you specified. On the other hand, it would probably allow for careless calculations. So maybe the way we’re used to IS better. This way, we’re careful about every single pixel, from margin to border to padding to width itself.

    Boy, I wishy-washy’d my way around that, huh? It’s definitely food for thought.

    • Michael

      When I first started out I hated having to calculate my width based on the padding and borders but over time I got used to it, much the way I assume people become accustomed to electro-shock therapy or drug addiction.

      I honestly always like the IE box model. I set my width exactly how I wanted and then everything else respected that. Of course this isn’t the way the spec says it should be done IE is the only one who did it and their method became one more problem for me.

      As it is now, I just do it the easy way in my IE style sheet. I’m also curious if this is the first time the “easy way” and IE have been seriously associated.

  • http://andrewburgess.ca Andrew Burgess

    That is *so* cool! I’d stopped using CSS frameworks for that very reason.

    Whether it’s the correct way or not, I remember that the IE box model was just the way I expected padding and borders to work when I started learning CSS.

    • http://techhustler.com TechHustler

      Seemes natural/intuitive, didn’t it?

      • Huckleberry

        If I recall correctly, yes it did seem more “Natural / Intuitive” — the IE6 way to be specific. However, after further thought, I think I have to agree w/ Paul, as doing it via the standard method is more specific, thus less room for error. I think the IE way would produce cleaner code though, not sloppier. But its functionality would not have been as robust.

        BTW, thanks Jeff for your great work on Tuts+. Thanks for the mind warping topic today too w/ the notion of IE6 getting it right. It’s just the right paradigm to make your rethink “Correct”. W3C standards developers please keep this on the forefront of your minds as CSS3 and HTML5 progress. Not that I’m saying to revise to IE6 Box Model or anything, but use this as the perfect example of the unforeseen.

        I’m rambling.
        Huck

      • http://www.jeffrey-way.com Jeffrey Way
        Author

        Thanks, Huck!

  • Ben

    I think it is great to have as an option. I won’t be using this on every layout I make but it would defiantly cut down on the time spent calculating widths in floating layouts. Every project is different. this method isn’t wrong just because it is ie’s default, it’s just different.

  • http://www.stevemullencreative.com Steve Mullen

    It IS annoying that you have to subtract from the dimensions of a fixed width box if you add padding, or a border. But I still hate IE and so I can’t bring myself to say that it has ever got anything right ever. :)

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      haha

  • http://matrix87.com MartinodF

    From a designer’s perspective, the border-box model is much easier. You can change padding and borders without affecting the floating or position of the box you’re working on.
    From a content perspective it’s the other way around. You can change padding and borders and be sure that your text/image/video will still fit in the box you’re working on.
    I am all for giving priority to the content, even if you have to change the width of an element a hundred times while designing a site, so I’ll go with the content-box model.

  • http://srcoley.com Stephen Coley

    I think everyone would feel a bit better if instead of saying, “IE got something right” we just say, “The other browsers got it wrong.”

    Never thought it’d be better to put emphasis on a negative aspect…

  • http://laszlokorte.de Laszlo Korte

    I think the reason for not handling this like the ie6 does is that there is some kind of inconsistence if the horizontal padding is greater than the width. If you try to set the padding in your example to 150px for example, the box will be expanded to matching with the padding option.
    In an absolute consistent solution of this situation the right padding would be left to the left padding and the other way round, so the content of the box had to be reflected by the vertical ax.

    So the modern box model is the simpler and consistent solution and and in my opinion consistency is very important when handling problems like this one.

    For the problem with adding borders in the 960-framework the css3 calc() operator should be fine.

  • http://techhustler.com TechHustler

    When I started learning web design, I was doing it the IE way without knowing it wasn’t the only way. Years later the ‘standard’ way would confuse the heck out of me and though I could see why they did it, I always thought the IE way made more sense. Who wants to conceive of a width and then have to at that time do the gymnastics of figuring out border x 2 + margin x 2?

    Too bad the entire web treats IE like a dirty leper. This seems like a little late to be giving that old model credit.

  • Tony

    When you’re packing a box and add padding does it change the the size of the box?

    IE got this one right.

  • Jarel

    I don’t think there’s a “right” way here, just a preferred way. Do you prefer that the width you set an element (usually containing content) expands with additional styling (borders, etc.) or reduces the available space for whatever’s inside the element? I prefer the former. :)

  • http://blog.wardelldesign.com Wardell

    I think box sizing was one of the things IE actually got right which is why CSS3 has the box-sizing attribute.

  • http://www.nanylew.com Charles

    I’m in that super majority who never gave it a second thought, but now seeing it, I am dumbfounded. Kind of like oh yeah, I can get with that.

  • http://swimandsing.com Jamie

    If you are building a box out of wood in real life and you want it to be 10″x10″x10″ cube. What dimension would you cut each piece of wood? In real life you need to consider the size of your material also to build the box. But what if the edges are beveled? What if there is trim on the box.

    If you are hired to build a 10″ cube and came to the client with a box that turned out to be one inch bigger because you put trim on it, you would not get paid.

    Then later I come back and paint borders on the box, does that change the size of the box? I say yes it does, even if it is by a tiny amount.

    Later I want to put something in the box that is 12″x12″x12″. Does the first box grow to allow the bigger box inside? I say no.

    I think building a box on a web page should be just like building a box in real life, but in real life there is no equivalent to padding. There would be to borders. So is a border an atribute of the div as an object or is it another object associated with the div?

    I would like the box to break if I put something to big inside the box, not other elements of the page. I can see if the object I put in the box does not fit unless the box grows to accept the borders I just added.

    Normally you don’t have a problem if the box has no padding or borders, I say it 200px wide it stays that way. I would like the box to stay that way even after I add padding and borders.

    • Jarel

      I just have to say that in the physical world of building, it entirely depends on what you want. So, per your example, a client wants a 10″ cube. That’s what the client wants. What if a different client wants a cube with an internal volume of 10″^3? Then you’d have to build the box accordingly, without worrying about what you add to the outside.

      A good example of this is the way homes are [usually] built. Most people determine the amount of space they want internally, then construct the home. Then they might add siding or other external pieces that add to it’s external dimensions but do not effect its internal dimensions.

      Anyway, just my thoughts on your comment. :)

  • Todd

    If IE got the box model right, why didn’t we adopt it!? Don’t forget that IE’s box model includes the weird behaviour of expanding to fit the size of whatever content is in it! Also, you’re assuming that padding and or margins are needed for elements. I very rarely use padding or margins on a parent container element that I want to have a fixed width. I make their children do all the padding or margins for me.

    I don’t think IE’s box model is something we should revisit. If it was a good model, it would still be in use today.

  • http://www.pixelforlife.com sKbCG

    I have always been irritated by this exact “problem”. I feel the IE method would make it so much easier on someone that is more of a designer rather than a programmer.

    It just seems to make more sense, especially for the padding.

    For a real life example:
    If I am adding packing popcorn to a box I’m about to ship, the dimensions of the box don’t stretch depending on how much “padding” i am adding. Why should it be any different for websites.

  • http://conecode.com Ryan

    I always thought that padding shouldn’t change the width of the box. I often forget to subtract those extra pixels until I refresh my browser and see my design is messed up. I’ll have to try this out some time.

    • http://www.monitojoomla.com/ jmarreros

      I agree with you, it’s logical that width includes the padding because it’s inside.

  • Matt

    Thanks for the tip. It sure has it’s uses, not everywhere, but like you’ve mentioned with floats, it certainly has its place.

    Also, on the fence.

  • http://www.djzio.be ZiO

    Thank you very much for this good tutorial.
    It helps me a lot!

  • http://mariuscroy.com Croy

    Would love if someone could point me to an alternative for Windows of xScope!

  • http://michaelrijsdijk.nl/ Michael Rijsdijk

    I can’t say that I’m not liking this… Still feels wrong to do it the IE way… But it does seem to make sense…

    Did they comply to the standard? Nope… Do I like the standard?…

    But seeing that -moz / -webkit based browsers are offering a method for this way of rendering makes me wonder if they dislike the standard way. It annoys the heck out of me when I’m sitting there calculating everything again and again because I missed a border or piece of padding which throws my layout out of whack…

    You’ve got me thinking about this Jeff!

    If you get an idea about a follow up on this don’t hesitate to ” Quick tip” it!

  • adrian

    well, this seems to work better for floated layouts, but remember that a box set to width: 100px; border: 1px solid black; in this box model would have a content width of 98px so if you’re using 960.gs and you have three divs filling the container horizontally and you put two child divs in one, then you end up with a broken layout inside the box. I’m not quite sure if that made sense in words so :

    section { box-sizing: border-box; border: 1px solid black; }

    div.container_12
    section.grid_4
    div.grid_2.alpha
    div.grid_2.omega
    section.grid_4
    section.grid_4

    Since now the width of the first “section” is 2px smaller than it needs to be for the two floated divs it contains, the layout inside the section breaks and the “section” gets taller and affects the layout below it.

    You really have to be careful with the box-sizing property it can make or break a layout. What we really need is a less fragile replacement for floats, or maybe an option to make parts of the box model be excluded from the flow of the document, like outlines and absolutely positioned elements.

  • Patrick

    I think, Microsoft did an extremely bad job on IE. There is an organisation called world wide web consortium. This consortium has build a huge web standards list. EVERY Browser EXEPT IE uses this standards. Its not important, if the box border model is the best or the w3c box model. The best is, when all browsers are using 1000% EXACTLY the same standards. And this isn’t impossible.

    Let’s talk straight: IE is bullshit. This is an irrevocable fact.

    • Matt

      You’re new to web development/design aren’t you?

    • http://net.tutsplus.com Jeffrey Way

      Actually, when IE6 first launched, it was the most advanced browser ever released.

  • http://www.jc-designs.net/blog Jeremy Carlson

    I wouldn’t say that either of the methods is right. They are just different. The one thing I actually like IE for is that it breaks when it should. FF will compensate. I hate that. You put an image that is larger than the div in FF, it won’t break the layout, but it will destroy IE…AS IT SHOULD! If I forget to the end carrot on an element, I want the damn thing to bust, not assume that I meant to put it in there.

    That being said, I for sure am NOT going to change the way a browser behaves to mimic IE, just because I might prefer one method over the other.

  • Ian

    I think they did. I just ran into this problem a couple days ago. Added a few pixels of padding and things broke.

  • http://www.claytonfelt.com Clayton

    I see that IE8 offers the -ms-box-sizing: border-box;
    That makes IE8 work like IE6 and IE7.
    Opera uses box-sizing:border-box;

    So it looks like you need all 4 to make all browsers observe the IE6 IE7 box model.

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

    Interesting. I will have to try this on a typical wordpress theme. It might be a good way to insure they all look the same.

    • http://www.normansblog.de Norman

      you do know that IE6 and IE7 are kind of standard compliant when it comes to w3c box model whenever they are not in quirksmode, right?
      So, as long as theres a valid Doctype, both, IE6 & 7 calculate the width of any element correctly.

      There are moments when the the new box-sizing method comes in handy, for instance you have a width of 100% but you also want to have a certain amount of padding to that element. Another example: form elements (inputs, buttons etc.)

      The article would have been more useful if it had described those situations instead of animating people to use the quirky box model of IE6..

      • http://www.jeffrey-way.com Jeffrey Way
        Author

        The article and video weren’t intended to “animate” anybody. It merely asked, is that model smarter?

  • http://www.go-trex.nl orves

    Hi Jeffrey, Im also a user of the 960grid and this quick tip is awesome to clean the source code with the extra children div I use for padding inside a grid element.

    Makes me think about the right box model case moz|webkit or IEs way.