Forum Moderators: not2easy
I was trying to do things according to the 'specs' (W3C), but all browsers appear to do the opposite, so I'm really just after a bit of clarification.
The W3C states:
http://www.w3.org/TR/html4/present/styles.html#h-14.3
If two or more META declarations or HTTP headers specify the preferred style sheet, the last one takes precedence.
However, all browsers use the first one! 'A List-Apart' article from years ago talks about using the first one as well... has the spec changed, or have I read it wrong?!
<link rel="stylesheet" href="/styles/red.css" type="text/css" title="Red Style" />
<link rel="stylesheet" href="/styles/green.css" type="text/css" title="Green Style" />
<link rel="stylesheet" href="/styles/blue.css" type="text/css" title="Blue Style" />
As I read it, the spec states that "Blue Style" should take precedence (the last one). But ALL browsers show the "Red Style". IE8, FF and Opera effectively treats the 2nd and 3rd stylesheets as 'alternate' and offers all 3 as alternative styles from the menu.
The spec does talk of this situation (muliple preferred stylesheets), however, I think in the real world it is an error and can be avoided... The 2nd and 3rd should be 'alternate stylesheet', leaving just 1 preferred (red.css) stylesheet IMO. (The code I'm writing is a generic PHP class that builds stylesheet elements and returns the 'preferred' one. I was considering just throwing an error if more than 1 'preferred' stylesheet was encountered - for the same media.)
The author may specify that one of the alternates is a preferred style sheet.
The quote you gave above has to be seen differently: there is a way to specify preferred style sheets using <meta> tag s(not <link> tags, and/or using HTTP headers (outside of the html document).
BTW: The standard also states:
If two or more LINK elements specify a preferred style sheet, the first one takes precedence.
But why you would add multiple preferred style sheets is really beyond me.
As you said: get one preferred and make the rest alternates.
Just to reiterate, if we have...
<meta http-equiv="Default-Style" content="Red Style" />
<meta http-equiv="Default-Style" content="Green Style" />
<meta http-equiv="Default-Style" content="Blue Style" /> <!-- TAKES PRECEDENCE -->
<link rel="stylesheet" href="/styles/red.css" type="text/css" title="Red Style" />
<link rel="alternate stylesheet" href="/styles/green.css" type="text/css" title="Green Style" />
<link rel="alternate stylesheet" href="/styles/blue.css" type="text/css" title="Blue Style" />
If two or more META declarations or HTTP headers specify the preferred style sheet, the last one takes precedence.
The last META 'Default-Style' declaration takes precedence (not LINK element, as I managed to read it) - that makes sense! The "Blue Style" should win in this case, despite the arrangement of LINKs, since the META has precedence over the LINKs.
Thank you very much!
Whilst we're on the subject, what is browser support like for the META 'Default-Style' declaration?
Whilst we're on the subject, what is browser support like for the META 'Default-Style' declaration?
Oh dear, not very good it seems (or at least does not work as I expect/hoped), and I'm testing the very latest versions of the browsers. I would have hoped that the 'Default-Style' META tag would set the stylesheet for the document, period. Regardless of whether styles were declared 'alternate' or not.
EXAMPLE:
<meta http-equiv="Default-Style" content="Blue Style" />
<link rel="stylesheet" href="/styles/red.css" type="text/css" title="Red Style" />
<link rel="alternate stylesheet" href="/styles/green.css" type="text/css" title="Green Style" />
<link rel="alternate stylesheet" href="/styles/blue.css" type="text/css" title="Blue Style" />
Expected result is that the "Blue Style" is used.
FF3 and IE8 - Work OK. Setting the META tag sets the stylesheet for the document. In the above example, the "Blue Style" is used (an 'alternate' stylesheet as indicated in the LINK element).
Safari4 and Chrome3 - Only partly works IMO. The META tag only works if the stylesheet LINK element, referred to by the META tag, is declared as preferred ie. NOT 'alternate'! In the above example, NO STYLESHEET is used! Even though the "Red Style" should take precedence if taking just the LINK elements into account, it is not used. As far as Safari and Chrome are concerned, the META tag is referring to an invalid 'alternate' style and literally gives up! In these browsers the META tag is only useful to pick the preferred stylesheet out of a list of stylesheets already declared as 'preferred', to override the browser default of picking the first one!
Opera10 - Seems to ignore the META tag completely! The order of the LINK elements decides. (And I thought Opera was the most standards compliant browser on the planet?!)
Setting the HTTP header makes no difference. Besides, the META tag should take precedence over the HTTP header.
Any comments?