How to Delay Loading of Non Critical CSS to Increase Mobile Site Speed

It’s a good idea to delay the loading of non-critical CSS to speed mobile site page speeds. Probably you use lots of CSS files to style your site, and this forces the browsers to load all these files before rendering your page to your site visitors. This increases your site load times and badly hits your mobile site page speed results.

Move CSS to Footer

Another important part about CSS files (unlike scripts that you add at bottom of pages) is that you traditionally load them in the HEAD section of HTML. While you can try these tips to load CSS faster, is there a way to load CSS files in the footer using javascript?

Here is what typically Google PageSpeed tests will reveal for your mobile site

non critical css

Google mobile usability is getting important, and soon mobile-friendly sites are expected to rank higher in search engine results. I found this script suggested by Google to load non-critical CSS with javascript in the footer.

      var cb = function() {
        var l = document.createElement('link'); l.rel = 'stylesheet';
        l.href = 'PATH/TO/STYLE.CSS';
        var h = document.getElementsByTagName('head')[0]; h.parentNode.insertBefore(l, h);
      var raf = requestAnimationFrame || mozRequestAnimationFrame ||
          webkitRequestAnimationFrame || msRequestAnimationFrame;
      if (raf) raf(cb);
      else window.addEventListener('load', cb);

You can replace the path to CSS with your CSS file and this code before the BODY closing tag at the bottom of your HTML. Now this CSS script will load after all content is rendered.

Non Critical CSS

Non-critical CSS means the CSS which can conveniently be loaded later and will not impact your site design drastically. While they suggest you should load all critical CSS inline in the HEAD, many times such CSS is too much to inline. While you can retain your critical primary site design CSS files in the HEAD, you can move the rest of the CSS files to the footer.

For example, we use Bootstrap for superb responsive mobile-first design, and we continue to load it in the HEAD as it is essential for basic site design like columns and sidebars. But we have shifted Font Awesome CSS (which loads all the amazing icons you see on this site) to the footer using this script as the icons can load later and are not critical for initial site design.

Try it yourself and increase your mobile site speed today.

Share with friends

About the Author: P Chandra is editor of QOT, one of India's earliest tech bloggers since 2004. A tech enthusiast with expertise in coding, WordPress, web tools, SEO and DIY hacks.