Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DEFER or ASYNC allowed on a stylesheet include?

I know script files can use the DEFER and ASYNC keywords on a resource include. Do these keywords also work for stylesheet (i.e., CSS) includes?

Syntax would presumably be:

<link rel="stylesheet" type="text/css" href="./path/to/style.css" defer />

I just don't know if it's allowed or not.

like image 666
Doug Avatar asked Sep 17 '14 12:09

Doug


People also ask

What are defer and async attributes on a script tag?

Defer attribute is useful when script is using for DOM manipulations. Means script will apply on document html. async attribute: It will download the script file and execute without wait the end of html parsing. In other words, It will not guarantee all the scripts will execute after the html parsing.

Can you use async and defer together?

Yes, you can use both attributes but you need to use defer or async, not both.

Should I use defer or async?

In practice, defer is used for scripts that need the whole DOM and/or their relative execution order is important. And async is used for independent scripts, like counters or ads. And their relative execution order does not matter.


4 Answers

Defer and Async are specific attributes of the tag <script> https://developer.mozilla.org/en/docs/Web/HTML/Element/script

They will not work in other tags, because they don't exist there. A stylesheet is not a script that contains logic to be executed in parallel or after the loading. A stylesheet is a list of static styles that get applied atomically to html.

like image 117
fmsf Avatar answered Oct 07 '22 14:10

fmsf


You could do this:

<link rel="stylesheet" href="/css/style.css" media="none" onload="if(media!=='all')media='all'" >

and create a noscript fallback

like image 25
Gijs Erenstein Avatar answered Oct 07 '22 14:10

Gijs Erenstein


As of Jan 2019, this StackOverflow post still ranks #1 in some Google searches. Therefore, I am submitting this very late answer for those who land here seeking to defer the loading of your CSS.

Credit: https://www.giftofspeed.com/defer-loading-css/


The gist

Add the following above the closing </body> tag of your html document

<script type="text/javascript">
  /* First CSS File */
  var giftofspeed = document.createElement('link');
  giftofspeed.rel = 'stylesheet';
  giftofspeed.href = '../css/yourcssfile.css';
  giftofspeed.type = 'text/css';
  var godefer = document.getElementsByTagName('link')[0];
  godefer.parentNode.insertBefore(giftofspeed, godefer);

  /* Second CSS File */
  var giftofspeed2 = document.createElement('link');
  giftofspeed2.rel = 'stylesheet';
  giftofspeed2.href = '../css/yourcssfile2.css';
  giftofspeed2.type = 'text/css';
  var godefer2 = document.getElementsByTagName('link')[0];
  godefer2.parentNode.insertBefore(giftofspeed2, godefer2);
</script>
like image 17
Robert Shaw Avatar answered Oct 07 '22 13:10

Robert Shaw


As of Sep 2020 i found that at least Chrome have native support to defer CSS with the attribute rel="preload" to make an async load of the file

<link href="main.css" rel="stylesheet" rel="preload" as="style"> 

You can use a more comprehensive approach using JavaScript to make it compatible with most browsers and include a noscript option when no JS is enabled

<link href="main.css" rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'">
<noscript><link rel="stylesheet" href="main.css"></noscript>

source: https://web.dev/defer-non-critical-css/

like image 13
siddharta Avatar answered Oct 07 '22 13:10

siddharta