Skip Navigation

The Box Model

The term "box model" is often used by people when talking about CSS-based layouts and design. Not everyone understands what is meant by this though, and not everyone understands why it is so important.

Any HTML element can be considered a box, and so the box model applies to all HTML (and XHTML) elements.

The box model is the specification that defines how a box and its attributes relate to each other. In its simplest form, the box model tells browsers that a box defined as having width 100 pixels and height 50 pixels should be drawn 100 pixels wide and 50 pixels tall.

There is more you can add to a box, though, like padding, margins, borders, etc. This image should help explain what I'm about to run through:

Outline of box model

As you can see, a box is made up of four distinct parts. The outside one, the margin, is completely invisible. It has no background color, and will not obstruct elements behind it. The margin is outside the second part, which is the border. The border outlines the visible portion of the element. Inside the border is the third part of the box, the padding, and then inside that the content area of the box. The padding defines the space between the content area of the box and the border.

(Note that in the image above, the only border of the three drawn that would actually be visible is the solid line - the dashed lines are added to help demonstrate the box model).

When you define a width and a height for your box using CSS, you are defining not the entire area taken up by the content, padding, border and margin. You are actually just defining the content area itself - the bit right in the middle. The padding, border and margin must be added to that in order to calculate the total space occupied by the box. (From this point on, we will use width for demonstrations, but the same principles apply to both width and height).

  1. box {
  2. width: 200px;
  3. border: 10px solid #99c;
  4. padding: 20px;
  5. margin: 20px;
  6. }

The above CSS, applied to a box, would mean that that box occupied 300 pixels of space horizontally on the page. The content of the box would occupy 200 pixels of that (dashed line added to demonstrate the edge of the area occupied by the box):

Box model demonstration.

In the above image, you can see that the pale blue area is 240 pixels wide (200 pixels of content plus 20 pixels padding either side). The border is 10 pixels wide either side, making the total width including the border 260 pixels. The margin is 20 pixels either side, making the total width of the box 300 pixels.

In practice, this can cause some confusion. For example, if I have a 100 pixel wide space available, and want to fill it with a pale red box with a dark red border and a small amount of padding, it would be very easy to write the CSS like so:

  1. box {
  2. width: 100px;
  3. border: 1px solid #900;
  4. padding: 10px;
  5. margin: 0;
  6. background: #fee;
  7. }

(A declaration of 0, as used for the margin above, does not require a unit to be added. Any value other than 0 does require a unit, e.g. px for pixels. Also, although "background" is defined as a shorthand property, it is more widely supported than the more correct "background-color".)

However, that will not give us a 100 pixel wide box, as the width declaration defines the content area of the box. The content area of the box will be 100 pixels - the total width of the box as defined above will be 122 pixels:

Box model demonstration.

In order to set the above box to only occupy 100 pixels horizontally, you would need to set the width of the content area to be 100 pixels minus the padding and minus the border, in this case 78 pixels, like so:

  1. box {
  2. width: 78px;
  3. border: 1px solid #900;
  4. padding: 10px;
  5. margin: 0;
  6. background: #fee;
  7. }

To calculate the overall width of a box, including all padding, borders and margins, you would use the following formula:

  1. total box width = content area width + left padding + right padding + left border + right border + left margin + right margin

Compatibility

At this point, you should now have a good understanding of what the box model is, and how boxes should be treated by different browsers. However, as you will soon learn (if you did not know already), not every browser does as it is supposed to. In order to use boxes, and by extension make the most of CSS in your website, you will need to be aware of how the different browsers treat boxes in practice and how to overcome and work around the problems presented by these idiosyncrasies.

Top Notch

Opera 6 Opera 7 Mozilla Firefox Camino Safari Konqueror Netscape 6 Netscape 7 Internet Explorer 6

Most browsers released in the last few years have no problem with boxes and render boxes correctly. Opera 6 and 7, Mozilla 1 (and by extension other browsers based on the Gecko engine like Netscape 7, Camino and Firefox and other derivatives), Safari, Konquerer (and derivatives) and Internet Explorer 5 for the Mac are all shining examples of how a web browser should behave, all rendering boxes flawlessly. IE 6 for Windows also will render a box correctly, as long as the Document Type Definition for the page is correct.

Whoops, Mrs Miggins, You're Sitting On My Artichokes

Internet Explorer 5 Internet Explorer 6

Some browsers don't display a box correctly. Unlike those below here, these browsers are widely enough used on the web that it is usually worth the effort to work through the problem. There are various methods for doing this, some better than others, that follow on. Most notable among the browsers with problems are Internet Explorers 4 and 5 and Internet Explorer 6. IE 6 is easy to work around, by adding a correct DTD (which you should be doing anyway).

Internet Explorer 5 is the main reason there is a box model problem at all. It, unfortunately, does not follow the simple definition for box layout as defined by the W3c. When you define a width for a box and it is rendered in IE5, instead of that width defining the content area of the box, it includes the borders and padding. Margins are added on to the content width correctly, but padding and borders are not. Unfortunately, this leaves us with some unpleasant choices:

  1. Use a box model hack
    Hack's like Tantek's box model hack are unfortunately something of a necessity. While some might argue that using hacks like this is completely missing the point of using CSS for web design, commercial necessity and the prevalence of IE5 leave us with little in the way of choice. The IE5 box model hack is in use all over the web and has spawned plenty of variants.
  2. Add in extra code
    Some might consider this a slightly "better" way of working around this problem. Rather than adding a style sheet hack, you can nest elements within each other. Adding a div within another div means that rather than using padding, you can use just margins, which are handled correctly by IE5. As with the box model hack, it is far from a perfect solution, but there are few other options if you want a site to look the same in IE5 as other more capable browsers.

