Keep it Dark: Hiding and Filtering CSS

Published Wednesday, January 10, 2007 @ 11:45 am • 8 Responses

Hiding and filtering CSS rules for specifically targeted browsers is often a foregone conclusion when it comes to cross-browser design considerations. Rather than dive into some lengthy dialogue concerning the myriad situations and implications of such design hackery, our current scheduling restraints behoove us to simply cut to the chase and dish the goods. Having said that, we now consider this post a perpetually evolving repository of CSS filters..

Hide CSS from IE3, IE4, NS4

This method employs JavaScript to hide CSS from IE3, IE4, NS4, and any other browser that does not support document.getElementById. The script must be written as a single line. Backslashes must comment out slashes.

<script type="text/javascript">
<!--//--><![CDATA[//><!--

if(document.getElementById) document.write('<style type="text\/css"> h1 { color: red } \/* hides from old browsers *\/ <\/style>');

//--><!]]>
</script>

Hide CSS from NS6, Moz1, Op5.12, Op6.0

This method employs JavaScript to hide CSS from NS6, Moz1, Op5.12, Op6.0. The script must be written as a single line. Backslashes must comment out slashes.

<script type="text/javascript">
<!--//--><![CDATA[//><!--

if(!(document.getElementById&&!document.all)) document.write('<style type="text\/css"> h1 { color: green } \/* hides from NS6.1, Moz1.0 *\/ <\/style>');

//--><!]]>
</script>

Hide CSS from all IE < 7

This method employs downlevel-conditional comments1 to hide CSS from all IE less than IE7. Extrapolation of this method provides CSS filtering for any specific version(s) of IE. Note: the last line of this method may result in validation errors1.

<!--[if gte IE 7]>
<link rel="stylesheet" type="text/css" href="ie-hacks.css" media="screen" />
<![endif]-->

Likewise, downlevel comments may be used to filter scripts, markup, or just about anything.
In this case, a JavaScript file is hidden from all IE less than IE7:

<!--[if gte IE 7]>
<script src="http://domain.com/javascript.js" type="text/javascript"></script>
<![endif]-->

This example shows CSS hidden from any IE strictly less than IE6.0:

<!--[if gte IE 6.0]>
<style type="text/css">h1 { color: red }</style>
<![endif]-->

Hide CSS from NS4.x, IE3Win, IE4win+mac, IE5win, and IE6win

This method hides CSS from NS4.x, IE3Win, IE4win+mac*, IE5win, and IE6win. The method relies on the exclusion of quotes from the url() attribute:

<style type="text/css">
<!--
@import url(hidden.css) screen; /* hide from NS4.x, IE3Win, IE4win+mac, IE5win, and IE6win */
-->
</style>

* Note: IE4/Win will parse CSS when located in the same directory as the reference file. Further, this method fails with IE4.72/Win

Hide CSS from NS4

This method hides CSS from all NS4:

<style type="text/css" media="screen, projection">
<!--
.hidden {color: red } /* hides from NS4 */
-->
</style>

Hide CSS from NS4, IE4win+mac

This method hides CSS from NS4 and IE4win+mac:

<style type="text/css">
<!--
@import "hidden.css"; /* hides from NS4, IE4win+mac */
-->
</style>

Hide CSS from NS4, IE4win+mac

This method also hides from NS4 and IE4win+mac:

<style type="text/css">
<!--
.hidden {color: red } /* hides from NS4, IE4win+mac */
.pseudo {/* perhaps not needed */}
-->
</style>

Hide CSS from Opera 5, Opera 6, Opera 7

This method hides CSS from Opera 5, Opera 6, Opera 7:

<style type="text/css" media="sc&#82;een">
<!--
.hidden {color: red } /* hides from Opera 5,6,7 */
-->
</style>

Hide CSS from NS6.1 and Mozilla

Inserting a comment between an element and its associated class or id results in hiding the corresponding CSS from Netscape 6.1 and Mozilla:

<style type="text/css">
<!--
span/*comment*/.hidden { color: red } /* hides from NS6.1 and Mozilla */
-->
</style>

Hiding via the @import Directive

Hide CSS from NS4.x, IE3win, IE4win (not 4.72), IE4.01mac, IE4.5mac, Konq2.1.2, Amaya5.1win

This method hides CSS from NS4.x, IE3win, IE4win (not 4.72), IE4.01mac, IE4.5mac, Konq2.1.2, and Amaya5.1win. It employs the url() attribute with quotes:

@import url("style.css");

Hide CSS from NS4.x, IE3, IE4* (not 4.72)

This method hides CSS from NS4.x, IE3, IE4* (not 4.72). It employs the url() attribute without quotes:

@import url(../style.css);

