Properly hiding CSS from Internet Explorer

Properly hiding CSS from Internet Explorer

  • Posted on 07/11/2006 at 07:30 AM by Sandra Clark
  • Categories: CSS

At CFUNITED, I mentioned the proper way to hide information from Internet Explorer. While many CSS filters and hacks rely on using advanced selectors that IE 6 and below don't recognize, that is not going to be the case for Internet Explorer 7.

So the proper way to hide information from Internet Explorer would be to use something similar to the code below.

<head> <link rel="stylesheet" type="text/css" href="assets/css/styles.css" media="all" /> <!--[if lt IE 6]> <link rel="stylesheet" type="text/css" href="assets/css/ie5.css" /> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="assets/css/ie6.css" /> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="assets/css/ie7.css" /> <![endif]--> </head>

Notice, that the first call is simply a link to an overall styles.css file. This file will hold all the CSS for my site, for all browsers. This style sheet is designed to work with a CSS 2 compliant browser (which IE is not). Most, if not all of the styles here will work in Internet Explorer, however, there are bound to be rules which need to be overwritten to work correctly in at least one version of Internet Explorer. We take advantage of the Cascade by making sure that our main style sheet is loaded first, and then the style sheets that need to override specific rules for each of the IE browser versions are loaded last. (Since properties which apply to the same rule, but come later, overwrite earlier properties).

The first conditional statement shown below:

<!--[if lt IE 6]> <link rel="stylesheet" type="text/css" href="assets/css/ie5.css" /> <![endif]-->

Loads style sheet information for Internet Explorer 5 (note that Internet Explorer 5 for the Mac does not read conditional comments at all, but since Microsoft has stopped supporting this program and most Mac users are either using Safari, Konqueror or Firefox, its really a non issue). Any style sheet rules that need to be overwritten to accommodate IE5 would be written here.

The second and third conditional statements:

<!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="assets/css/ie6.css" /> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="assets/css/ie7.css" /> <![endif]-->

respectively take care of any problem for IE6 and then IE7. Nice!

Keep in mind that when taking care of the differences, you do not need to overwrite the entire rule, only those properties that need to change. Consider the following:

In our styles.css file, we have the following rule defined.

div#box1 label, div#box2 label{ text-align: left; width: 20em; padding: .5em; margin-top: -.15em; }

IE5 has a problem with the box model so we need to adjust the width to accommodate the situation. So in ie5.css, we would redefine the rule as:

div#box1 label, div#box2 label{ width: 21em; }

Notice that only the changed property occurs in the ie5.css. Since IE6 and IE7 implement the box model correctly, there is no need to change the property in either of those style sheets. The ie6.css and ie7.css style sheets will simply use the original property values from styles.css.

Microsoft has an overview of the topic, if you care to delve in more deeply.

Add a Comment

  • Posted By Sandra Clark
  • Posted On May 4, 2007 at 06:51 AM
They work for me on cfm. I'd look at your web server as well.

http://www.shayna.com

  • Posted By Sandra Clark
  • Posted On May 18, 2007 at 03:32 AM
I used to use Deans Libraries and promote them. Unfortunately, there were instances that using it caused web pages to crash a browser. Unless those have been fixed, I stopped using it. Too bad, its a really nice browser fix.

http://www.shayna.com

  • Posted By Sandra Clark
  • Posted On May 19, 2007 at 04:13 AM
Redefining the style definitely works, I do it all the time.

Sometimes it could be your CSS. Sometimes you might not be calling the linked style sheet correctly. To debug this do two things.

First get rid of the IE conditional statements and see whether or not your style is overridden in Firefox.

Second make sure that your ie conditional statement comes AFTER your other style sheet calls. REmember that the last stylesheet called overrides the previous ones.

Third, put something funky in your IE style sheet such as body{background-color: green;}. Then call the stylesheet first through firefox (to make sure it works and then through IE).

If it is found in Firefox and not in IE, then check your actual conditional statement and make sure it is correct.

