Is it permissible to use DEFER or ASYNC when including a stylesheet?

I am curious if script files are able to utilize the DEFER and ASYNC keywords on a resource include. Are these keywords also compatible with stylesheet (CSS) includes?

The syntax I imagine would be:

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

I'm unsure if this is permissible or not.

Answer №1

Defer and Async are unique attributes of the <script> tag only. You can learn more about this at this link.

It's essential to note that these attributes won't function in other tags since they are specific to scripts. Unlike scripts, stylesheets contain static styles and do not involve any logic for parallel execution or post-loading activities on a webpage.

Answer №2

One possible solution is to implement the following code:

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

In addition, you can create a noscript fallback

Answer №3

Acknowledgment:


Insert the following code snippet above the closing </body> tag in your HTML file

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

  /* Load Second CSS File */
  var acknowledgement2 = document.createElement('link');
  acknowledgement2.rel = 'stylesheet';
  acknowledgement2.href = '../css/yoursecondcssfile.css';
  acknowledgement2.type = 'text/css';
  var defer2 = document.getElementsByTagName('link')[0];
  defer2.parentNode.insertBefore(acknowledgement2, defer2);
</script>

Answer №4

In September 2020, I discovered that Chrome now has built-in support for deferring CSS using the rel="preload" attribute to asynchronously load the file.

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

To ensure compatibility with most browsers, a more comprehensive approach involves using JavaScript and adding a noscript option for situations where JavaScript is disabled.

<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/

Answer №5

Although there is no official documentation on this, here are my observations:

Testing Method

  • Tested exclusively with Google Chrome version 61.0.3163.100 (Official version) (64 bits)
  • Utilized Fast & Slow 3G throttling in the developer tools.

What I Examined

I had a stylesheet that imported custom fonts and applied them to the text.

Before:

The text using the custom font remained invisible until it fully loaded and then appeared on the page.

After/Result:

Therefore, the outcome was that the text became visible immediately as the page started rendering, initially using the fallback font, and then switching to the custom font after some time.

It appears that Google Chrome supports defer on <link> tags, even if this feature is not publicly documented...

Answer №6

Beacon appears to be fond of the following:

<script>
  window.addEventListener('load', () => {
    let stylesheet = document.createElement('link');
    stylesheet.rel = 'stylesheet';
    stylesheet.type = 'text/css';
    stylesheet.href = "/path/to/style.css";
    document.head.appendChild(stylesheet)
  })
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Activate a button with jQuery when a key is pressed

Currently, I have two buttons set up with jQuery: $('#prev').click(function() { // code for previous button }); $('#next').click(function() { // code for next button }); My goal now is to implement a .keypress() handler on the ...

Tips for choosing an attribute within a list with a CSS selector and Selenium

Is it possible to use a css selector to target an element within a <li> using Selenium? Below is the code snippet in question: <div id= "popup"> <ul> <li> <div id="item" option= "1" ....> </div> < ...

What would be more efficient: inputting all items in a form at once or adding them one by one consecutively

My mind is preoccupied with a dilemma: is it better to have all possible items already on the form, or should items only be added when the user requests them? Let me elaborate - Imagine I have a form with 4 input fields and one textarea. Alongside that, t ...

Craft a Flawlessly Repeating Sound Experience - Online

I'm facing a challenge in creating a flawless loop of an audio file. However, all the methods I've tried so far have resulted in a noticeable gap between the end and the start. Here are the approaches I experimented with: The first approach inv ...

The code is slicing data, but the changes are not reflecting in the user interface

Initially, there are three drop down menus displayed. Upon selecting an option from the first drop down menu, the values in the second drop down menu load. After selecting an option from the second drop down menu, a new set of drop downs appears. However, ...

ACTUAL preventing a component from losing its focus

I have been exploring ways to effectively stop a DOM element from losing focus. While researching, I came across different solutions on StackOverflow such as: StackOverflow solution 1 StackOverflow solution 2 StackOverflow solution 3 However, these sol ...

Ways to make a div fill the whole screen and allow for scrolling as well

Issue I'm facing an issue with the login.php screen and the addphoto.php screen. I want these screens to cover the entire page, but despite using the following code: .login-screen, .form-screen { position: fixed; top: 0; left: 0; disp ...

I am experiencing an issue with the button that is supposed to link to a section within the same page while it is located within the jumbotron. Interestingly, the button

I'm facing an issue with a button placed inside my jumbotron. The button is supposed to link to a specific section within the page, but it's unresponsive. Even hovering over it shows no interaction. Strangely, if I move the button outside of the ...

Arranging divs using inline-block display. How to adjust the heights consecutively?

After much searching and attempting, I am still unable to achieve a simple task. My goal is to apply display inline-block to some divs while aligning them in a row, similar to the image below: The issue arises when number 4 and 5 are positioned after 1 wi ...

What is the best way to collapse a button in asp.net using javascript after setting it to hidden?

I have a scenario where I have 3 buttons in a row on my asp.net page. Using JavaScript, I am setting the middle button (ButtonSR) to invisible. However, I want the button below it to move up and take its place instead of leaving an empty space. ...

Is it possible to modify the button icon on hover by utilizing chakra-ui and vanilla-extract?

My stack includes nextjs13, chakra-ui, and vanilla-extract Seeking guidance on how to update both icon and text within a button upon hover, utilizing chakra-ui and vanilla-extract. The current setup of my button is as follows: <Button > <svg wi ...

Positioning bar chart columns and text using CSS

My component generates HTML for a simple bar chart, with the height and background color computed within the component. Everything functions correctly, but I am facing an issue where long text causes displacement of the bar above it. Another problem is th ...

The right alignment with Flexbox's justify content property is not being implemented as expected

I'm having trouble aligning my footer using flexbox - no matter what I try, it won't move all the way to the right. When I use justify-content: center, the items are centered, but when I change it to justify-content: right, everything shifts bac ...

Accessing the name attribute of option tags in a controller: A guide

Within my blade file, the following code snippet is present: <select class="form-control" name="region_id_report"><option name="blank">No Region</option></select> Inside my controller, a variable named $regionidreport stores the v ...

CSS media query that prioritizes styling for large screens over small screens

I am currently working on making my new website development project mobile responsive, but I am encountering some strange behavior that is quite frustrating. I am hoping someone can help me figure out what mistake I may be making here. Whenever I test the ...

Attempting to align the fixed banner in the middle of my blog

I have spent hours combing through code after code on this site, trying to center my fixed banner without success. I am feeling frustrated and in need of some assistance. Here is the CSS snippet I have been working with: .heading { position:fixed; ...

The slideUp function is not functioning as expected

I am trying to implement a slideUp effect on my webpage. Here is the code within my <body> tag: <script> $('#slide_up').click(function(){ $('p.text_study').slideUp('slow', function() { $ ...

Styling the scroll bar within a fixed parent container's inner div with CSS

Check out the JSBIN link here: JSBIN The modules container has a fixed height of 100%. I am attempting to make the list items scroll while keeping the search div fixed within the wrapper container, ensuring that the search bar remains visible at all times ...

In React, the entire component refreshes every time the modal is opened

<ThemeProvider theme={theme}> <GlobalStyle /> {componentName !== 'questionaire' && componentName !== 'activityResult' && <CardWrapper />} <ErrorModal ...

Symbol for infinity does not appear correctly within the span element

I am currently facing an issue where HTML entities are not displaying within a span element. Specifically, I am attempting to show &infin; if there is no destroy date for my object. Interestingly, when the entity is moved outside of the span, it appear ...