Referencing CSS stylesheets with link[rel=stylesheet] or @import causes browsers to delay page rendering while a stylesheet loads. When loading stylesheets that are not critical to the initial rendering of a page, this blocking behavior is undesirable. The pattern below allows us to fetch and apply CSS asynchronously. If necessary, this repo also offers a separate (and optional) JavaScript function for loading stylesheets dynamically. As a primary pattern, we recommend loading asynchronous CSS like this from HTML. The loadCSS.js file exposes a global loadCSS function that you can call to load CSS files programmatically, if needed. This is handy for cases where you need to dynamically load CSS from script. The code above will insert a new CSS stylesheet link after the last stylesheet or script that it finds in the page, and the function will return a reference to that link element, should you want to reference it later in your script.

Features

  • The loadCSS function has 3 optional arguments
  • The loadCSS patterns attempt to load a css file asynchronously in any JavaScript-capable browser
  • loadCSS attempts to inject the stylesheet link after all CSS and JS in the page
  • Your stylesheet will be inserted before the element you specify
  • To update, you can change your preload markup to this HTML pattern and delete the JS from your build
  • You can load CSS asynchronously

Project Samples

Project Activity

See All Activity >

Categories

File Sync

License

MIT License

Follow loadCSS

loadCSS Web Site

You Might Also Like
Cloud data warehouse to power your data-driven innovation Icon
Cloud data warehouse to power your data-driven innovation

BigQuery is a serverless and cost-effective enterprise data warehouse that works across clouds and scales with your data.

BigQuery Studio provides a single, unified interface for all data practitioners of various coding skills to simplify analytics workflows from data ingestion and preparation to data exploration and visualization to ML model creation and use. It also allows you to use simple SQL to access Vertex AI foundational models directly inside BigQuery for text processing tasks, such as sentiment analysis, entity extraction, and many more without having to deal with specialized models.
Try for free
Rate This Project
Login To Rate This Project

User Reviews

Be the first to post a review of loadCSS!

Additional Project Details

Programming Language

JavaScript

Related Categories

JavaScript File Sync Software

Registered

2021-08-19