Chomper Stomping
jQuery/JavaScript/CSS 3/HTML 5, Java/PHP/Python/ActionScript, Git, Chrome/Firefox Extensions, Wordpress/Game/iPhone App Development and other random techie tidbits I've collected



programming concepts

March 19, 2008

Stylesheets and Alternate Stylesheets, how including external CSS works

It really helps to read the documentation. Way back when I started designing websites using external stylsheets, I found a little trick. If you had IE specific styles and Firefox specific styles, you could extract them both out, and include them both like this:

<link href=”ffStyles.css” rel=”stylesheet” type=”text/css” title=”Firefox Styles” />

<link href=”ieStyles.css” rel=”stylesheet” type=”text/css” title=”IE Styles” />

Since you are (in this case) telling the browser that you have two styles, and both are different (you used a title attribute), but both should be default (you used the “stylesheet” parameter for the “rel” attribute) the browser doesn’t know what to do. You’ve told it only to use one, but that both are the default, and thus, have contradicted yourself.

At this point, IE and FF both make very different (but helpfully so) decisions. I’m betting that IE said, “well, these are cascading, so in cascades you always take whatever the last thing is and use that” and therefore IE used the last style as the default and ignored the first. Firefox either said “IE uses the last one, so I’ll use the first one so people can use the trick Chris is talking about above” or they said “Ok, looking for stylesheet… found it! Use it! Oh, another one… um, we don’t need anymore because we already have one, so ignore it” either way it doesn’t really matter, because the point is, it works out nicely for the developer.

I always thought this was a “bug” that I was exploiting, and that my dual stylesheet was an exploit/hack. Then I read the documentation today for the first time and learned how stylesheets are supposed to work and be included. The major thing I found out was that the title attribute is purely optional, and serves a double purpose:

It clearly states in the documentation not to put a title attribute if you want it to be “persistent” (universal) but if you want it to be “preferred” (default) to set the title attribute and set the rel type as “stylesheet”. It doesn’t say anything about being able to have two stylesheets both with the rel attribute set to “stylesheet”, but it does make it appear as if you are not supposed to have more than one with the rel type of “stylesheet” (at least that’s what I’m inferring from the fact that they are giving you a way to make “alternate” stylesheets). This actually makes sense, because if they give you a way to have “preferred” and “alternate” stylesheets, why would you make two preferred? What would that even mean? I’m guessing the way they intended was something like this:

<link href=”basicStuff.css” rel=”stylesheet” type=”text/css” />

<link href=”blueStuff.css” rel=”stylesheet” type=”text/css” title=”blue theme” />

<link href=”redStuff.css” rel=”alternate stylesheet” type=”text/css” title=”red theme” />

This way, all of the stuff that is universal to both themes just goes in without a title tag, because the only purpose of the title tag is so you can switch between them, and why would you want to switch the universal stuff (you wouldn’t)? So, by putting a title, you are implying that the user should be able to see and choose between your titled styles, and by putting “stylesheet” instead of “alternate stylesheet” you are stating that “this one is the one you should see by default”. So in this example, the blueStuff.css style would be used by default (for both IE and FF) and the redStuff.css style would be an available alternate.



About the Author

Christopher McCulloh
E-Commerce developer at Finish Line Co-Author of HTML, XHTML and CSS All-in-one Desk Reference for Dummies Graduated from IU with a Bachelors of Media Arts and Science and a Certificate in Applied Computer Science. Tech Editor for Building Facebook Applications for Dummies and Building Websites All-in-one for Dummies 2nd Edition. Creator and maintainer of the Status-bar Calculator Firefox Extension Three years professional experience in Java E-Commerce Development and four years professional experience with PHP for a combined total of seven years professional JavaScript/HTML/CSS experience




 
 

 
mysqlerror

WP phpBB Bridge: Warning: mysql_set_charset() expects parameter 2 to be resource, boolean given

Warning: mysql_set_charset() expects parameter 2 to be resource, boolean given in wp-content/plugins/wp-phpbb-bridge/inc/widgets/wpbb_topics_widget.php on line 149 This is an error caused by the fact that the WP phpBB Bridge pl...
by Christopher McCulloh
0

 
 
 

Events Calendar Pro Nav Formatting Messed up on Empty Calendar

The Events Calendar Pro (from http://tri.be/) has a few problems. If you are trying to figure out why a calendar with no events in that month has completely screwed up header navigation, just put this line of code inside of tab...
by Christopher McCulloh
3

 
 
warning

OH SHNIKES, WE’VE BEEN HAXORED!!!

Yes. It finally happened. After… 6 years? on the web I finally got hacked. Two domains affected: http://cmcculloh.com http://hallelujahbutton.com (this also of course affected all sub-domains of cmcculloh.com, such as blo...
by Christopher McCulloh
1

 

 
blue-xl

WordPress Settings API – Adding Options to Existing Page

Adding new options to an existing page in the dashboard in wordpress can be maddening. I’ve literally spent 15+ hours dealing with this horrible API at this point. To the point where I wrote two different wrappers for it....
by Christopher McCulloh
0

 
 
custom_ratings

Teaser of things to come…

Lots going on at ChomperStomp right now. I’ve been up to my eyeballs in work and in babies (3 month old and 2.5 year old). Here’s a little teaser for something big I’m working on: That’s right, custom us...
by Christopher McCulloh
0

 




0 Comments


Be the first to comment!


Leave a Reply

Your email address will not be published. Required fields are marked *

*


− 6 = one

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>