Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load CSS asynchronously without using JavaScript?

Suppose if I have a website http://somethingsomething.com

And I have 3 css file

  • common.css
  • homepage.css
  • inner-pages.css

homepage.css is required for homepage and common.css is for whole site but inner-pages.css is for other pages only and it's big file. Is it possible to load inner-pages.css after homepage data download. In the same way like we use async attribute for script tag. As far as I know async attribute is only for JS not CSS

my one friend suggested to use requirejs for this http://requirejs.org/docs/faq-advanced.html#css but I don't want to use javascript to load css and even I would think upon JS way i would not use require.js just for this. So if it is not possible with CSS only. what would be the simple JS way to do this?

like image 905
Jitendra Vyas Avatar asked Nov 06 '13 17:11

Jitendra Vyas


People also ask

How do I load CSS before page load?

Since the browser has to wait for all CSS to be loaded, it's important to provide it as quickly as possible. A very simple way to make sure the browser receives CSS as early as possible is by including it in the HEAD section of your HTML document. This way, the browser will start loading CSS as soon as possible.

Is CSS synchronous?

That's because by default, browsers will load external CSS synchronously—halting all page rendering while the CSS is downloaded and parsed—both of which incur potential delays.

Is loading CSS blocking?

By default, CSS is treated as a render blocking resource, which means that the browser won't render any processed content until the CSSOM is constructed. Make sure to keep your CSS lean, deliver it as quickly as possible, and use media types and queries to unblock rendering.


2 Answers

Async CSS with media="bogus" and a <link> at the foot Say we've got our HTML structured like this:

<head>
    <!-- unimportant nonsense -->
    <link rel="stylesheet" href="style.css" media="bogus">
</head>
<body>
    <!-- other unimportant nonsense, such as content -->
    <link rel="stylesheet" href="style.css">
</body>

More at http://codepen.io/Tigt/post/async-css-without-javascript

like image 196
Abhishek Gupta Avatar answered Sep 23 '22 03:09

Abhishek Gupta


You can place an iframe in your page pointing to some dummy page that serves the CSS, that should serve the CSS file async.

<iframe src="loadcss.html"></iframe>

Do note it seems pretty trivial, this causes a minimum of 2 css file transfers per page and 3 css file transfers per child page (if it isn't cached). If you were to minify the css you would only have 1 transfer regardless.

like image 24
David Nguyen Avatar answered Sep 24 '22 03:09

David Nguyen