Hall of Shame

Internet Explorer 4 Netscape 4

On the one hand, the browsers that I am about to mention are appalling, all failing dismally to render a simple box correctly for one reason or another. On a more positive note, users of these browsers, mostly old versions of current browsers, make up an extremely small, and continually shrinking, portion of web users. While you could probably find a workaround for the bugs in the display of boxes in these browsers, it is almost certainly not worth the effort - you are likely to cause yourself more harm than good with workarounds for these!

Netscape 4's box model is awful, but even worse, the simple box model hacks to fix the problem for IE5 and IE6 will crash Netscape 4. Netscape 4's style sheet support is abysmal overall, and it is being supported less and less. Though it is strictly a personal choice, I don't think it is worth the time and effort to support Netscape 4 any more - it's just not used enough, and the number of users is only ever going to shrink.

Internet Explorer 4 suffers, basically, the same problem as IE5. It treats boxes in a very similar way. However, it falls over in far more ways, and many of the available hacks will crash IE4. As it is also used by few people, and that number is dropping, many designers ignore it.

What does the future hold?

CSS3 promises us the option to determine how we want the user agent to treat boxes, and specify which box model we want to use. Support for CSS3 at a level that will be possible is many many years away yet. Until then, we are stuck with the CSS2 box model, and while IE5 is still used by a significant percentage of the web's population, we are going to have problems with boxes.

26 comments

Jesse Holmes
United States #1: December 23, 2004
I have added it up every way I can think of, and it seems to me that padding is actually subtracted from the content area, not added. Are you sure about this?
When you specify a width for a box, all browsers bar IE will make the content area, not including padding, the width defined. IE is different, and doesn't treat boxes properly. That's the reason the box model can be a bit tricky.
nice article, 10x!
 United Kingdom #4: March 12, 2006
Interesting read, lets hope more people read it too.
Well done, this post is recommended.
Thanks you
Great explanation. The clearest I have read in a while.
Excellent explanation...i was always a bit in disarray about who what where and how width is calculated.
It's as clear as the border around my containing table, only problem is the reliability of browsers to interpret and render correctly... Not a problem with Nested Tables :o)

Only this way is recognised as a standard implementation for which we can get a groovy image for the footer of our designs.. :o)

Great explaination, not to techhy or geeky just plain, simple, and straight forward M.B.P.C ! Margin, Border, Padding, and Content for those who couldn't keep up :o)
contradiction
Romania #9: April 12, 2007
for #2. ILoveJackDaniels, United Kingdom

if u use a strict doctype ie will add padding to width (height)
 United States #10: April 12, 2007
i need help understanding webdesign
Great Explanation, Thanks!
Really usefull post. Thx.
excelent explanation, box model was strong to understand for me, but with this explanation I have undestood that all.
Greg
United States #14: November 29, 2007
I would argue that the W3C box model is the broken one. Do you measure a box in your house by what it can contain? No. You measure along the outside of the box, including the border (the box material) and the padding (any foam padding you have inside the box).

Skipping the analogy, suppose you have 2 divs that you want to line up, but one you want to have a border and the other you do not. You would expect to be able to set each to the correct fixed width and have it work, but no, you'd have to adjust the width to include the missing border. It's retarded and broken. It's a shame that the other browser makers adopted it. I hate IE with a passion, but this is one case where they got it right.
Peter
United States #15: December 14, 2007
I agree with Greg W3C model is the broken one,
hence all the confusion with the box model. I am all for standards, but standards have to make sense.
Bill
United States #16: December 20, 2007
When you measure a frame for a picture, do you measure the outside of the frame? No. If my content is 5"x7" I want a container that will allow the full 5"x7" to be visible. If I get a frame that is 5x7, then I'm going to clip my picture by the width of the frame.
The W3C box model is correct, for it is the content area that is of interest. It is the content area where layout and content considerations are important. The perimeter (padding and border) stack on top of that and are of consideration only to surrounding elements. It all depends on your perspective, I guess.
P.S. Yes, I do measure the interior of a box if I question whether it is large enough to contain what I want to put in it - especially if I need 3" of padding material around it.
SIME
United States #17: January 17, 2008
THANKS DUDE, EVERY LITTLE BIT HELPS.
I never realized how borders factored into the box model, that's extremely useful to know. I'm glad I read this, thank you for taking the time.
Internet Marketing
Unknown #19: April 6, 2008
Thank´s a lot for this (and other) articles on your site. They are very usefull for beginners in css. New members of our agency MUST read your blog :-)
Mikael
Unknown #20: April 30, 2008
This is the first time I understand how this works. I am not a web designer but I am an Engineer some times I do design websites for my own use. How great this was, thank you.

Mikeal from the Ethiopian high lands.
Christine
United States #21: May 14, 2008
I use this DTD: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Is this the correct one to use to be sure that IE6 will render a box correctly?

Thanks for the great info!
josh
United States #22: June 6, 2008
This really helped me understand how CSS boxes work. Maybe now I can actually build a CSS site that works the way I want it to!
great tutorial
thank you
Anonymous
China #24: June 22, 2008
Perfect,Thanks!
B0NG671
Guam #25: July 30, 2008
very very helpful. thank you!
ali
Islamic Republic Of Iran #26: August 6, 2008
Thanks alot by information.

Post Your Comment

· Comments with keywords instead of a name have their URLs removed.
· Your email address will not be displayed or shared.

Live Comment Preview

 United States #27: 1 minute ago