Click here to Skip to main content
Click here to Skip to main content

Internet Explorer & CSS issues

By , 13 Feb 2006
 

Introduction

Trying to get CSS-based websites to look the same across all browsers can often be difficult. Many of the problems however lie with Internet Explorer implementing CSS commands differently to other, more standards compliant browsers. All is not lost, however, as many of the differences you see across browsers are caused by the same Internet Explorer CSS issues...

1. Page elements are narrower in Internet Explorer

Perhaps the most famous IE and CSS problem is Internet Explorer's misinterpretation of the CSS box model, which can cause page elements to be narrower in IE. Every HTML element is essentially a box, the width of which is the total of its margin, border, padding and content area. Imagine the following CSS rule:

div
{
    margin: 5em;
    padding: 4em;
    border: 1em solid green;
    width: 30em
}

This means that each div is 50em wide in total. This amount is made up of a 30em wide content area, and a 4em padding, 1em border and 5em (invisible) margin on both the left and right sides.

In IE however, the "border and padding are included in the width" of the content, as opposed to added on. In IE therefore, the width of the content is only 20em (30em less 5em padding and border on either side), and the total width of the div is just 40em.

This CSS box model problem occurs in IE5.x, and can occur in IE6, depending on how you declare the ISO value in the HTML code. There are two ways of doing this:

  • <?xml version="1.0" encoding="iso-8859-1"?>
  • <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

The first command is placed on the very first line of the HTML document and the second can be placed anywhere within the <head>. In order for XHTML pages to validate, it is compulsory to use one of these commands. The W3C recommends using the first command as the second will be phased out in the future.

By using the first command however, Internet Explorer 6 will render the CSS box model incorrectly, just like in version 5 browsers. To fix the box model problem, you'll need to insert a CSS hack to send different width values to different browsers. The CSS hack you use will depend on which ISO value you use, and therefore which versions of IE are rendering the box model incorrectly.

To fix up only IE5.x, use the following CSS commands:

div
{
    margin: 5em;
    padding: 4em;
    border: 1em solid green;
    width/**/:/**/ 40em;
    width: 30em
} 

To fix up all versions of IE, use these CSS commands:

div
{
    margin: 5em;
    padding: 4em;
    border: 1em solid green;
    width: 40em
}

html>body div
{
    width: 30em
}

(See the article, CSS hacks & browser detection for more on these hacks.)

2. Text spilling out of its container in non-IE browsers

Internet Explorer, unlike other browsers, will expand borders and background colours so text doesn't spill out of its containing element.

Let's say a div has been assigned class="box" and has the following CSS commands assigned to it:

.box
{
    width: 40px;
}

Non-IE browsers will adhere to the width: 40px CSS command, so the box won't expand to fit any text that's longer than 40px. IE however interprets width as min-width, and therefore will expand the div to fit the text (the same applies with height and min-height).

To ensure the text doesn't spill out of the box in all browsers, you'll need to use the following CSS rule, in addition to the first one:

html>body .box
{
    width: auto;
    min-width: 40px
}

IE will ignore this CSS command, as the command has html>body at the front of it (see the article, CSS hacks & browser detection for more on this). As such, this CSS command is only for non-IE browsers. The first CSS rule, width: auto, cancels out the original width rule. The second command, min-width: 40px then assigns a minimum width to the box, so the box will always expand to fit the text.

3. Disappearing background images

IE has a very freaky bug where it likes to make background images (and sometimes even text - particularly if there are floated elements around) disappear. This often happens when you scroll up and down on a web page and you can usually make the background re-appear by refreshing the page.

Obviously you won't want your site visitors to have to refresh a page to see a background image in full! A freaky solution to this freaky problem is to insert the CSS command, position: relative into the CSS rule containing the background image:

.foo
{
    background: url(filename.jpg);
    position: relative
} 

Occasionally this won't work, so another solution is to assign a width or a height to the element with the background image. You may not want to assign a height or width, so a solution is to assign a height of 1% for Internet Explorer. Because IE interprets height as min-height (see point 2 above) this CSS rule won't affect the appearance:

.foo
{
    background: url(filename.jpg);
    height: 1%
}

html>body .foo
{
    height: auto
} 

The height: 1% CSS command is cancelled out by the height: auto CSS command. Internet Explorer doesn't understand html>body, so by inserting this in front of the second CSS rule this whole CSS rule is ignored by IE.

4. Widths only working on IE

Every HTML element is either a block or an inline element. Examples of block elements include <div>, <p>, <h1>, <form> and <li>. Example of inline elements include <span>, <a>, <label>, <strong> and <em>.

One of the characteristics of inline elements is that you can't change the width of an inline element. The following CSS rule shouldn't, in theory, work:

span
{
    width: 100px
} 

This CSS rule won't work, except in Internet Explorer where each span will now have a width of 100px. In every other browser however, the width of the span will simply be the width of the number of characters contained in the element. The solution? Make the span a block level element:

span
{
    width: 100px;
    display: block
} 