* Note: IE4win will execute the CSS if its file is located within the same directory as the calling HTML file.

Hide CSS from NS4.x, IE6win and below

This method hides CSS from NS4.x, IE6win and below. It employs the url() attribute with the media attribute value set to screen:

@import url(style.css) screen;

Hide CSS from NS4.x, IE4win and below, IE4.01mac, Konq2.1.2

This method hides CSS from NS4.x, IE4win and below, IE4.01mac, Konq2.1.2. It employs the import directive without the url() attribute:

@import "style.css";

Hide CSS from specific (X)HTML tags

Listing more than one class name hides the CSS from that particular tag:

<div class="class1 class2">The CSS from either class will not affect this tag</div>

Target IE5/Win Only

Using the now-common mid-pass filter, it possible to target IE5/Win exclusively:

@media tty {
i{content:"\";/*" "*/}} @import 'ie5-win.css'; /*";}
}/* */

Target IE5/Mac Only

Using the now-common IE5/Mac band-pass filter, it is possible to target IE5/Mac exclusively:

<style type="text/css">
/*\*//*/
@import "ie5mac.css";
/**/
</style>

Target IE5/Win Only

Using the now-common IE5/Win band-pass filter, it is possible to target IE5/Win exclusively:

<style type="text/css">
@media tty {
i{content:"\";/*" "*/}}; @import 'styles.css'; {;}/*";}
}/* */
</style>

References


Dialogue

8 Responses Jump to comment form

1mtness

June 12, 2007 at 2:59 pm

Hide css from IE7 (and of course othe IEs, too!)

html>/**/body { }

Can’t remember where I found that one.
Works like a charm.

Kind regards,
mtness

2Perishable

June 17, 2007 at 8:29 am

Thanks for the tip! I am working on an IE7 hacks article and will definitely add this to the list. You can never have too many hacks for good ‘ol Internet Explorer!

3mtness

June 23, 2007 at 11:05 am

hi there again!
just to explain this hack in full:

htlml>/**/body foo { bar }
where *foo* is a css class or id or selector and
*bar* a css statement like font-size

4Perishable

June 23, 2007 at 6:31 pm

Ah, very nice — very thoughtful of you to drop in again to share the details behind the IE7 hack. Do you happen to know if it passes W3C validation? That would be icing on the cake..

5mtness

August 6, 2007 at 12:53 pm

Hi there again!

I just dropped by with good news:
This hack passes W3C validation!

html>/**/body #content { height: 235px; }
/* hide css from IE7! */

renders to:
html > body #content { height : 235px; }

That’s it.

Another nice link for ya:
http://pornel.net/firefoxhack

target FF only:
#hackme, x:-moz-any-link {styles for Firefox 2.0 here}

#hackme, x:-moz-any-link, x:default {restore styles for Firefox 3.0 and newer}

works like a charm, too.
This one doesn’t validate, coz -moz-any-link is an unknown pseudo-element or pseudo-class.

Kind regards,
mtness

6Perishable

August 6, 2007 at 2:43 pm

Ahh, so good to see you again!
Your site is looking better than ever..
I found myself actually enjoying the Internet there for awhile.. but, alas, back to the grind!

Thanks for hooking us up with even more tasty Firefox hacks. I look forward to returning to this post in the near future to dig into some of the juicy details.

Muchas gracias, señor!
m0n

7iain Urquhart

December 18, 2007 at 8:30 pm

does anyone know a way of hiding javascript from ie 5 mac…

jquery seems to be crashing the browser and I’d prefer to just have it hidden from ie mac users,

thanks. Iain.

8Perishable

December 18, 2007 at 10:53 pm

Hi iain,

Beyond testing for specific functional support via if(function.name), you may have no choice but to journey into the dark realms of “browser detection”.. ;)

May the force be with you.

Always.

Subscribe to comments on this post


Share your thoughts..

TopRead official comment policy

← Previous post • Next post →

« Memorable Quotes, Part 2Riding the Wave »

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

automation is great: i've got photoshop batch processing 300+ images while FTP is simultaneously uploading them to the server..

Perishable on Tumblr

Tons of Firewalls

Tuesday, 7 October 2008, 1:45 am

Recently overheard on conservative talk radio (instructing listeners how to obtain a free promotional video from their new website):

“This website has tons and tons of firewalls, so you have to use your real email address to download the video..”

The Quiet Search Revolution

Monday, 6 October 2008, 12:15 pm

Just a thought.. As awesome as Google is these days, it would suck if they ended up owning the entire search-engine business. When they get to the point where all competition is impossible (due to their sheer size, financial resources, media influence, etc.), how many alternate search engines will have the resources for continuous improvement and top-quality search results? When this happens, we will have no choice but to do exactly what Google tells us to do.

