First, note that a lot of position problems can arise because of
differing values on default paddings and margins. How do you know what
the default padding for a [list]
or a
is? Might it not
vary depending on the browser?
You can save some hassle using a global reset. This resets all elements to default values that you choose. I use:
font-size: 100%;
line-height: 1em;
margin: 0;
padding: 0;
}
The * is the universal selector. This selects every element.
I know of three ways to serve different style sheet rules to IE:
<ul>
[*] Browser detects plus javascript. I believe this is what Crono describes above.
[*] CSS hacks
[*] Conditional comments
</li>
[/list]
Conditional comments, I believe, are by far the best. Let's look at all three:
Browser detects and scripting
This
will work if you get your scripting right, but it's unnecessary.
Scripting, as Crono pointed out earlier, is slower than CSS and should
be avoided when a pure CSS solution is available.
Therefore, you should choose one of the other two methods.
CSS hacks
IE
does not understand some of the CSS selectors, and you can use this to
hide rules from it. For example, IE does not understand the child
selector "parent>child":
:body {color: red;}
html>body {color: black;}
This
CSS will cause your web page text to be coloured black in all major
browsers except IE, which will have red text instead. How does it work?
Well,
in CSS later rules overide earlier ones. Both rules set the text colour
of the <body>, but the one that comes second will have priority.
This second rule selects every <body> element (there's only one!)
that is a child (direct descendant) of an <html> element (again,
there's only one!).
In other words, the selectors select exactly the same element: <body>.
But
since IE doesn't understand the child selector ">", it will ignore
the second rule. So only the first rule is seen by IE, and the text
remains red.
But CSS hacks are a bad idea, I believe. When IE7
comes along, a lot of hacks will break; for example, IE7 will
understand the child selector. But IE7 won't fix all the shortcomings
of IE, so you might end up with broken pages! Read
http://blogs.msdn.com/ie/archive/2005/10/12/480242.aspx for details.
Conditional comments
These,
I believe, are the best method to use. Conditional comments are
conditional statements hidden inside ordinary comments. This means that
only the browsers you target (typically IE) will execute the
conditional statements. Other browsers will treat them like ordinary
comments -- and ignore them.
For example, this HTML will load an extra style sheet just for IE. Other browsers will only get the first stylesheet:
<link href="style.css" rel="stylesheet" type="text/css">
<!--[if gte IE 5]><link href="ie-hacks.css" rel="stylesheet" type="text/css"><![endif]-->
Those orange bits are the conditional comments. You can read about the conditional comments syntax here:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/ccomment_ovw.asp
Let's
recreate the CSS hacks example, but without using any CSS hacks. Here
is the content of the normal style sheet "style.css":
body {color: black;}
And here is the content of ie-hacks.css, which only IE will see:
body {color: red !important;}
The
"!important" means that this rule will overide other rules. Note
that no CSS hacks have been used, so this example will always work as
expected even in future versions of IE (although it's still best to
review the consequences when IE7 comes out; the point here is that
nothing crazy will go wrong).