Home arrow Style Sheets arrow Page 2 - Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes
STYLE SHEETS

Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes


Anyone who creates Cascading Style Sheets knows that Internet Explorer is a headache of a browser to build for because of the way it handles CSS. In this article, Jennifer goes into detail about how to deal with some of the most common bugs, and notes which ones may be fixed in IE 7.

Author Info:
By: Jennifer Sullivan Cassidy
Rating: 3 stars3 stars3 stars3 stars3 stars / 32
January 25, 2006
TABLE OF CONTENTS:
  1. · Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes
  2. · The Use of JavaScript and CSS
  3. · Hiding CSS from Buggy Browsers
  4. · Workarounds for Internet Explorer
  5. · More Workarounds for Internet Explorer

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Cross Browser Issues: CSS Hacks Explained, Tips, Tricks and Fixes - The Use of JavaScript and CSS
(Page 2 of 5 )

There have been some coders that have found a way to incorporate JavaScript into CSS in order to avoid using hacks for browser compatibility.  While this may sound great, there are still several browsers that don’t support JavaScript in the CSS files, like Firefox, Opera, and Netscape.  Fortunately, most of those browsers don’t need to support the JavaScript in the CSS, since the modern versions of them handle CSS rather well.  It’s usually IE you have to worry about, but you are still going to run into a problem for those who disable JavaScript in their browsers.

Still, the following example demonstrates the use of JavaScript in an external style sheet:

document.tags.H1.color = "blue"
document.tags.H1.fontSize = "20pt";

document.tags.H2.textAlign = "left";

This method isn’t new, however. It is really a JavaScript Style Sheet, or JSSS, which was actually submitted to the W3C in 1996 by Netscape as a method of offering an alternative to CSS, which at that time wasn’t widely accepted.   It has severe limitations as far as positioning, but other features like dynamic calculations and conditional processing could be pretty powerful.  JSSS is no longer supported in Netscape 6 and higher.  JSSS style sheets may also seem less friendly than CSS style sheets to users without programming backgrounds.

Multiple Style Sheets for Multiple Browsers

While it may be your choice to only offer a single style sheet for all browsers, you may decide you wish to serve a separate style sheet for each type of browser, especially due to the vast differences in the way browsers support CSS.  In this case, you’ll need a way to detect the visitor’s browser type and version, and then show the correct style sheet based on the results.

A simple JavaScript script can detect a browser type and version, and then choose the style sheet that was written for that browser:

<script language="JavaScript"><!--

version=parseInt(navigator.appVersion); 

if (navigator.appVersion.indexOf('5.')>-1){

version=5

}; 

if (navigator.appVersion.indexOf('6.')>-1){

version=6

};

if (navigator.appVersion.indexOf('7.')>-1){

version=7

};

browser='OTHER';

if (navigator.appName=='Netscape'){

browser='NS'+version;

}

if (navigator.appName=='Microsoft Internet Explorer'){

browser='MSIE'+version;

}

if (navigator.appVersion.indexOf('MSIE 4')>0) {

browser='MSIE4';

}

if(browser == 'NS5'){

browser='NS6'

};

//Internet Explorer

if (browser=='MSIE4') {

document.write('<link rel="stylesheet" type="text/css" href="/css/msie4.css">');

}

if (browser=='MSIE5') {

document.write('<link rel="stylesheet" type="text/css" href="/css/msie5.css">');

}

if (browser=='MSIE6') {

document.write('<link rel="stylesheet" type="text/css" href="/css/msie6.css">');

}

//Netscape

if (browser=='NS4') {

document.write('<link rel="stylesheet" type="text/css" href="/css/nn4.css">');

}

if (browser=='NS6') {

document.write('<link rel="stylesheet" type="text/css" href="/css/nn6.css">');

}

if (browser=='NS7') {

document.write('<link rel="stylesheet" type="text/css" href="/css/nn7.css">');

}

//everyone else

if (browser=='OTHER') {

document.write('<link rel="stylesheet" type="text/css" href="/css/other.css">');

}

// --></script>

Obviously, you will have to create a separate style sheet for each browser in your script.


blog comments powered by Disqus
STYLE SHEETS ARTICLES

- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...
- CSS3 Animated Image Gallery: Transitions
- CSS3 Properties: Fixed Heights with box-sizi...
- CSS3 Properties: Altering Strokes and 3D Eff...
- CSS3 Properties: Text-Stroke
- CSS3 Transitions: Width and Height Properties
- Creating a Drop Down Menu in CSS3
- Intro to CSS Transitions

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials