Forum Moderators: not2easy

Message Too Old, No Replies

Multiple 'preferred' stylesheets - which one should take precedence?

All browsers seem to differ from the W3C specs?

         

penders

6:47 pm on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(I'm writing some code to manage stylesheets, anyway...)

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?!

swa66

8:06 pm on Oct 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you post a small sample of the html headers (sanitized of course) that shows what you're doing as there are multiple forces at work.

penders

12:58 am on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Three 'preferred' stylesheets, characterised by having rel="stylesheet" and different TITLE attributes...
<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.)

swa66

9:58 am on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think it could be seen as an error according to the standards to have more than one of the preferred style sheet. From [w3.org...] :
The author may specify that one of the alternates is a preferred style sheet.

But maybe over all the edits it got to a point where somebody saw usefullnees in having multiple preferred style sheets.

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.

penders

11:02 am on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah yes, thanks for the clarity! I think I'd spent too much time pondering over my HEAD section and gotten muddled between my LINKs and METAs!

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?

penders

11:42 am on Oct 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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?

penders

2:05 pm on Oct 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I hadn't realised that when Chrome (and also Safari, since it was a Webkit issue) first came out (Sept 2008) they did not even support 'alternate' stylesheets. But worse, they treated 'alternate' stylesheets as persistent! This would have broken any website that used 'alternate' stylesheets! Well, at least this issue seems to have been fixed now.