HTML
CSS
performance optimization
stylesheet
web development

DEFER or ASYNC allowed on a stylesheet include?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

The discussion around using defer or async attributes on stylesheet includes often emerges from the desire to optimize page load times. These attributes, while commonly associated with scripts, have specific purposes and behaviors that arise from different needs in web page optimization.


Understanding defer and async

defer Attribute

The defer attribute is a boolean attribute typically used with the <script> tag. It instructs the browser to load and execute the script only after the HTML document has been completely parsed. Scripts with defer are executed in the order they appear in the document.

async Attribute

Similar to defer, the async attribute is a boolean attribute utilized with the <script> element. When a script has the async attribute, the browser is instructed to fetch it asynchronously, meaning the script can be executed as soon as it's ready, irrespective of its order in the HTML document.

However, both these attributes do not apply to <link> elements used for stylesheets.

Can defer or async be Applied to Stylesheets?

Stylesheet Fetching and Rendering

When a stylesheet is included via a <link> element, it affects the Critical Rendering Path (CRP) because the browser needs to fetch these resources before rendering the page. This need arises as the document's style influences the layout of the page, and rendering must only occur once the styles are downloaded and applied.

Influence and Limitations

Neither defer nor async is applicable to style sheets due to the fundamental role styles play in the rendering process. If styles were deferred or asynchronously loaded, it could lead to a Flash of Unstyled Content (FOUC), where the page appears unstyled momentarily before CSS is applied.

Alternative Methods to Optimize CSS Loading

1. Media Attributes

Using the media attribute can enable a non-blocking behavior for CSS by loading them for specific conditions. For example, loading a print-specific stylesheet can be deferred until printing is initiated:

html
<link rel="stylesheet" href="print.css" media="print">

2. Rel Preload

Preloading involves fetching CSS resources in advance without blocking the parser. This technique can be employed using link with rel="preload":

html
<link rel="preload" href="styles.css" as="style" onload="this.rel='stylesheet'">

The onload attribute is crucial here as it changes the 'rel' to 'stylesheet' only after the resource is fetched.

3. Async CSS with JavaScript

A more advanced method involves bringing in CSS files as part of JavaScript logic, which can allow asynchronous loading:

javascript
1let link = document.createElement('link');
2link.rel = 'stylesheet';
3link.href = 'styles.css';
4document.head.appendChild(link);

This approach enables better control over when styles are loaded.

Comparative Summary

AttributeApplicable TagsEffect on LoadingImpact on Page Render
defer<script>Loads script after parsing completesDoes not affect initial rendering
async<script>Loads and executes as soon as downloadableScripts execute independently of HTML parsing
Stylesheet Media<link>Blocks rendering for matching mediaCan optimize loading for specific scenarios
Preload<link>Loads resource ahead of needRequires onload for style application

Conclusion

While defer and async play critical roles in JavaScript optimization, they do not apply to stylesheets where immediate application is essential for visually rendering pages correctly. Developing optimized web pages entails considering how and when styles are loaded, with techniques like media attributes, preloading, and programmatically including styles via JavaScript serving as effective alternatives for managing CSS loading strategies. The challenge remains achieving a balance between speed and immediate visual content rendering.


Course illustration
Course illustration

All Rights Reserved.