Here are examples of various methods which let you hide CSS from buggy browsers. Please send comments and report errors to Christian Jacobsson,
.Due to limited CSS support many browsers ignore CSS under certain circumstances, which can be exploited. Below are just a few examples I find useful, many more can be found at http://w3development.de/css/hide_css_from_browsers/ and http://www.richinstyle.com/masterclass/crossbrowser.html. None of the tricks in this section should rely on invalid HTML or CSS.
@import tricks can be added in a single external style sheet instead of in every HTML page.
| Example: | Hiding technique: | Hides CSS from: | Notes: | |
|---|---|---|---|---|
| Confirmed | Assumed | |||
| A. This text should be red. |
<style type="text/css"><!--
|
|
|
Seems to hide CSS from all current MSIE browsers and Netscape 4. |
| B. This text should be green. |
<style type="text/css" media="screen, projection"><!--
|
|
|
Netscape 4 only recognizes media="screen". While media="all" hides CSS too, it's a less good choice for media dependent style sheets.
|
| C. This text should be blue. |
<style type="text/css"><!--
|
|
|
Hides CSS from most older browsers (may not hide from IE4.5/mac though). |
| D. This text should be pink. |
<span class="a b">More than one classname is used</span>
|
|
|
Hides CSS from a particular tag in the HTML source. |
| E. This text should be orange. |
<style type="text/css" media="scReen"><!--
|
|
|
Very useful if CSS overflow: auto is used (which is buggy in Opera 6).
|
| F. This text should be red. |
span/*comment*/.class {color: red}
|
|
|
It seems the comment must be placed between an element and a class (or id) selector. |
Since different browsers support different JavaScript objects, these objects can be used to identify a browser brand and -version. More suitable objects are listed at http://www.javascriptkit.com/javatutors/objdetect3.shtml (which also explains some theory behind object detection in general). Note that object detection is not the same as sniffing for User Agent strings, which is said to be unreliable since many browsers fake their UA strings (see http://webtips.dan.info/brand-x/useragent.html and http://www.blooberry.com/indexdot/html/topics/uastring-navobj.htm). However note that Opera supports different JavaScript objects depending on which browser it identifies itself as (see note below).
document.getElementById while newer browsers have less CSS bugs.
| Example: | Hiding technique: | Hides CSS from: | Notes: | |
|---|---|---|---|---|
| Confirmed | Assumed | |||
| A. This text should be red. |
|
|
|
Should be suitable for detecting all browsers released before the W3C Document Object Model and its document.getElementById object (in October 1998, see http://www.w3.org/DOM).
|
| B. This text should be green. |
|
|
|
*Hides CSS from Opera5/6 when it identifies itself as "Mozilla" or "Opera", but not when it identifies itself as "MSIE 5.0". Does not hide from Opera 7.0. |
Microsoft Internet Explorer 5.0 and later recognizes "Conditional Comments", i.e. content that is written between SGML comments (like <!-- and -->) and therefore should be ignored by other browsers. See also http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/overview/ccomment_ovw.asp and http://www.blooberry.com/indexdot/html/tagpages/c/conditblock.htm.
| Example: | Hiding technique: | Hides CSS from: | Notes: | |
|---|---|---|---|---|
| Confirmed | Assumed | |||
| A. This text should be green. |
<!--[if gte IE 5]>
|
|
All browsers except the specified MSIE browser version (beginning with IE 5). | The example writes CSS to IE 5 and later. |
| B. This text should be blue. |
<![if !gte IE 5]>
|
|
Any specified MSIE version (beginning with IE 5). | The example writes CSS to all non-MSIE browsers, as well as IE 4 and earlier. Will result in validator errors. |