As deeply ingrained as it is for everyone to instinctively and unthinkingly turn to Google for their search activity, it is time to leave a few alternate search tabs open for as much use as possible. Instead of using Google just because that’s what you always do, try your search on MSN, Yahoo, Ask, or any of the other independent search engines instead. Sharing traffic with other search engines is a nice, quiet way to keep the competitive spirit alive and well in the search-engine business.

Disappearing WordPress Posts

Wednesday, 1 October 2008, 7:50 pm

Today I experienced difficulties while trying to publish or even save new posts in WordPress. I would compose the post as usual, add all of the keywords, tags, meta tags, and so on, but as soon as I clicked the “Publish” or “Save” button, the post would just disappear from existence.

The weird thing is that during the drafting process, WordPress’ default auto-save feature showed that the post had been saved at expected intervals. Unfortunately, after trying to publish several different posts, WordPress showed absolutely no record of the posts ever being created. They simply vanished into thin air.

Fortunately, a little investigation revealed the culprit. If you should find yourself dealing with this same issue, here are some different things that you should try. First, re-upload fresh copies of your entire WordPress installation. I don’t know why exactly, but apparently various files can either go stale or completely disappear from the server. Overwriting or writing fresh files may do the trick.

If that doesn’t work, check your WordPress database for errors. In my case, a little investigation revealed that something had caused a couple of fatal errors in the wp_posts table. Fortunately, checking and repairing the table solved the issue.

Tumblr Battles

Wednesday, 1 October 2008, 5:30 pm

Please excuse the duplicate Tumbr posts.. seems there is no way to ping Tumblr to refresh/rebuild the RSS feed according to changes in post content. So, to resolve the issue I have discussed now like two or three times regarding paragraph elements and proper feed formatting, I have no choice but to repost a majority of my text posts.

This is necessary for the proper import and display of my Tumblr feed into WordPress. Currently, there are five items displayed at once, each styled according to proper inclusion of paragraph tags. Thus, whenever the Tumblr feed “forgets” to enclose single-paragraph posts with the proper tags, the result is an unstyled post entry displayed on my site.

Assuming that makes sense, you will please excuse my dust while I repost a few older entries in an attempt to reconstruct (the hard way) a properly formatted Tumblr feed.

More Optimization Measures

Wednesday, 1 October 2008, 5:27 pm

Another important step in improving the performance of my recent redesign involves the optimization of both CSS and JavaScript content. During development there were around 15 server requests for these two types of files, 10 JavaScript files and 5 CSS files. This was okay for my own use, but would not work for production purposes.

Optimizing these file types involves consolidation, compression, and caching. Consolidation of 10 JavaScript files into three is huge improvement. Now I deliver one JS file for the functionality of the site, one for Mint, and another for Analytics. Likewise for the stylesheets; after consolidation, a single stylesheet is delivered to all modern browsers. There are two additional stylesheets as well, but they are targeted at IE6 and mobile browsers and will not load elsewhere.

Once the files were consolidated as much as possible, it was time to optimize or “crunch” them. Using the sexy Flumpcakes CSS optimizer, I was able to reduce my stylesheets by around 25%. Likewise for JavaScript, I used xtreeme.com’s optimizer to shave an additional 20% off the size of my JS content.

Finally, once I had consolidated and compressed my JS and CSS files as much as possible, I wanted to further my optimization efforts by ensuring that these files were cached by the browser. By setting far-future Expires headers for everything but the statistical files, my site gains an additional performance boost by eliminating the need to reload preexisting content.

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • Adam Singer: Thanks for this. You're right, if it isn't broken, don't fix it. I was about to update my permalinks and install a plugin to redire...
  • Marilyn: It looks great on my browser! I wish I had that much creativity in my head! It's gorgeous!...
  • Randy: "Too girly?" It looks like a great design. Define "too girly!"...
  • Christopher Ross: .htaccess based redirects are wonderful. I'm always baffled by web professionals who don't take the time to learn more about them....
  • federico: Hi Jeff... tnx so much...it worked perfectly... c u Federico...
  • Cooltad: The skin seems (mostly) fine in my expert opinion. Your one of the few people able to make a design with a transparent table and a b...
  • Neal: The free Intro to Linux book is a great place to start http://www.ischool.utexas.edu/mirrors/LDP/LDP/intro-linux/html/index.html ...
  • Louis: @Jeff: Your “Archives” page is slick, although I would expect a cleaner implementation from such a vehement advoc...
  • Jeremy: Well I think that you may be over-critical, I don't see a darn thing wrong with it - I like it a lot!...
  • Jeff Starr: Alright, this is exactly the kind of information I was hoping to get. Lots of great ideas and recommendations here. I will be reading...

Read more recent comments..