Hide CSS from browsers

Here are examples of various methods which let you hide CSS from buggy browsers. Please send comments and report errors to Christian Jacobsson,

.

1. CSS tricks relying on lack of CSS support in browsers.

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.

Example: Hiding technique: Hides CSS from: Notes:
Confirmed Assumed
A. This text should be red. <style type="text/css"><!--
@import url(hidefromie6.css) screen;
--></style>
  • Netscape 4.06
  • Netscape 4.79
  • IE 4.72/win
  • IE 4.01/mac
  • IE 5.0/win
  • IE 6.0/win
  • Netscape 4
  • IE 4/win
  • IE 5/win
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"><!--
/* style sheet */
--></style>
  • Netscape 4.06
  • Netscape 4.79
  • Netscape 4
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"><!--
@import "hidefromie4.css";
--></style>
  • Netscape 4.06
  • Netscape 4.79
  • IE 4.72/win
  • IE 4.01/mac
  • Netscape 4
  • IE 3
  • IE 4
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>
  • Netscape 4.06
  • Netscape 4.79
  • IE 4.72/win
  • IE 4.01/mac
  • Netscape 4.x
  • IE 3
  • IE 4
Hides CSS from a particular tag in the HTML source.
E. This text should be orange. <style type="text/css" media="sc&#82;een"><!--
/* style sheet */
--></style>
  • Opera 5.12
  • Opera 6.0
  • Opera 7.0
  • Opera
(from html.conclase.net).
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}
  • Netscape 6.1
  • Mozilla 1.2
  • Mozilla
(from richinstyle.com).
It seems the comment must be placed between an element and a class (or id) selector.

2. Browser sniffing using JavaScript object detection.

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).

Example: Hiding technique: Hides CSS from: Notes:
Confirmed Assumed
A. This text should be red.

<script type="text/javascript">
<!-- 
if(document.getElementById) 
document.write('
<style type="text\/css"> 
p {margin: 0;} 
<\/style>');
//-->
</script>

Red text: keep all this on 
one line in real javascript

  • Netscape 4.06
  • Netscape 4.79
  • IE 4.72/win
  • IE 4.01/mac
  • Netscape 4
  • IE 3
  • IE 4
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.

<script type="text/javascript">
<!-- 
if(!(document.getElementById&&!document.all)) 
document.write('
<style type="text\/css"> 
p {margin: 0;} 
<\/style>');
//-->
</script>

Red text: keep all this on 
one line in real javascript

  • Netscape 6.1
  • Mozilla 1.0
  • Mozilla 1.2
  • Opera 5.12*
  • Opera 6.0*
  • Netscape6+
  • Mozilla
*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.

3. MSIE Conditional Comments.

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]>
<style type="text/css">
/* style sheet */
</style>
<![endif]-->
  • IE4.72/win
  • IE5.0/win
  • IE6.0/win
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]>
<style type="text/css">
/* style sheet */
</style>
<![endif]>
  • IE4.72/win
  • IE5.0/win
  • IE6.0/win
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.

<<Back