DEFER or ASYNC allowed on a stylesheet include?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
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:
2. Rel Preload
Preloading involves fetching CSS resources in advance without blocking the parser. This technique can be employed using link with rel="preload":
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:
This approach enables better control over when styles are loaded.
Comparative Summary
| Attribute | Applicable Tags | Effect on Loading | Impact on Page Render |
defer | <script> | Loads script after parsing completes | Does not affect initial rendering |
async | <script> | Loads and executes as soon as downloadable | Scripts execute independently of HTML parsing |
| Stylesheet Media | <link> | Blocks rendering for matching media | Can optimize loading for specific scenarios |
| Preload | <link> | Loads resource ahead of need | Requires 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.
Related reading
- Delete many rows from a table using id in Mysql
- Delete sql rows where IDs do not have a match from another table
- Delete unused kafka consumer group
- Description of TF Lite's Toco converter args for quantization aware training
- Define css class in django Forms
- Deleting array elements in JavaScript - delete vs splice
- design a stack such that getMinimum should be O1
- Design an efficient algorithm to sort 5 distinct keys in fewer than 8 comparisons

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.