Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Assigning browser-specific styles

Rated 3.89 (Ratings: 9) (Add your rating)

Log in to add a comment
(6 comments so far)

Want more?

  • More articles in Code
  • More articles by ppk
 
Picture of ppk

Peter-Paul Koch

Member info | Full bio

User since: September 12, 1999

Last login: September 03, 2005

Articles written: 8

As is well known, most browsers have their own, highly idiosyncratic interpretation of CSS, thereby making the life of us web developers more difficult. In practice, it is especially important to make a distinction between Netscape 4 and all other browsers. This article discusses a simple way of doing just that.

Of course you can write a browser detect and then document.write the correct <LINK> tag into the document, but personally I find this solution cumbersome and boorish, especially when we have an excellent alternative.

Styles not for Netscape 4

First for the simple part: how to disable a style for Netscape 4. The technique hinges on the fact that Netscape 4 does not support the @import declaration by which you import a second style sheet. Since Netscape 4 doesn't see the second sheet at all, you can concentrate your dangerous styles (borders, especially) in this sheet.

@import url(extra.css) imports the extra style sheet extra.css into your normal style sheet.

Please note that, while Netscape 6 does support @import it requires the declaration to be the very first in the style sheet. This is a correct (though tedious) implementation of the CSS specs. The advantage is that if you wish, you can restrict certain styles to Explorer by placing the @import later in the regular style sheet.

So far this is easy, now for the harder part.

Styles only for Netscape 4

By a judicious use of @import it is also possible to define style sheets for Netscape 4 only, the exact reverse of what we did before. Let's take a common example: we want our fonts to be exactly matching in Netscape and Explorer. While a 12px font looks perfect in Explorer, it is too small in Netscape 4. We'd rather give this browser a 13px font size.

Now if we do this in our normal style sheet:

@import url(extra.css)

p,td,li {
 font-size: 13px;
}

and this in extra.css:

p,td,li {
 font-size: 12px;
}

it doesn't work. Imported styles are less specific than the styles in the regular sheet that imports them. This means that the browsers (except for Netscape 4) read out the 12px font size from the imported style sheet but overrule it in favour of the 13px declaration in the regular style sheet. So your fonts are too large in Explorer.

Now we get to the trick. The trick is to give your body tag a class:

<body class="ie">

Change the imported extra.css to

body.ie p,
body.ie td,
body.ie li {
 font-size: 12px;
}

Now it works perfectly! The trick is that the 12px style in the imported style sheet is now more specific than the 13px definition in the regular style sheet. After all, the 12px style gives a rule for elements inside an element with a certain class, while the 13px style only works on elements in general. Thus the 12px style overrules the 13px style, except in Netscape 4 which never sees the 12px style.

Simple and efficient, no need for browser detects.

Peter-Paul Koch is a freelance browser expert and JavaScript guru living in Amsterdam, the Netherlands. He has been an Internet professional only since 1998, so he's definitely second generation.

His personal site is www.quirksmode.org. It includes the W3C DOM Compatibility Tables, currently the best resource on the Internet for this subject. Because of this research, he has been asked to co-edited chapters 17 to 19 of Flanagan's "JavaScript, the Definitive Guide", O'Reilly, 4th edition.

He is an administrator of the WDF-DOM mailing list, that counts most international JavaScript gurus among its members.

He has written the "Keep it Simple" column on Digital Web Magazine, as well as articles on A List Apart, Apple Developer Connection, and O'Reilly's Web Dev Center, in addition to Evolt.

There's always more than one way to do it...

Submitted by volkow on August 9, 2001 - 08:38.

Of course one may use the "@import" model to solve one's cross-browser problems, but if it is only needed to sort out Netscape 4.xx among other visitors, you can use the trick with the "disabled" attribute of the which is not supported (read-ignored) by Netscape Communicator 4.xx.



This way there's no need to worry about less specific rules or anything-the order is simple: IE and newer browsers read the first included css, and then stop at the last one, as the attribute "disabled" holds the broser from applying it.

NC is applying first css, then second one, not caring if there're is "disabled" attribute or not. The key is in synchronised use of the elements-both style-sheets should have absolutely the same rules, just with the different sets of properties.

This solution is simplier than using javascript to sniff the version, and uses natural flaws in the browser rendering and processing mechanisms.

login or register to post comments

The actual code, eaten by the comments parser

Submitted by volkow on August 9, 2001 - 08:41.

<link rel="stylesheet" type="text/css" href="/normal.css">
<link rel="stylesheet" type="text/css" href="/nc.css" disabled>

login or register to post comments

even another option ;-)

Submitted by checkmate on August 9, 2001 - 10:20.

i prefer to rely on a combination of these methods
@import "/good.css";


it does the same thing as the disabled method (allows you to not have to worry about using more specific selectors in your good.css file), so YMMV

login or register to post comments

they need a preview on this thing ;-)

Submitted by checkmate on August 9, 2001 - 10:23.

here's the actual code: <style type="text/css">@import "/good.css";</style> <link rel="stylesheet alternate" type="text/css" href="/netscape4.css">

login or register to post comments

My take on the browser specific style sheets issue

Submitted by Realitystorm on March 5, 2005 - 04:36.

Basic CSS1:
<link href="/_interface/basic_css1.css" rel="stylesheet" type="text/css" />
Standard CSS2:
<!--[if IE]><![if gte IE 7]><![endif]--><style type="text/css">/*\*/ @import url("/_interface/standard.css") all; /**/</style><!--[if IE]><![endif]><![endif]-->
WIN IE CSS2
<!--[if lt IE 7]><link href="/_interface/win_ie.css" rel="stylesheet" type="text/css" /><![endif]-->
MAC IE CSS2:
<style type="text/css">/*\*//*/ @import url("/_interface/mac_ie.css"); /**/ </style>
Netscape 4.x CSS2:
<script type="text/javascript">if(document.layers) {document.write(unescape('%3CLINK%20href%3D%22/_interface/netscape4x.css%22%20rel%3D%22stylesheet%22%20type%3D%22text/css%22%20/%3E'));}</script>
For details see:
http://www.queerminds.com/realitystorm/memory/internet/css/browser_specific_style_sheets.cfm
Oh, and it validates... :-)

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.