(Turning the span into a block element will make the width command work in every browser, but it will also make the span begin on a new line. To combat this, you could assign float: left to the spa<code>n.)

5. Unstyled version of web page appearing in IE

When your website loads up in Internet Explorer, does an unstyled version of the page appear for a second or two, before the styled version kicks in this? If so, your website may be suffering from what's known as the Flash Of Unstyled Content (or FOUC).

If you're using the @import directive (e.g. <style type="text/css">@import "styles.css";</style>) to call up your CSS file then this phenomenon may be happening on your website in IE. It's weird, there's no logical explanation for it, but this problem obviously needs to be fixed.

The simple solution to this illogical problem is an equally illogical solution - insert either a link or a script element into the header:

  • <script type="text/javascript" src="scripts.js"></script>
  • <link rel="stylesheet" href="styles.css" type="text/css" media="print" />

It doesn't matter which one you insert (or even if you insert both). If you provide a print stylesheet, using the link element to reference it (as indicated in the example above), then you'll never see the FOUC phenomenon.

6. Fixed width web page not sitting in centre of window

Got a fixed width website and can't get it to centrally align in the window in Internet Explorer? Or you can get it to centrally align in IE but not in any other browser? Fear not, it's not your fault! Unfortunately, the correct way of centrally aligning content through CSS doesn't actually work in IE:

#container
{
    width: 770px;
    margin: 0 auto
}

The second command, margin: 0 auto, basically gives our containing element an automatic margin on the left and right, thereby positioning the containing element in the centre of the browser window.

IE however, will need slightly different commands to make this work:

body
{
    text-align: center
}

#container
{
    width: 770px;
    margin: 0 auto;
    text-align: left
} 

This will then centrally align the container in IE too. To prevent the text from centrally aligning too, we insert text-align: left into the container div.

This article was written by Trenton Moss, who's crazy about web usability and accessibility. He's the founder of Webcredible, a web usability and accessibility consultancy and is extremely good at usability training and usability testing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Boberta
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralencodingmemberDave Bacher25 Jul '06 - 5:28 
The first command is placed on the very first line of the HTML document and the second can be placed anywhere within the <head>. In order for XHTML pages to validate it's compulsory to use one of these commands. The W3C recommends using the first command as the second will be phased out in the future.
 
---
 
The XML preamble is optional as long as you use UTF8 or UCS2 encoding. A validating XML or XHTML processor is required to ignore a missing XML pre-amble, as long as the page content follows UTF8 or UCS2 encoding (source XML 1.0, XHTML 1.0, XHTML 1.1, XHTML 2.0). It is recommended to include the preamble, but never a requirement.
 
HTTP 1.1 specifies the encoding header, along with content-type and some others. The meta "http-equiv" element is used to override these headers; you don't need a meta tag (without an XML preamble) if your server is correctly specifying the encoding.
 
Most browsers will tollerate the XML header before the DOCTYPE before kicking into quirks/legacy mode. However, there are enough non-IE browsers that will engage Quirks mode if DOCTYPE isn't the first thing in the file (and no, its not just the browsers using IE's engine, either) that I honestly can't recommend including the preamble if you care about standards mode.
 
The bigger issue is there is no way to communicate to the browser the version of CSS being applied. IE 4, 5 and 5.5 are based on CSS1 draft specifications. IE 6 is based on the draft CSS1 specification with pieces of the draft CSS2 specification thrown in.
GeneralValuable Information, but only for the Short-TermmemberNiraj Vatvani23 Feb '06 - 1:19 
These hacks are valuable because they provide good alternatives to existing hacks. This article helps me in "hot-fixing" my existing problems. Ofcourse, some of these hacks may fail in compliance against the W3C CSS validator.
 
However, it must be understood that these hacks may not be valid for future releases of IE, or web-browsers in general. This is where conformance to standards is important because the standards offers a more tangible guarantee that your current code will most probably work with future releases of any web-browser provided that there has not been any evolution in the CSS syntax.
 
Nonetheless, Trent Moss has unraveled CSS gemstones that may be very handy for the CSS community at large. For this, he gets a 5 bar rating in my books.
 
/*****************************/
/*******START SIGNATURE*******/
At work, never ever be irreplaceable. If you cannot be replaced, you can't be promoted!;P But then again, promotions are for people still striving to reach their level of incompetence! :->
/********END SIGNATURE********/
/*****************************/
QuestionFlash Z-order?memberkorondy22 Feb '06 - 6:58 
Many thanks for the informative article.
 
I have a problem that has been plaguing me since time immemorial. I have a Flash movie on my client's page located just below a menu bar that contains pull-down menus. No matter what I try (z-order, rendering order, etc.) the Flash movie is always on top (using Mozilla browsers; it works just fine in all versions of IE) obscuring the pull-down potion of any active menu.
 
As a work-around, I have replaced the Flash animation with a static image if the browser is Mozilla. I would sure like to find a better, permanent solution. Do you have any ideas?
GeneralConditional Commentsmembercadred13 Feb '06 - 9:57 
Why use the box model hack when you can use conditional comments?
 

