What will be the best way to load CSS async? - html

I have nine different CSS files. My website will not load until browser downloads all the CSS files. Most of CSS not even needed for home page. In JavaScript you can do like <script async>,
but for stylesheets, what will be best solution?
I have searched found following articles
Code Pen
keithclark.co.uk
They recommend to use
<link rel="stylesheet" href="css.css" media="none" onload="if(media!='all')media='all'">
or
<head>
<!-- head content -->
<link rel="stylesheet" href="style.css" media="bogus">
</head>
<body>
<!-- body content -->
<link rel="stylesheet" href="style.css">
</body>

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.
-- Google Developers
By default, CSS is treated as a render blocking resource.
Media types and media queries allow us to mark some CSS resources as non-render
blocking.
The browser downloads all CSS resources, regardless of
blocking or non-blocking behavior.
CSS is a render blocking resource. Get it to the client as soon and as quickly as possible to optimize the time to first render.
However, what if we have some CSS styles that are only used under certain conditions, for example, when the page is being printed or being projected onto a large monitor? It would be nice if we didn’t have to block rendering on these resources.
CSS "media types" and "media queries" allow us to address these use cases:
<link href="style.css" rel="stylesheet">
<link href="print.css" rel="stylesheet" media="print">
<link href="other.css" rel="stylesheet" media="(min-width: 40em)">
By using media queries, we can tailor our presentation to specific use cases, such as display versus print, and also to dynamic conditions such as changes in screen orientation, resize events, and more. When declaring your style sheet assets, pay close attention to the media type and queries; they greatly impact critical rendering path performance.
Let's consider some hands-on examples:
<link href="style.css" rel="stylesheet">
<link href="style.css" rel="stylesheet" media="all">
<link href="portrait.css" rel="stylesheet" media="orientation:portrait">
<link href="print.css" rel="stylesheet" media="print">
The first declaration is render blocking and matches in all conditions.
The second declaration is also render blocking: "all" is the default type so if you don’t specify any type, it’s implicitly set to "all". Hence, the first and second declarations are actually equivalent.
The third declaration has a dynamic media query, which is evaluated when the page is loaded. Depending on the orientation of the device while the page is loading, portrait.css may or may not be render blocking.
The last declaration is only applied when the page is being printed so it is not render blocking when the page is first loaded in the browser.
Source - Render blocking CSS -

Related

Can <link rel=preload> preload resources inside <iframe>s?

I have a page that embeds another page, let’s call it frame.html:
<iframe src="sub-page.html">
On sub-page.html, it loads some CSS and an image:
<link rel="stylesheet" href="sub-page.css">
<img src="sub-page.svg" alt="Sub-Page">
I at first thought that I could speed up the loading of these sub-subresources by adding <link rel=preload> to frame.html:
<link rel="preload" href="sub-page.css" as="style">
<link rel="preload" href="sub-page.svg" as="image">
…but that doesn’t seem to help, looking at the resulting waterfall graphs.
The specs for preload links seem to emphasize that it’s only for subresources of the current page, but since you can preload font files that are called from a stylesheet, it’s plausible that resources of a frame could also count. Am I missing something, like the correct use of the crossorigin attribute?
Yes.
Turns out I was looking at an old, unpurged version of the page when I was trying my performance tests.
<link rel=preload> absolutely can preload subresources of an <iframe>’d page..

How can I reduce external webfonts lag time on page load?

We use cloud typography for a selection of web fonts chosen by a designer. The response time is creating a lag that people have begun to notice.
<link type="text/css" rel="stylesheet" href="//cloud.typography.com/XXXXXXX/YYYY/css/fonts.css" media="all" />
Is there a way, while still respecting CT's licencing model to bring these fonts in locally? Or do I switch to standard web fonts?
To sort of explain my answer/comment...
Say you have something like this for example..
<link type="text/css" rel="stylesheet" href="localfolder/main.css" />
<link type="text/css" rel="stylesheet" href="//cloud.typography.com/XXXXXXX/YYYY/css/fonts.css" media="all" />
<link type="text/css" rel="stylesheet" href="localfolder/other.css" />
<link type="text/css" rel="stylesheet" href="localfolder/again.css" />
<link type="text/css" rel="stylesheet" href="localfolder/some.css" />
<link type="text/css" rel="stylesheet" href="localfolder/thing.css" />
You can change this to something more like...
<link type="text/css" rel="stylesheet" href="localfolder/css.php" />
<link type="text/css" rel="stylesheet" href="//cloud.typography.com/XXXXXXX/YYYY/css/fonts.css" media="all" />
With the php file of css.php being like this
header("Content-type: text/css");
require "localfolder/main.css";
require "localfolder/other.css";
require "localfolder/again.css";
require "localfolder/some.css";
require "localfolder/thing.css";
exit;
Basically this will combine all of your local CSS into a single script, which you can then use a PHP cache control and gzip to ensure your local CSS is sent as quick as possible in a single http/file request
... And your second link for typography tag will start downloading straight away as well
As soon as your first link tag (the css.php) is finished being downloaded/checked.. It will continue with anything else in the head tag until they are all done.
This may work for you, it really does very per site/design.. Basically most browsers will download only so many files at once... refer to Max parallel http connections in a browser? for some more info on this...
--- Another possible option ---
You can load the page without the typography link/tag.. and then add it dynamically via javascript.. see something like this How to load up CSS files using Javascript? for an example..
The side effect here depending on how the site is designed, would be that you might see old/default fonts for a few seconds or something.. But you can hide this from the user with a re-design possibly or some form of loader..
Otherwise the only other option i can think of is to try finding the same font or similar with google fonts https://www.google.com/fonts as they do load quicker in general.. And the advantage of using a google hosted css/js/lib is that alot of users also already have them cached because they are common across alot of other sites.
Hopefully this can give you some idea's or possibly help with a solution, but it is a tricky question and a good one... This is how i would deal with it if i was in the same situation.

