Quick Tip: Isolating IE 7 Within A Stylesheet

In most cases IE fixes can be easily separated from the main CSS file via conditional comments. However, if certain restrictions don't allow for this you may need a way to isolate IE 6 and IE 7.

Most designers know that IE 6 doesn't understand

html>body

and therefore ignores any CSS entitity directly after it. However, IE 7 recognizes the first-child selector so another step is needed to isolate IE 7.

For example, let's say we have a

div

with an

id

of "content" that is rendered three different ways in IE 6, IE 7 and Firefox, respectively. You can use

html>body

to separate IE 6 from the group, but then you still need a way to separate IE 7 and Firefox.

I came to this junction and realized that I had never had this problem before. Thankfully, we have search engines. A quick Google search revealed a comment within a post about dropdown menus that had the answer.

Apparently,

*:first-child+html

is only readable by IE 7. Figure 1 has an example of how you would use this line of CSS to separate the styles for the different browsers.

  1. #content // This is normal CSS that every browser understands.
  2. {
  3. CSS PROPERTIES FOR ALL
  4. }
  5. html>body #content // Every browser except IE understands the first-child selector
  6. {
  7. CSS OVERRIDES FOR FIREFOX/SAFARI/ETC.
  8. }
  9. *:first-child+html #content // Only IE 7 reads this line and subsequent CSS properties
  10. {
  11. CSS OVERRIDES FOR IE 7
  12. }
Figure 1: Example CSS that isolates IE 6, IE 7 and the rest.

To finish, let's see it in a real example. The CSS that is in Figure 2 produces the box that is shown in Figure 3. It's red in IE 6, green in IE 7 and blue in all other browsers.

  1. #blogpost-95-example { width:500px; height:300px; margin-right:18px; float:left; }
  2. #blogpost-95-example { background-color:#f00; }
  3. html>body #blogpost-95-example { background-color:#00f; }
  4. *:first-child+html #blogpost-95-example{ background-color:#0f0; }
  5. <div id="blogpost-95-example">&nbsp;</div>
Figure 2: The CSS and XHTML for Figure 3.

 
Figure 3: The results of the CSS and XHTML in Figure 2.

So there you have it, isolation within the style sheet.

Next Post
Seeking Originality
Previous Post
Kindle, A Reading Device by Amazon.com

Send a Message Have something to say? Use the form below to email your comment directly to me. If warranted, I’ll do my best to respond in a timely matter.

What is my first name?
(Hint: It’s David)

Portfolio
Screen shot of the About page for www.sbts.edu

Content © David Yeiser, 2009 | Privacy Policy | Contact