GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • HTML
  • CSS
  • JavaScript
  • Performance
  • Accessibility
  • Identity
  • Learn HTML
  • Learn CSS
  • Learn JavaScript
  • Learn AI
  • Learn Performance
  • Learn Accessibility
  • More courses
  • AI and the web

This view

  • Original page
  • New address

GeezerVison is showing web.dev — Using CSS module scripts to import stylesheets | Articles | web.dev

Modern Web Guidance is now stable! Upgrade your coding agents with expert-vetted web development best practices. Read the announcement.

Using CSS module scripts to import stylesheets Stay organized with collections Save and categorize content based on your preferences.

Learn how to use CSS module scripts to import CSS stylesheets using the same syntax as JavaScript modules.

Dan Clark
Dan Clark

With the new CSS module scripts feature, you can load CSS style sheets with import statements, just like JavaScript modules. The style sheets can then be applied to documents or shadow roots in the same manner as constructable stylesheets. This can be more convenient and more performant than other ways of importing and applying CSS.

Browser support

CSS module scripts are available by default in Chrome and Edge in version 93.

Support in Firefox and Safari is not yet available. Implementation progress can be tracked at the Gecko bug and WebKit bug, respectively.

Prerequisites

  • Familiarity with JavaScript modules.
  • Familiarity with constructable stylesheets.

Using CSS module scripts

Import a CSS module script and apply it to a document or a shadow root like this:

import sheet from './styles.css' assert { type: 'css' };
document.adoptedStyleSheets = [sheet];
shadowRoot.adoptedStyleSheets = [sheet];

The default export of a CSS module script is a constructable stylesheet whose contents are those of the imported file. Like any other constructable stylesheet, it is applied to documents or shadow roots using adoptedStyleSheets.

Unlike other ways of applying CSS from JavaScript, there is no need to create <style> elements or mess with JavaScript strings of CSS text.

CSS modules also have some of the same benefits as JavaScript modules.

  • Deduplication: if the same CSS file is imported from multiple places in an application, it will still only be fetched, instantiated, and parsed a single time.
  • Consistent order of evaluation: when the importing JavaScript is running, it can rely on the stylesheet it imports having already been fetched and parsed.
  • Security: modules are fetched with CORS and use strict MIME-type checking.

Import assertions (what's with the 'assert'?)

The assert { type: 'css' } part of the import statement is an import assertion. This is required; without it, the import is treated as a normal JavaScript module import, and will fail if the imported file has a non-JavaScript MIME type.

import sheet from './styles.css'; // Failed to load module script:
                                  // Expected a JavaScript module
                                  // script but the server responded
                                  // with a MIME type of "text/css".

Dynamically imported stylesheets

You can also import a CSS module using dynamic import, with a new second parameter for the type: 'css' import assertion:

const cssModule = await import('./style.css', {
  assert: { type: 'css' }
});
document.adoptedStyleSheets = [cssModule.default];
Note that it's cssModule.default (not cssModule itself) that is added to adoptedStyleSheets. This is because the object returned from dynamic import() is a module namespace object. The CSSStyleSheet is the default export of the module, so it's accessed at cssModule.default.

@import rules not yet allowed

Currently CSS @import rules don't work in constructable stylesheets, including CSS module scripts. If @import rules are present in a constructable stylesheet, those rules will be ignored.

/* atImported.css */
div {
    background-color: blue;
}
/* styles.css */
@import url('./atImported.css'); /* Ignored in CSS module */
div {
    border: 1em solid green;
}
<!-- index.html -->
<script type="module">
    import styles from './styles.css' assert { type: "css" };
    document.adoptedStyleSheets = [styles];
</script>
<div>This div will have a green border but no background color.</div>

Support for @import in CSS module scripts may be added to the specification. Track this specification discussion in the GitHub issue.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2021-08-17 UTC.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.