How CSS influence loading times

I'm pretty new to HTML&CSS, and I'm building my first website.
My head tag looks like this:
<head>
<meta charset="utf-8">
<meta name = "viewport" content = "width=device-width, minimum-scale=1, maximum-scale=1">
<meta name = "apple-mobile-web-app-capable" content = "yes" />
<link type="text/css" rel="stylesheet" href="css/normalize.css">
<link type="text/css" rel="stylesheet" href="http://fast.fonts.net/cssapi/739d414f-2430-436c-b2cf-5b84cc45995c.css">
<link type="text/css" rel="stylesheet" href="css/mobile.css">
<link type="text/css" rel="stylesheet" href="css/projects2.css">
<link type="text/css" rel="stylesheet" media="screen and (min-width:680px)" href="css/tablet.css">
<link type="text/css" rel="stylesheet" media="screen and (min-width:1400px)" href="css/desktop.css">
<title>Yulie Wollman | Projects </title>
</head>
I have 2 questions regarding to the CSS:
First, is multiple external CSS files influence my loading times? is it better to merge them into one big CSS file?
Secondly, will the media screen css link tags influence my loading times on smaller resolutions than 680px/1400px?
Thank you,
Boaz Keren Gil
With HTTP 1.0/1.1 every single HTTP request comes with overhead so the fewer requests the better.
You should work to keep the CSS segregated for your ease of coding at the server but combine them and then serve a single CSS compiled "file" to the client.
<?php
header('Content-Type: text/css; charset=UTF-8');
include('css/normalize.css');
include('css/mobile.css');
include('css/projects2.css');
?>
If you're running Apache you'll need to ensure you can execute PHP inside of the file type you use (e.g. .css)...
AddType application/x-httpd-php5 .css
...just make sure that you serve the correct mime FROM the file itself when you do this.
From my experience I would've said that one request is always better that lots of requests, so one big css is , in this case better, BUT if you have really big css file for lots of pages, and you'll load it on every page even if 99% of it's style are not necessary - that's not good at all. So decide it for your own case: few pages, small css - merge it to one, big project, lots of pages - separate requests on each.
And for second - nop,they will not.
Overall question Yes, it does influence load time. Each external css file is a additional request so it does add overhead.
However, the browser will cache the css so the next request will not suffer that much.
In general, if all pages in the site use the same css I would combine them and minimize them once deployed into your production environment
There are tools available like YSlow from Yahoo or Page Speed from Google that give you great insight in what is happening.
https://developer.yahoo.com/yslow/
https://developers.google.com/speed/pagespeed/?hl=nl
You can also use plugins for your browser to see the effect.
Generally it is recommended to reduce the number of http requests, especially for the websites targeting mobile browsers. The mobile networks have usually high latency (could be 300ms per request), then for 10 referenced files (css, javascript) it is 300 * 10 = 3 seconds loading time.

media query - can I use seperate, independant stylesheet?

I was only introduced to media queries yesterday while researching building my mobile optimized pages. I have found that contrary to my initial interpretation a media query directs to a stylesheet that is used in addition to and overrides the existing stylesheet.
Is that right? Is there a way to tell the visitors browser to ignore the original stylesheet and just use the one for mobile?
Here is existing code:
<link href="styles_mobile.css" media="screen and (max-device-width: 480px)" type="text/css" rel="stylesheet">
<link type="text/css" href="styles.css" rel="stylesheet">
It's perfectly acceptable to do so.

Force browser to render webpage's Print stylesheet

A lot of web pages use a Print stylesheet to format things better for printing. My question is, is it possible to force the browser to render a page using the print stylesheet without actually printing it?
In Chrome (version 78), you can force the browser to render the webpage's print stylesheet using Chrome DevTools by going to More Tools > Rendering
then selecting the Print option in the Emulate CSS Media dropdown
usually the print css has a media type of print. simply remove the media definition and use it in replace of the main style sheet
<link rel="stylesheet" type="text/css" media="print" href="print.css" />
change to
<link rel="stylesheet" type="text/css" media="all" href="print.css" />