The Wayback Machine - https://web.archive.org/web/20100103111556/http://renownedmedia.com/blog/css-box-model-differences-in-firefox-and-internet-explorer/

CSS Box Model differences in Firefox and Internet Explorer

2 Comments
CSS Tutorials

Problem:

Here is the bane of web developers existence, Internet Explorer incorrectly displaying the CSS box model. What is the box model? This describes all elements used in HTML documents. Every element is in the shape of a rectangle (and you wonder why developers cringe when circles are a part of layouts?) A box has several attributes to it, such as width, height, padding, margin, and border.

Margin is the space outside of the box, padding is the space inside the box to come in contact with elements inside of it, width is the width of the box, height is the height of the box, and border is a line that is on the outside of the box between the margin and the padding. It seems like a simple concept doesn’t it?

Except, there is one small problem. Firefox and other standards complient browsers will add the size of the padding to the width or height of the box, while Internet Explorer will simply place the padding inside of the box without adjusting the width or height. Personally I like the way IE does it more, however this goes against the rules put in place by the W3C.

Below, in both the Firefox and Internet Explorer categories, I’ve first displayed how the browser interprets the box model, then display a screenshot in the browser. The lines are separated by 5 pixels, and each large line represents 25 pixels. Here is the code used to render each image:

<style>
* {
 font-family: verdana;
 font-size: 12px;
}
</style>
<div style="border: 1px solid black;">
 <div style="margin: 10px; border: 2px solid red;
padding: 10px;background-color: orange; float: left;
width: 100px;"><div style="background-color: lime;">&nbsp;</div></div>
 <div style="margin: 10px; border: 2px solid red;
padding: 10px; background-color: orange; float: left;
width: 100px;"><div style="background-color: lime;">&nbsp;</div></div>
 <div style="margin: 10px; border: 2px solid red;
padding: 10px; background-color: orange; float: left;
width: 100px;"><div style="background-color: lime;">&nbsp;</div></div>
 <div style="clear: both;"></div>
</div>

Firefox:

border                                  border
   |<- padding -|   content   |- padding ->|
                |<-- width -->|
Firefox Box Model

Firefox Box Model

Internet Explorer:

border                                  border
   |<--------------- width --------------->|
   |- padding ->    content    <- padding -|
Internet Explorer Box Model

Internet Explorer Box Model

What this means is that in FF, if you create a box that is 400px wide, and has a padding of 10px and a margin of 10px, it will actually end up being 440px wide total. In IE, if you create the same box, it will be 420px wide total. This is a very annoying problem because this is the most simple building block to creating any CSS/XHTML based website layout, and the two competing browsers treat these items differently.

Solution:

The solution for this problem is a simple (albeit hacky) fix. Place one element inside of the other, where your outside (container) element does not have any padding but has a width defined, and the inside element does have padding defined. The reason this is considered a hack is because we have to add twice the markup to our webpages to get the same effect rendered correctly in both browsers.

  • Digg
  • Google Bookmarks
  • MySpace
  • Reddit
  • Facebook
  • NewsVine
  • StumbleUpon
  • Twitter
  • WordPress
  • Delicious
  • Yahoo Messenger
  • AIM
  • Bebo
  • Share/Bookmark

Related posts:

  1. Attractive HTML/CSS Gmail Signatures This will explain how to use attractive signatures in...
  2. Tabbed CSS Navigation This explains how to build a navigation which looks...
  3. Image CSS CSS Rules for IMG tags, along with information about using...
  4. CSS Drop Shadows using text-shadow The latest version of CSS allows you to add shadows...
  5. Introduction to CSS: Selectors and Relationships Explained This article will provide an introduction to CSS, which...

2 Responses

  1. Another solution to normalize differences across browsers is to use a strict doctype. Check out http://helpcss.com/wiki/Doctype

  2. Another solution would be to fix the box modeling using CSS3. Although not yet finalized, both Firefox and IE (8 of course, not the accursed IE6) have their own implementations. more info can be found at http://www.quirksmode.org/css/box.html

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>