2004-08-13 (Friday, August 13, 2004)
Update
If I stick with this approach, someday it would be nice to have a list of safe CSS statements & declarations for each layer. For now, quirksmode.org has a general compatibility table. Thanks, mr. Koch!
2004-08-12 (Thursday, August 12, 2004)
In the beginning
In a previous article, I described a few strategies for coping with all the CSS bugs in browsers to minimize the headaches associated with creating CSS for real browsers, as opposed to the "ideal" standards-compiant ones we're all secretly pining for. That was then. I've since had time to learn a bit more about my methods and reflect on them a bit. As of this writing, I have not yet actually deployed this strategy anywhere, so if you do, please let me know how it works for you, or what you think.
The basic strategy hasn't changed much, only the methods. I'm working on a layered approach that effectively hides the "fancy" CSS from older, buggy browsers that are prone to crashing, or rendering a messy or, worse, unreadable page. My methods, however, have changed. I'm trying to Keep things simple (Hat tip to Peter-Paul Koch), and at the same time keep the content of the pages accessible, while simultaneously taking advantage of recent standards available in the newer browsers. The goal is to have pages that can be read in any browser, but look better in the most common, standards-compliant ones.
The Levels
Basically, there are 3 levels of "style":
Level 0: Unformatted HTML (or XHTML)
Unstyled, boring - but still readable. This is the foundation of your document. Some users can disable style sheets, impose their own, just don't support them, or don't use them (screen readers, etc.). So, even without your style sheets, the basic document should follow in a logical order, and be readable.
For this level, I generally test in Netscape 4 (why? it will make sense later).
Level 1: Typography (basic styling)
No layout, but the text might be easier to read. Mostly font face, size, spacing, etc. This will be incoporated into the next layer, but you can still make it visible to some "intermediate" browsers - the ones that might not be able to handle the layout, but can do text without crashing, or wrecking the page. Not that this has it's own challenges, particularly if you're trying to specify text-sizes in an accessible manner, but I feel that more browsers support this application of CSS sufficiently that it should be available to them. The page may not look perfect, or exactly the way you dreamed, but again, it's readable, and has a bit of extra "panache" over the plain, unstyled document.
For this level, I generally test in Internet Explorer 5 (Mac and Windows).
Level 2: CSS-based layout
The ultimate in CSS expression. This is where the vast majority of browsers have major bugs and problems, and where the most care must be taken to avoid messy pages.
I generally test in Mozilla (and Safari) and Internet Explorer 6 for this level.
The Browsers
I will first try to briefly describe which browsers I am concerned with, and which ones I want to "exclude" from the advanced CSS layout.
"Current Browsers" (at least, the ones I can test in, or want to worry about)
These are the most common, newest, most standards-compliant browsers that can at least deal with CSS-based layouts, even with a few minor bugs or quirks. There are also a couple legacy browsers that I just haven't yet figured out to shield from the 2nd level of CSS.
- Internet Explorer 6 Windows (IE6Win)
- Still the single most popular browser on the planet, much to the collective frustration of web designers. Microsoft recently announced there will be no new development on Internet Explorer as a free, stand-alone browser (v7 will come with the next OS, whenever that is). With any luck, it will die.
- Internet Explorer 5.5 Windows (IE5.5Win)
- Not nearly as common as IE6Win, but I'm not 100% certain how to shield this browser without also blocking IE6Win.
- Mozilla (Moz)
- A very good browser, as of this writing. I test in the the most recent version, and I generally assume that it behaves pretty much the same on all platforms.
- Safari - for Mac OS X
- This is the deFacto browser in Mac OS X (although I sometimes use Mozilla). After adopting the same strategy that has led to the dominance of Internet Explorer in Windows (bundling it with the Operating System), it has effectively taken over the Mac share of the browser market. It is based on the Linux browser Konquerer.
There are, of course, a plethora of other browsers out there. But, I don't want to spend my life testing in them. I don't expect them to comprise a significant portion of the people visiting my site, and until that changes, I will hope that they either don't get to mess with my CSS layout, or that they don't mess it up too much.
Other Browsers
A few more browsers of note:
- Netscape
- The Netscape browser is based on (& originally gave birth to) the open-source Mozilla Project. I assume that if it works in the latest major release of Mozilla, it works in the equivalent version of Netscape.
- Netscape Navigator 4
- Ah yes, the old dinosaur. This used to dominate the browser market, back in the early 1990s. Not anymore. It's CSS support is piss-poor and I don't trust it as far as I can throw it. It handles HTML well enough, though, so people using it should be allowed to read my pages, even if they're missing out on the party. If I've added a few styles visible in my collector ("level 0") style sheet, I test to be sure that's safe in Netscape 4. After that, I don't think about it anymore.
- Internet Explorer 5 for the Mac
- This is a dead browser, let it die. Microsoft has officially announced there will be no new development of this browser, now that Safari has officially taken over on Mac OS X. They will even discontinue support by 2005. Thankfully, Safari is an excellent browser, and much better than IE5Mac. In the words of Peter-Paul Koch (who's articles I've been reading a lot lately), Internet Explorer 5 for Mac is a "bug ridden crash prone piece of junk". It is still used in the venerable Mac OS 9, but then, so is Netscape 4, and that whole OS is fading out slowly anyhow. OS 9 won't run the majority of newer browsers, anyway, so it's not like they could upgrade, even if they wanted to. Code for it if you want, you'll probably need to use the commented backslash hack. In this scheme, it gets to see "Level 1".
- Opera
- Great, small, relatively obscure, and not free. It has the advantage of being small and light enough to run smoothly on older machines, with excellent CSS support. I don't wanna pay for it, but more power to you if you use it.
- Konqueror
- Part of the K-windows environment on Linux. Don't know much about it, and can't test on it. I hear Safari for Mac is based on much the same architecture, so I hope it's not too different.
For some professional insight, see these on-line articles:
- Browsers (on quirksMode.org)
- I'll fill this in more when I have time
The Methods
Finally, the application of what I've been talking about this whole article.
The @import "shield"
Nothing new here from my previous article. I use an @import statement that looks like this:
@import url("level1.css");
This hides the Level 1 style sheet from the following browsers, thus "shielding" them from it (source: http://w3development.de/css/hide_css_from_browsers/) :
- Netscape 4.x
- Win IE 3
- Win IE 4 (not 4.72)
- Mac IE 4.01
- Mac IE 4.5
- Konqueror 2.1.2
- Win Amaya 5.1
I haven't confirmed it yet, but it appears that IE Win 4.72, which isn't blocked by this statement, behaves much like IE 5 - it is blocked at the next level, and the simplified Modified Box Model Hack works as expected. It uses the non-standard box model, and parses CSS in a similar way to IE 5. This is good, because it means we can essentially treat IE 4.72 & 5 in the same way, hopefully without any big surprises.
By adding a comment after the @import, the Level 2 style sheet can be shielded from a few more browsers:
@import/* */ url("level2.css");
This hides the Level 2 style sheet from:
- Win IE 5 and lower
- Mac IE 4.01
- Mac IE 5
Notice that Mac IE 5 is now shielded, as well as "Win IE 5 and lower", but I don't know if this includes Win IE 5.5 - I really should confirm this someday.
I will point out that these import statements could be used in one of two ways:
- In a primary (level 0) style sheet that is linked to html documents with the
<link>
tag. This linked, level 0 style sheet will be visible to many older browsers, so it could act as an extra layer (level 0.5?), but these browsers are still shielded from the imported sheets. The linked style sheet effectively acts as a collector for the other layers of CSS. - directly in a
<style>
tag in the html document. This requires a few html comments inside the tag, for older browsers that don't support styles.
I prefer the <link>
tag, because you can also have some fun with media-dependent sheets (IE Windows won't
import a style sheet if you specify a media in the @import
statement, eve if it's "screen
"),
and alternate style sheets that are not practical if the sheets are imported directly from the html document. Your preferences,
experience, or requirements may dictate otherwise.
For example, CSS Zen Garden uses a statement like the following in their page:
<style type="text/css" title="currentStyle">
@import "/001/001.css";
</style>
This allows them to use a cgi or javascript (I don't know exactly how) to dynamically change which style sheet is loaded into the page, based on a link the user clicks on. Pretty neat, hugh? You're site may not need this, however.
With this approach, I don't use vey many hacks (except for that second @import statement), since the main browsers that can actually see my CSS should be stable enough that I don't have to worry too much about browser-specific behavior. Except for Internet Explorer for Windows (v 5.5? and 6).
Coping with Internet Explorer
Internet Explorer has it's own set of bugs and unsupported CSS, although version 6 for windows is the best of the lot. Until it dies, I feel it is important to ensure it can display a decent page.
As part of my crusade against IE, I will take this opportunity to remind to that if you are still using Internet Explorer to browse the web, please consider an alternative.
There are a few ways of doing this. Most notabley, there are the various incarnations of the "Box Model Hack". I don't like the original, but have been trying out the "modified Simplified Box Model Hack" (mSBMH) . You can also use conditional comments.
With any version of the box model hack, corrections for IE 5 (&4.72 Win) & 6 (& 5 Mac) are included along with the "standard" declarations. This can be easier for the process of tweaking or de-bugging. On the downside, this also means every other browser is also downloading extra statements that they don't need. There is also no safe bet that some other browser in the future will parse CSS the same way IE currently does, causing the hack to misfure when it shouldn't. Highly unlikely, but still...
Conditional Comments, on the other hand, can achieve a similar effect: feeding specific CSS rules to only certain versions of IE. Using this method, the corrections are not in the main style sheet that every other browser is downloading. This could be a small pain if you are changing things often, and need to adjust both sets of CSS, but for and "deisgn and leave" site, this won't be a problem. Conditional comments also have the advantage that it is a very safe bet that their contents will never be visible to any browser, except the one targetted by the comment.
Conditional comments only work in an html file, and not a CSS file. So, you could either use them to hide one or two corrections
in a <style>
tag in the header, or if you have a lot of statements, use a <link>
tag
instead to link to a style sheet with the corrections only for that version of IE.
For example:
<!--[if IE]>
<link href="ie-only.css" rel="stylesheet" type="text/css" />
<![endif]-->
In theory, I envision the design process something along these lines: First, get a stable design for standards-compliant
browsers. Use the modified SBMH in the design phase to add whatever you need to get it to work in IE5 and 6, and modify as
you go. Once it's all working, just move all the selectors with * html
you added using the mBMH, to a separate
style sheet ("ie-only.css
"). You might want to leave them in the Level 1 style sheet, because IE 4.72
does not support conditional comments. Then, link that style sheet to the html documents within a conditional comment, as
above.
While you're at it, you could remove all the *
's in front of each selector. It's just extra info, and they're
no longer necessary, since the "ie-only.css
" style sheet is now only visible to IE 5 and up, because
of the conditional comments. Keep the html
's in there, since this will ensure higher specificity so the rules
get applied, no matter when you load them. Taking out the *
's also ensure that, in case future versions of IE
fix the selector "bug", but still can't handle the main css properly, the page will not get mangled, as it will
see the same CSS as IE 6. If it's safe for IE 6, chances are it will be safe for IE 7, right? I hope so, too.
Summary
The strategy consists of the following layers:- Level 0 (unstyled) raw HTML or XHTML - safe for non-CSS browsers, and Netscape 4
- Level 1: Typography, basic CSS - safe for IE 4.72 - IE 5 (5.5?) Win & Mac
- Level 2: CSS Layout and newer standards - safe for standards-compliant browsers such as Mozilla, Safari, and even IE6
- Level 1: Typography, basic CSS - safe for IE 4.72 - IE 5 (5.5?) Win & Mac
- @import, and commented @import to "shield" browsers of various grades, thus creating the 3 layers.
- Commented Backslash hack against IE 5 Mac, if you need it for Level 1.
- The modified Simplified Box Model Hack, for the design & testing phase, or if you need it for Level 1.
- Conditional Comments against Internet Explorer Windows (the versions that aren't blocked by the @import "shield").
That's it. Fewer headaches for the amateur web designer (like me!)
Related Links
- Peter-Paul Koch's "Keep it Simple" Column
- Browsers (on quirksMode.org)
- CSS Hacks (quirksMode.org)
- Web Development: Best Practices
- MacIE5 CSS Hack (aka "Commented Backslash Hack")
- The Modified Simplified Box Model Hack
- Hide CSS From Browsers (w3development.de)
- Hide CSS From Browsers
- Microsoft: About Conditional Comments
- more to come...