Before you start coding your site using CSS for specific browsers, here are some tips that hopefully will make your life easier.
1.
Design From Scratch
Most webmasters who have had to convert their existing website to CSS say that they find it easier to design their site from scratch in CSS than to try to find a way to reproduce their old layout in CSS.
Another reason for designing from scratch is that CSS allows you to do many things not possible using the old tables paradigm. Rethinking your entire design allows you to take advantage of the new possibilities.
2.
Code to Follow the CSS Standards First
Many of the web designers hanging out in webmaster forums have found that it is far easier to develop their CSS code for a highly standards-compliant browser like Firefox first, and then only later add the workarounds to make their code work on IE, than to code for IE and then try to make it work for Opera, Konqueror, Safari and Firefox.
It is also logical to write for a more standards-compliant browser first: sooner or later, Microsoft is bound to issue a newer version of IE that will have the existing CSS bugs fixed. When they do so, all you have to do is to remove the workarounds which you created and you're done. If you write your main style sheet with styles that are coded in a non-standard way to deal with IE bugs first, you will wind up having to rewrite everything when Microsoft fixes the bugs.
This is not to say that Firefox does not have its own share of CSS bugs. As such, my personal recommendation is to code with the two (supposedly) most standards-compliant browsers first, ie, Firefox and Opera, while at the same time periodically making sure that your CSS code validates with the W3 Consortium's free style sheet validator. You can add the workarounds for IE later.
(If you are not sure what validation means, or how you can get it done, check out my article on HTML and CSS Validation: Should You Validate Your Web Page? at http://www.thesitewizard.com/webdesign/htmlvalidation.shtml
3.
Use External Style Sheets and Take Advantage of the "Cascading" Aspect of Cascading Style Sheets
One way to handle the bugs and omissions existing in different browsers is to put all your standards-compliant CSS code in a separate (external) style sheet file that is loaded by every browser.
Then, when you find that a specific browser requires a workaround, use the methods listed below to load an additional style sheet designed specifically for that particular browser. The beauty of CSS is that you can redefine a different style for the same selector and have the last-defined style override all preceding definitions.
Placing your main standards-compliant style sheet and the browser-specific style sheets in different external files allow you to simply remove a particular file in the future should that version of the browser cease to be used.
4.
There's No Substitute for Testing
Even when you have validated your CSS code, you should still test your code manually using different browsers. There is no substitute for this: just because your code is standards-compliant does not mean that the browser will render it the way you want it to (or even correctly for that matter).
You can find information on how you can install multiple versions of different browsers on a single machine from http://www.thesitewizard.com/webdesign/multiplebrowsers.shtml
Including or Excluding Style Sheets for IE 5+
One of the easiest things to do is to specify that a certain style sheet be loaded only by IE 5, 5.5, 6 (and later versions) and be excluded from those versions of IE.
Microsoft provides a non-standard extension that allows you to detect those versions of IE, and include or exclude code depending on those versions.
To cause (say) a CSS file like "iespecific.css" to be loaded by IE 6 and not other browsers, use the following code in the HEAD section of your web page:
Likewise, for any version of IE 5 (including 5.0, 5.01, 5.5, etc), use the following:
Detecting minor versions of IE 5 is a bit more tricky. The moment you specify a minor version, you are expected to get the whole version number correct. For example, to detect the release build of IE 5.5, you will need the following code:
The test will fail if your visitor has a version of IE 5.5 that not have that exact version number (for example, if he/she has updated IE 5.5 with one of the service packs).
To make life easier for webmasters who need to test for a whole range of versions of IE, you can use the comparison operators "lt" (less than), "lte" (less than or equal), "gt" (greater than), and "gte" (greater than or equal).
For example, to test for all versions of IE greater than or equal to version 6, you can use
The above code examples work because a normal browser sees the entire block as HTML comments since they are enclosed within "". IE 5 or above will however attempt to parse the block to see if it has instructions specific to it.
You can also exclude a certain style sheet using this method. For example, to exclude the CSS file "not-ie.css" from IE 6, use:
Notice the operator "!" immediately preceding "IE". This is the NOT operator, causing the statements following to be used only if the expression "IE 6" is not matched by the browser.
Again, the above code works because a normal browser will interpret "" and "<[endif]>" as HTML tags that it does not recognise, and ignore them. This code, however, will not validate as correct in a HTML validator, since it does not use valid HTML tags.
Note that you can also test for IE without specifying a version number. For example, the following loads the style sheet only if the browser is not IE 5 or above:
Microsoft's documentation for this non-standard feature can be found at http://msdn2.microsoft.com/en-us/library/ms537512.aspx
Since the documentation does not specify that these features apply only to the Windows versions of IE, I assume that they also apply to the Macintosh version. I'm not able to verify this though.
How to Hide CSS from IE 5 and Below for Windows
If you place a comment immediately after a selector in a style sheet, IE 5 and earlier on Windows will ignore that selector.
In the following example, "Large text anyone?" will not be enlarged in IE 5 and earlier.
...(etc)...
Large text anyone?
Although some web designers claim that this trick will also hide the style for the selector on IE 4.01 for Macintosh, I found that IE 4.01 on a 68k Mac displays the text correctly.
In general, IE for Macintosh uses a different source code base from IE for Windows. Hence bugs in the Windows versions do not imply bugs in the Macintosh variety and vice versa.
Hiding Styles from IE 3 for Windows
If you use the @import rule to load an external style sheet as in the following example, IE 3 for Windows will not load it.
One side effect of this is that Netscape 4 will also not load the referenced style sheet. According to some webmasters, certain versions of IE 4 for Windows is affected in the same way if the style sheet is located in a different directory from the calling HTML file. Since I no longer have IE 4, I'm unable to test this claim.
How to Hide Cascading Style Sheets from Netscape 4
Apart from IE, the other browser that you will probably want to specifically code for, to work around bugs, is Netscape 4. It is probably the only old browser from the early-CSS era that is still being used today (to the anguish and frustration of most CSS-using web designers). Other browsers from that era (such as IE 4 and earlier) are thankfully so rare that most webmasters (like me) pretend they don't exist.
Like other browsers of that period, Netscape 4 is replete with bugs in its CSS implementation. Fortunately, it is easy to exclude styles from Netscape 4. There are a few well-tested methods to do this, all of which take advantage of bugs in the browser's CSS implementation. They are known to work in all Netscape 4.X versions up to and including 4.80, which is the most recent 4.X version at the time I write this.
I will list the two most commonly used methods. They are so widely used on the Internet that I doubt AOL/Netscape will slip in a bug-fix for these in one of their security fixes (assuming they even bother to maintain the 4.X series anymore).
a. Using the @import Rule
As mentioned earlier, using the @import rule to load an external style sheet will cause that style sheet to be ignored in Netscape 4.
For example, "not-netscape4.css" will not be included in the following rule:
The side effect of using this rule is that IE 3 for Windows will also not load the style sheet. This is not necessarily bad, since IE 3's support for CSS is even buggier than Netscape's. It may, however, also exclude your style sheet from certain versions of IE 4 for Windows.
b. Using the Media Attribute in the Link Tag
If you link to your style sheets with a media attribute other than "screen" using a line like the following, Netscape 4 will not load them.
Even the following line will not work in Netscape 4:
Netscape 4 only loads the style sheet if you either omit the media attribute or use "screen" as the value for that attribute.
As far as I can tell, there are no side effects to using this method.
Conclusion
While this list of hacks and workarounds is by no means exhaustive, I found the above to be the most useful and having the least number of side effects. With this bag of tricks in your arsenal, you should be able to create a site that works fairly similarly under the browsers being used today.
No comments:
Post a Comment