http://www.shayna.com

  • Posted By Dave
  • Posted On March 20, 2007 at 08:26 AM
Hi, Love this site, and Sandra's phenomenal CSS advice (on CF-Talk), so I hate to nit-pick. But... In this example (for blog.display_entry&id=131), won't IE5 pick up the stylesheets for both IE5 AND IE6?

  • Posted By Sandra Clark
  • Posted On March 20, 2007 at 02:42 PM
The IE6.css will be picked up by both IE5 and 6 in this example.

To create a specific one for a version of IE, you can do something similar to:

<![if !IE 5]>

<![endif]>

http://www.shayna.com

  • Posted By Sandra Clark
  • Posted On July 12, 2007 at 01:45 PM
In straight HTML? I can't think of a way. Remember that IE7 understands most of the selectors that we used for IE5 and 6 for hacks. Hence the idea of the conditional IE comments instead.

I suppose if you were using a server side language, you could figure out the browser, stick in some specific classes where necessary based on the browser, but then isn't that just going back to the days of browser specific code?

According to thecounter.com, IE5 has about a 1% penetration rate for June 2007. Is it really even worth supporting at this point?

http://www.shayna.com

  • Posted By Peter D
  • Posted On May 4, 2007 at 06:43 AM
Here's a puzzler: These proper CSS / IE hides work for me on CF templates on my local machine. On live CF server they do not work if my page extension is ".cfm", exact same code works fine if my page extension is ".html". I understand that these hides work only for Win/IE machines BUT do they only work for .html? OR is their something on our CF server messing up embedded HTML?

  • Posted By Gary Holmes
  • Posted On May 17, 2007 at 11:18 PM
Here's a way to fix IE6 compliance bugs by attatching Javascript libraries:

http://www.grumpycoder.co.uk/how-to-fix-ie6-for-w3c-standards-compliance-using-javascript-libraries/

Worth a read!!

http://www.grumpycoder.co.uk

  • Posted By Al Chou
  • Posted On May 18, 2007 at 08:25 PM
In my HTML I've got

and the only thing in the IE-specific CSS file is:

#pagetitle { font-size:2pt ; line-height:2pt }

which is supposed to override the main stylesheet's declaration:

#pagetitle { font-size:24pt; line-height:24pt; font-style:italic }

but IE doesn't seem to notice that I've tried to redefined the pagetitle id's style. Am I doing something wrong?

http://DavidsonDales.com/

  • Posted By Al Chou
  • Posted On May 18, 2007 at 08:27 PM
Does redefining an id's style via a second external stylesheet in a link tag inside a conditional comment work? I tried to post an example just now, but my comment didn't get posted, so I'm keeping this one shorter in case it doesn't get posted either.

http://DavidsonDales.com/

  • Posted By Al Chou
  • Posted On May 18, 2007 at 08:29 PM
Does redefining an id's style via a second external stylesheet in a link tag inside a conditional comment work? I tried to post an example just now, but my comment didn't get posted, so I'm keeping this one shorter in case it doesn't get posted either.

http://DavidsonDales.com/

  • Posted By Al Chou
  • Posted On May 18, 2007 at 08:31 PM
Does redefining an id's style via a second external stylesheet in a link tag inside a conditional comment work? I tried to post an example just now, but my comment didn't get posted, so I'm keeping this one shorter in case it doesn't get posted either.

http://DavidsonDales.com/

  • Posted By ed
  • Posted On July 12, 2007 at 01:33 PM
Is there a way to streamline this hack and inline the code? For example, if you wanted to add pixel shims via a spacer.gif to one certain area just for ie5 and nothing else:

.spacerIE5{height: 2px;} .spacerIE7andTheRest{height: 1px;}

  • Posted By shaina
  • Posted On September 30, 2007 at 11:42 PM
I have a jsp page which works fine in ie6,firefox but in ie7 page alignment goes for a toss.All the content shifts to center of the page whereas it should be on the left side.Pls help.Thanx

Post Your Comment Here