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:

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).
box {
width: 200px;
border: 10px solid #99c;
padding: 20px;
margin: 20px;
}
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):

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:
box {
width: 100px;
border: 1px solid #900;
padding: 10px;
margin: 0;
background: #fee;
}
(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:

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:
box {
width: 78px;
border: 1px solid #900;
padding: 10px;
margin: 0;
background: #fee;
}
To calculate the overall width of a box, including all padding, borders and margins, you would use the following formula:
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

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 [url=http://www.addedbytes.com/design/dtds-explained]Document Type Definition[/url] for the page is correct.
Whoops, Mrs Miggins, You're Sitting On My Artichokes

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:
- Use a box model hack
Hack's like [url=http://www.tantek.com/CSS/Examples/boxmodelhack.html]Tantek's box model hack[/url] 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. - 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

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.

37 Comments
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?
#1, Jesse Holmes, United States, 23 December 2004. Reply to 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.
#2, Dave Child, United Kingdom, 23 December 2004. Reply to this.
nice article, 10x!
#3, Mon, Cyprus, 19 September 2005. Reply to this.
Interesting read, lets hope more people read it too.
#4, Neon, United Kingdom, 12 March 2006. Reply to this.
Well done, this post is recommended.
Thanks you
#5, Fred, Spain, 28 May 2006. Reply to this.
Great explanation. The clearest I have read in a while.
#6, Pam, Canada, 23 June 2006. Reply to this.
Excellent explanation...i was always a bit in disarray about who what where and how width is calculated.
#7, Anthony Ettinger, United States, 27 September 2006. Reply to this.
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)
#8, Neil Prytherch, United Kingdom, 29 January 2007. Reply to this.
for #2. Dave Child, United Kingdom
if u use a strict doctype ie will add padding to width (height)
#9, contradiction, Romania, 12 April 2007. Reply to this.
i need help understanding webdesign
#10, rena, United States, 12 April 2007. Reply to this.
Great Explanation, Thanks!
#11, Chrissie Brown, Germany, 3 July 2007. Reply to this.
Really usefull post. Thx.
#12, Rafael Guimarães, Unknown, 15 August 2007. Reply to this.
excelent explanation, box model was strong to understand for me, but with this explanation I have undestood that all.
#13, Carlos Magaña, Unknown, 12 September 2007. Reply to this.
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.
#14, Greg, United States, 29 November 2007. Reply to this.
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.
#15, Peter, United States, 14 December 2007. Reply to this.
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.
#16, Bill, United States, 20 December 2007. Reply to this.
THANKS DUDE, EVERY LITTLE BIT HELPS.
#17, SIME, United States, 17 January 2008. Reply to this.
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.
#18, Jack Moore, United States, 24 January 2008. Reply to this.
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 :-)
#19, Internet Marketing, Unknown, 6 April 2008. Reply to this.
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.
#20, Mikael, Unknown, 30 April 2008. Reply to this.
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!
#21, Christine, United States, 14 May 2008. Reply to this.
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!
#22, josh, United States, 6 June 2008. Reply to this.
great tutorial
thank you
#23, ????????, Unknown, 8 June 2008. Reply to this.
Perfect,Thanks!
#24, Anonymous, China, 22 June 2008. Reply to this.
very very helpful. thank you!
#25, B0NG671, Unknown, 30 July 2008. Reply to this.
Thanks alot by information.
#26, ali, Islamic Republic Of Iran, 6 August 2008. Reply to this.
Great explanation. very useful to every one. Thanks a lot
#27, sivakumar, India, 3 September 2008. Reply to this.
Nice to see some Plain English. Now all I have to do is get my students to understand . . . ! Thanks.
#28, Andrew, United Kingdom, 19 September 2008. Reply to this.
No - Greg is correct - it's the CSS box model that is broken. The CSS approach breaks horribly as soon as you want your layouts to scale to the size of your screen - a common requirement.
For example, if I have a DIV that is 100% the height of the screen, and then another DIV inside that, but inset by say 10 pixels you can't do it with the CSS box model. And when I say can't I mean can't - the only way to get round it is to use javascript, which is what a lot of sights end up doing - and that's just crazy.
Of course with IE's supposedly broken box model you simply set the padding to 10px on the out div and it works - dead easy. Oh, but I forgot it's Microsoft, so it must be rubbish right?
#29, John, United Kingdom, 19 October 2008. Reply to this.
I agree with Greg, Peter and John.
This is another example of the failure of the CSS standards. John gives a valid design to expect CSS to be able to provide and it can't.
#30, Pocoyo, United Kingdom, 6 January 2009. Reply to this.
Very helpfull and understandable piece. thankS very muCh.
#31, Emin, United Kingdom, 13 May 2009. Reply to this.
Thanks for the turtorial, it was a nice read.
However, I need to wonder why so many developers are adhering to the W3C box model instead of challenging it. The box model makes absolutely no sense.
To think the W3C committee expects us to take margin measurements to the box is asinine. It's like measuring the space between your wall and couch while buying a new couch with these measurements to ensure it fits.
I would recommend everyone write to the W3C and get them to do it Microsoft's way, which is the *correct* way to DESIGN a page.
:)
#32, PetrifiedJello, United States, 21 May 2009. Reply to this.
Hi guys. Thanks for tips and great tutorial about Css box. I really need that.
#33, hasfa, Kuala Lumpur Malaysia, 31 August 2009. Reply to this.
A fantastic explanation, simple and to the point. I still question the W3C box model but a least now I can confidently with with it minus hours of frustration. Thanks.
#34, Rob, Ireland, 7 November 2009. Reply to this.
Thanks alot
#35, Khalid, Jordan, 28 December 2009. Reply to this.
Good Explanation
#36, Dale Pinney, United Kingdom, 22 February 2010. Reply to this.
Thanks. Simple and straightforward.
#37, Kiko, Australia, 8 March 2010. Reply to this.