Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are unused CSS images downloaded?

Tags:

css

People also ask

How do I get the unused CSS code?

The Coverage tab in Chrome DevTools can help you find unused JavaScript and CSS code. Removing unused code can speed up your page load and save your mobile users cellular data.

What is unused CSS?

Unused CSS are stylesheets completely useless for rendering and loading the page — yet, they're included in the page's code. If you remove all of them, nothing will happen. The page will be displayed correctly above and below the fold. At the same time, unused CSS affects the page's loading time.

Why is my image not showing up in CSS?

Make sure the image path is set correctly in the background-image url. Once you have made sure that your CSS file is linked correctly, also check that the image itself is set correctly. Again, you will want to open your code inspector in the browser to check.


This would be browser dependent, since it's how they decide to implement the spec, however in a quick test here:

  • Chrome: Doesn't
  • FireFox: Doesn't
  • Safari: Doesn't
  • IE8: Doesn't
  • IE7: Doesn't
  • IE6: Unknown (Can someone test and comment?)

No, they are not downloaded, not at least in Firefox, IE8 and Chrome.

An easy way to test this:

<!DOCTYPE html>
<html>
    <head>
       <style type="text/css">
        .nonexistent {
            background: url('index.php?foo');
        }
        </style>
    </head>
    <body>
<?php if(isset($_GET['foo'])) {
    file_put_contents('test.txt', $_SERVER['HTTP_USER_AGENT']);
} ?>
    </body>
</html>

If test.txt is populated with the browser's user agent, then the image is downloaded. This was not the case in any of my tests.


A quick test proved it.

<!DOCTYPE html>
<html>
<head>
<title></title>

<style type="text/css"><!--

.hasnothing{background-image:url(http://25.media.tumblr.com/tumblr_ky7aakqvH01qatluqo1_400.jpg);}
.hassomething{background-image:url(http://27.media.tumblr.com/tumblr_kxytwr7YzH1qajh4xo1_500.png);}

--></style>

</head><body>

<div class="hassomething"></div>

</body></html>

The 2nd image, tumblr_kxytwr7YzH1qajh4xo1_500.png, was downloaded but not the other one. This was proven true in Chrome (Developer tools) and Firefox (Firebug).


Firefox and Chrome (Ubuntu 9.10) don't download images for classes/ids that aren't applied in the DOM.

This may be both platform and browser dependant, though.


Sometimes, it depends just exactly what "unused" means. Different browsers define it differently. For example, in Firefox, styles applied to the <noscript> tag are deemed "unused" and thusly, any images won't be downloaded.

Chrome 26 (possibly all of them, not sure), does download CSS images if they are applied to the <noscript> element, even when JS is enabled. (It isn't immediately clear to me why though, perhaps this is a bug?).

It does not download CSS images applied to elements within the <noscript> element, though. (this is expected behaviour, of course).

Example:

CSS:

noscript { background-image: url('always.png') 0 0 repeat; }
noscript p ( background-image: url('nojsonly.png') 0 0 repeat; }

HTML:

<noscript>The CSS background image of this NOSCRIPT-element will always be downloaded in Chrome.  Will not be downloaded in Firefox</noscript>
<noscript><p>The CSS background image of this P-element won't be downloaded in Chrome or other browsers, unless JS is disabled</p></noscript>

In this case, if the user has JS-enabled, both always.png and otherbg.png are downloaded in Chrome. If the user does not have JS enabled, then only nojsonly.png is downloaded in Chrome.

I use this technique for measuring traffic-levels from non-JS-enabled users, as Google Analytics fails us here. I prefer using the background CSS image rather than a normal <img...> tag, because I'm working under the (untested) theory that bots are less likely to grab a CSS image than a <img...> image, leaving more accurate counts for the human-visitors.