Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 ยท CSS and Rendering

Render-Blocking CSS and Critical CSS

Reduce the CSS that blocks first paint. Learn critical CSS inlining, media attributes, non-blocking stylesheet loading, @import pitfalls and unused CSS cleanup.

A stylesheet in the <head> stops the page from painting until it is downloaded and parsed. A 400 KB stylesheet on a slow connection means a blank screen for a long time, even if the HTML arrived instantly.

The solution is not to remove CSS but to separate the CSS needed for the first screen from the CSS needed later, and to stop the latter from blocking.

Why all CSS blocks

The browser cannot know whether a rule in file two overrides a rule in file one, so it waits for every stylesheet before painting. The only exceptions are stylesheets that do not apply to the current situation. A media attribute tells the browser exactly that:

HTML
<link rel="stylesheet" href="/css/base.css">
<link rel="stylesheet" href="/css/print.css" media="print">
<link rel="stylesheet" href="/css/wide.css" media="(min-width: 1200px)">

The print stylesheet still downloads, but at low priority, and it does not block rendering. On a phone the wide stylesheet does not block either.

Avoid @import

@import inside a stylesheet makes the browser discover the second file only after it has downloaded and read the first. That creates a request chain.

CSS
/* slow: a chain of requests */
@import url("/css/typography.css");
@import url("/css/layout.css");

Use several <link> tags instead (all discovered at once), or let your build tool concatenate files.

Critical CSS

Critical CSS is the minimal set of rules to render the above-the-fold content: layout skeleton, header, hero and fonts. The approach has three parts.

  1. Inline critical CSS in a <style> tag in the head. It arrives with the HTML, so no extra request blocks painting.
  2. Load the full stylesheet without blocking.
  3. Keep the critical part small, ideally under about 14 KB (the first TCP round trip carries around that much).
HTML
<head>
  <style>
    /* critical: header, hero, base typography */
    body { margin: 0; font: 16px/1.5 system-ui, sans-serif; }
    .hero { min-height: 60vh; display: grid; place-items: center; }
  </style>

  <link rel="preload" href="/css/app.css" as="style"
        onload="this.onload=null;this.rel='stylesheet'">
  <noscript><link rel="stylesheet" href="/css/app.css"></noscript>
</head>

The preload + onload trick downloads the stylesheet with high priority and applies it when it arrives, without blocking the first paint. The <noscript> fallback keeps the page styled when JavaScript is off.

Loading the main stylesheet asynchronously can cause a flash of unstyled content if your inline critical CSS is incomplete. Test with a throttled network and make sure everything above the fold looks right before the full file arrives.

Playing with the idea

This demo simulates a slow stylesheet by applying it after a delay. Compare the page with only the critical CSS (immediate) to the final styled page.

Critical CSS first, the rest later

During the first moments the header and text are already styled, so the user sees a meaningful page instead of a blank screen.

Finding unused CSS

Most sites ship CSS that no page uses: old components, framework parts, page-specific rules loaded everywhere. In Chrome DevTools open Coverage (Command menu โ†’ "Show Coverage"), reload, and look at the percentage of unused bytes for each stylesheet.

Ways to reduce it:

  • Use a utility framework with a build step that includes only the classes you use.
  • Split CSS per route or per component and load each only where needed.
  • Remove dead rules found with tools such as PurgeCSS, with a safelist for classes added from JavaScript.
  • Avoid including an entire icon or UI kit for three icons.

Efficient selectors? Mostly not the problem

Selector matching is fast in modern browsers. A selector like .nav > li a costs little. Worrying about it rarely matters compared with file size, request chains and DOM size. Spend your time on those first.

Fonts and CSS

Web fonts referenced in CSS are discovered only after the CSS is parsed and the elements using them are laid out. Preload the one or two fonts you need for the first screen, covered in the fonts lessons.

A quick audit

Run this in the DevTools console to list the render-blocking stylesheets and scripts of a page:

JavaScript
performance.getEntriesByType("resource")
  .filter((r) => r.renderBlockingStatus === "blocking")
  .forEach((r) => console.log(r.initiatorType, r.name, Math.round(r.duration) + " ms"));

(renderBlockingStatus is available in Chromium browsers.)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a media attribute like media="print" do to a stylesheet link?

    Stylesheets that do not match the current media do not block rendering.
  2. 2. Why is @import often slower than link tags?

    Link tags in HTML are discovered together; @import adds sequential round trips.
  3. 3. What is critical CSS?

    Inlining it removes a blocking request for the part of the page users see first.
  4. 4. Which tool shows how many bytes of a stylesheet were used on a page?

    Coverage records used and unused bytes of CSS and JavaScript.

Summary

  • Every stylesheet that applies blocks painting until it is parsed.
  • Use media attributes, avoid @import, and keep CSS small.
  • Inline a small critical CSS block and load the remainder without blocking, with a <noscript> fallback.
  • Use Coverage to find and remove unused rules.

Next: Layout Thrashing and Reflow