Why do all browsers download all CSS files - even for media types they don't support?

Solution 1:

I think the answer is this:

Browsers are allowed and encouraged to parse media descriptors - no matter what the descriptor - as a way to make them future friendly

Future versions of HTML may introduce new values and may allow parameterized values.

*From: http://www.w3.org/TR/html4/types.html#h-6.13

In this way, media may one day include 3d-glasses or other descriptors, including bork ;-)

EDIT:

The latest CSS3 spec on media queries says this, which supports the above, to a certain degree:

Unknown media types evaluate to false. Effectively, they are treated identically to known media types that do not match the media type of the device.

*From: http://dev.w3.org/csswg/css3-mediaqueries/#error-handling

So they are treated as known and downloaded to be used, just not at that time/for that device.

Solution 2:

Thinking that the real reason that they load all media queries is because many devices CHANGE their responses to these queries after load.

Imaging an iPhone5 that is in portrait on page load (reporting 'width' as 640px, but not 'portrait, unfortunately the iSeries do not support those queries)... you then decide to turn the iPhone sideways, and the browser now activates the pseudo landscape mode (again, triggered from width @ 1126 rather than 'landscape').

Most likely, a responsive web design has been designed to feed different stylesheets to a browser displaying at 640 (rather narrow, probably a phone/tablet) than it does to a browser displaying at 1126 (more likely a laptop).

If it hadn't bothered to load the additional media query sheets, then it would suddenly have to stop, shoot an http request out, wait for the sheet to load, and then parse it to display. This could result in a rather ugly delay.

As most browsers follow a pattern of code reuse, and the core chunks of Webkit or Gecko, for example, may not be aware whether they are on a laptop or a tablet (as if those lines aren't beginning to blur anyway), it simply loads each media query regardless of whether or not they choose to display it.

While this saves each browser from looking bad, overall it breaks a good chunk of the utility behind media queries.

A cell phone or a cheap android tablet shouldn't have to download the additional files (especially on limited data plans) that it will simply never need.

At the moment, my designs DO use media queries, but I use them sparingly. Much of the media queryishness on my sites is implemented through javascript loading of required files to eliminate this waste. The remaining queries are used in cases of javascript being shut off, or for sheets that need to be loaded 'just in case' (my 640px layout, for example, is usually always loaded, as most devices might display it in one situation or another).

If anyone out there has a better, cleaner, method of handling this, please let me know.

In the meantime, if you can think of a simply to implement functionality that might circumvent this (maybe android-style manifests built into browsers?), you might want to drop a line to the Mozilla or Chromium teams... seem like they could use a hand on this one.

Solution 3:

After thinking about this more, I formed the theory that there might be a general "rule" at work - that any stylesheet, image or script would be downloaded, no questions asked, regardless of the specified mime-type or media attribute.

However, after a quick test, the results are a bit ambigious...

  1. <script src="bork.js" type="bork/bork"></script>
  2. <script src="bork2.js" type="text/bork"></script>

Chrome 12 downloads neither.
IE8 downloads #2.
Firefox 4 downloads both.
Opera 11 downloads both.
Safari 5 Win downlads both.

Still no parsing or running takes place in any of the browsers. A javascript alert(); inside either file does not run. And this is slightly different from the CSS loading case, because there the browsers parse the bork-media CSS code for @include directives and downloads those resources recursively.

Solution 4:

The answer may come down to media queries. Consider these for example:

<link rel="stylesheet" media="(min-width: 300px)" href="example1.css" />
<link rel="stylesheet" media="(min-width: 1000px)" href="example2.css" />

If a browser with a window size of 600px is used, the example1.css stylesheet will be applied. If the window is resized to 1200px then the stylesheet example2.css can be immediately applied without waiting for it to download first.

Its worth noting that even though the non-matching media query stylesheet is still downloaded, it does not block rendering while it is downloading (normally all CSS files need to be downloaded before rendering will begin).

Solution 5:

Sometimes, it's necessary to consider the prosaic answer. It's possible that all stylesheets are downloaded by browsers simply because the authors of each browser only really consider the case where there is a single (master) stylesheet when optimizing for speed, and the practice of a lot of sites of having a single stylesheet encourages this behavior. If nobody is testing for it, it's almost certainly not a case that's being optimized, as people prefer to work on results that are visible (or at least measurable). Maybe your question will encourage someone to change the testing regime…

Also, I'd venture that the overwhelming majority of sites' stylesheets are static documents, and so capable of being very highly cached (and delivered by CDN too, if the site owners choose to pay).