<!--[if IE]>
<style>
#somestyle {
...
}
</style>
<![endif]-->

 
You can even target specific IE versions I beleive
 

<!--[if IE 6]>
<![endif]-->

 
IMHO this is more simple than a css hack as well as more clear (and probably more future proof).
 
Edit: More info http://msdn.microsoft.com/workshop/author/dhtml/overview/ccomment_ovw.asp
 
-- modified at 15:59 Monday 13th February, 2006
GeneralRe: Conditional Commentsmemberkcs27 Feb '06 - 17:53 
The problem with conditional comments is that the style info has to be in the page and can't be used with css sheets.
 
-- modified at 0:06 Tuesday 28th February, 2006
 
Let me qualify the above by saying "I have a problem with conditional comments." I never, ever, ever have style info on the html page. Therefore, conditional comments are not an option for me.
GeneralRe: Conditional Commentsmembercadred28 Feb '06 - 5:28 
That's not true at all! My example above is simple, but you can have anything you want in conditional comments including a link tag
 
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="IE.css">
<![endif]-->

or a style import

<!--[if IE]>
<style>
@import url(IE.css);
<style>
<![endif]-->

 
So you would have a separate stylesheet for the special IE tweaks you would need, which keeps your default stylesheet clear of hacks.
 

GeneralI don't like thismemberJaroslav Klima13 Feb '06 - 9:40 
What you are presenting here are 'hacks', and they are plain evil. It is certainly possible to use standards compliant css and still have the same look across browsers, it just takes some skill and testing time.
GeneralRe: I don't like thismemberYo!13 Feb '06 - 20:22 
I agree, i am also of the opinion that you don't need hacks. The trick is that you must understand what you are doing ...
GeneralRe: I don't like thismemberTGolden17 Feb '06 - 9:38 
You both are a little quick to dismiss all of this as unnecessary and "evil". Yet neither one of you provided the "correct" way to deal with any of the various real situations the author identified. You owe it to the author and this community to do more than simply bad mouth the author's approach. Criticism is welcome if it's constructive - otherwise don't bother posting.
GeneralRe: I don't like thismemberGregoire21 Feb '06 - 3:10 
I fully agree with the answer of TGolden just above. This posting is only bad mouth and should be ripped off. Since Jaroslav seems to be so smart and have so many skills, he should show to the community how to work around the problems mentioned by the author.
 
Great ideas and tricks in the article.
GeneralRe: I don't like thismemberJaroslav Klima22 Feb '06 - 8:30 
I am sorry if my comment seemed offensive. I was merely trying to be helpful and point out that using css "hacks" should be avoided, since it is only a temporary solution and not good practice (for example, most of them will not work for IE7 as far as I know)
 
I do not feel confident enough about my web programing skills to try and teach other people, but this is what I do to get around the "Page elements are narrower in Internet Explorer" problem: Create a div with margins, padding and border width set to zero and the width parameter set to the desired width. Both IE and Gecko will render the div correctly. Then, place another div inside the first one and set all other properties except width. The "inner" div will fill the entire space of the "outer" one, having the same width on both IE and Gecko.
 
In my humble opinion, this aproach is better than the one described in the article, because it does not exploit faulty behavior, but solves the problem by using stuff that works correctly instead.
GeneralRe: I don't like thismemberGregoire23 Feb '06 - 2:55 
Fair enough for your comment which is more constructive this time.
 
I think that it really depends what the user wants to do: sometimes, you prefer to have a clean solution. Other times, you prefer to have a hack expecting that next version will fix the problem and you don't want to mess your code by ending with twice the number of divs than you initially needed. It's always a pain to read such code because you never initially understand the purpose of doubling the divs.
 
Bottom line, an article that could present both hacks and more standard solutions would be great...
 
Gregoire

GeneralRe: I don't like thismemberZachJ25 Feb '06 - 2:34 
How about having working solution?
 
You dont want to present hacks, period - not today - maybe yesterday - but everything being discussed here is not going to work in todays IE Browser. As each day goes by, this new article, becomes drastically less than informative, and more harmful as someone using IE6 is going to have no clue that that what is being presented is not going to work in IE7
GeneralRe: I don't like thismemberkcs27 Feb '06 - 18:06 
I think using hacks is perfectly valid, since Microsoft themselves uses the same hacks for their templates in VS2005. See Introduction to the ASP.NET Master Pages Template Set. Keep in mind that these are reference templates Microsoft is distributing.
 
The css sheets in these templates are full of those hacks (and use of some conditional comments).
 
Personally, my view is that these hacks _should_ and _would_ not be necessary if IE was standards compliant. But since it is not, and since IE7 will also not be compliant, then you are faced with an ugly problem becuase of an ugly product which unfortunatlely only has an ugly solution(s).
GeneralThanks alotmembertheDiver13 Feb '06 - 4:58 
Hi there
 
Nice article, it sure help me with some problems i have had on my website.
 
Keep up the good work.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 13 Feb 2006
Article Copyright 2006 by Boberta
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid