Subscribe

Close

Thank you for visiting!

Please consider subscribing to the RSS feed or following me on Twitter.

Regarding style sheets and the title attribute

When linking to external style sheets and using the title attribute, you have to be a bit careful. I'll explain to you how to correctly link to external style sheets.

Normally, you link to an external style sheet like this:

<link rel="stylesheet" type="text/css" href="/css/main.css"
	media="screen,projection" />

Perfectly fine!

However, you'd like to add a print stylesheet to, using media="print":

<link rel="stylesheet" type="text/css" href="/css/print.css"
	media="print" />

Now, this too works fine. However - when you want to use the title attribute with your style sheets, you have to be careful.

The title attribute is great to use if you want simple style switching and alternative style sheets. But you need to know what you do to get it to work!

In this code, the print style sheet will not work, it won't be triggered in different browsers:

<link rel="stylesheet" type="text/css" href="/css/main.css"
	media="screen,projection" title="Default stylesheet" />
<link rel="stylesheet" type="text/css" href="/css/print.css"
	media="print" title="Print stylesheet" /> 

It's because the print style sheet's title attribute has a value. If the value is removed, or the whole title attribute, the print style sheet will work.

This happens because there are three different types of style sheets - and if an external style sheet is of the wrong kind - this style sheet will simply be ignored.

Three different types of style sheets

The three types of style sheets are:

  1. Persistent, which is a kind of default style sheet. No title attribute is needed.
  2. Preferred, which is the style sheet web browsers apply. Title attribute is needed.
  3. Alternate, which is alternatives to already existing style sheets. Title attribute is needed.

If these are mixed up, things might not be working as expected. Here's more on specifying external style sheets.

Conclusion

If you want your style sheets to work, make sure they're of the correct style sheet type. This final example works very well:

<link rel="stylesheet" type="text/css" href="/css/main.css"
	media="screen,projection" title="Default stylesheet" />
<link rel="stylesheet" type="text/css" href="/css/print.css"
	media="print" />

To the top