HTTP, TLS & Web Performance

Render-Blocking Resources and How to Tame Them

Find the stylesheets and scripts that delay your first paint, and learn which attributes and techniques get them out of the way.

Key takeaways

  • CSS blocks rendering because the browser avoids showing unstyled content that it would have to redraw moments later.
  • A classic script without defer or async stops HTML parsing while it downloads and runs.
  • Use defer for your own scripts that need the document, and async only for independent ones where order doesn't matter.
  • Split or inline the styles needed for the first screen and load the rest without blocking.
  • Every extra server you depend on early adds connection setup time before its file can even start.

A render-blocking resource is a file the browser insists on fetching and processing before it will draw the page. Stylesheets in the head block by default, and so do ordinary scripts that appear there. You tame them by sending less, deferring what isn't needed for the first screen, and making sure the essential files arrive early.

The goal is a short path from "HTML received" to "something useful on screen". Everything on that path is worth questioning.

How blocking works

The browser reads HTML from top to bottom and builds the Document Object Model, or DOM, as it goes. From CSS it builds a second structure describing styles. It needs both before it can calculate layout and paint. The full sequence is covered in how a browser turns code into pixels.

A stylesheet therefore delays the first paint. A classic script is stricter. Because it may call document.write or inspect what's been parsed so far, the parser stops at the tag, waits for the file, runs it, and only then continues. If a stylesheet is still loading, the script also waits for that, since it might ask about computed styles.

The path to first paint
1 Receive HTML Parsing starts as bytes arrive. 2 Fetch CSS Painting waits for these files. 3 Run blocking scripts Parsing pauses at each one. 4 Build layout Sizes and positions are calculated. 5 Paint The visitor sees content. Receive HTML Parsing starts as bytes arrive. 1 Fetch CSS Painting waits for these files. 2 Run blocking scripts Parsing pauses at each one. 3 Build layout Sizes and positions are calculated. 4 Paint The visitor sees content. 5

Browsers soften this with a preload scanner that looks ahead in the HTML and starts downloads early. It can't discover files that are referenced only inside CSS or created by JavaScript.

Finding the culprits

Start with a Lighthouse run in Chrome's developer tools, which flags render-blocking requests. Then open the Network panel and look at what completes before the first paint marker in the Performance panel.

Ask three questions about each blocking file:

  1. Does the first screen need any of it?
  2. If so, how much of it?
  3. Could it arrive sooner?

A coverage tool in the developer tools can show how much of each CSS and JavaScript file was used during load. Treat the number with care, because rules for hover states, menus and other pages show up as unused even though you need them.

Scripts: defer, async and modules

These attributes change when a script downloads and runs.

Script tag Blocks parsing Runs when Order kept
Plain script src Yes Immediately at that point Yes
defer No After parsing, before DOMContentLoaded Yes
async Not while downloading As soon as it arrives No
type="module" No Deferred by default Yes
<script src="/js/app.js" defer></script>
<script src="/js/analytics.js" async></script>
<script type="module" src="/js/menu.js"></script>

defer is the safe default for application code. async suits scripts that stand alone, such as some measurement snippets. If async script B relies on A, it will fail some of the time, depending on which arrives first, which makes the bug hard to reproduce.

Neither defer nor async applies to inline classic scripts. Putting scripts at the end of body is the older solution and still works, though defer in the head lets the download begin sooner.

Remember that a deferred script still costs processing time when it runs. A very large bundle can leave a painted page that doesn't respond to taps. Remove unused libraries and split the rest, loading features on demand as described in lazy loading done right.

Stylesheets: trim, split and prioritize

First, make the CSS smaller. Delete rules for features that no longer exist, minify the file with a CSS beautifier and minifier, and serve it with gzip or Brotli compression.

Second, use the media attribute. A stylesheet whose media query doesn't match is still downloaded, at low priority, but doesn't block painting:

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

Third, consider critical CSS: the rules needed to style the first screen, placed in a style element in the head. The full stylesheet then loads without blocking. A widely used approach is:

<link rel="stylesheet" href="/css/rest.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="/css/rest.css"></noscript>

This relies on an inline event handler, which a strict content security policy will refuse. In that case, attach the handler from an external script. Inlined CSS also can't be cached separately and adds weight to every HTML response, so keep it small and generate it automatically. On a site with one modest stylesheet, critical CSS may not be worth the maintenance.

Avoid @import inside stylesheets. The browser only learns about the imported file after downloading the first one, creating a chain. Use separate link tags or combine the files at build time.

Fonts and third parties

Web fonts don't block the whole page, but they can hide text. The font-display descriptor controls this. swap shows fallback text immediately and switches when the font arrives. optional gives the font only a very short window, then sticks with the fallback for that view. More on the choices is in choosing and loading web fonts.

Files hosted elsewhere need a DNS lookup, a TCP or QUIC connection and a TLS setup before the first byte. Resource hints help:

<link rel="preconnect" href="https://assets.example.com">
<link rel="preload" href="/fonts/body.woff2" as="font" type="font/woff2" crossorigin>

preconnect opens a connection early. preload fetches a specific file you know you'll need. The crossorigin attribute is required on font preloads even for your own domain, or the font is downloaded twice. Use hints sparingly, since each one competes with other early work.

Ways to get a file off the critical path
Scripts Add defer to application code Add async to independent snippets Split features into on-demand modules Delete libraries you no longer use Styles Remove dead rules and minify Mark conditional sheets with media Inline a small critical set Replace @import chains Fonts and others Choose a font-display value Preload only key font files Preconnect to essential origins Self-host where it makes sense Scripts Add defer to application code Add async to independent snippets Split features into on-demand modules Delete libraries you no longer use Styles Remove dead rules and minify Mark conditional sheets with media Inline a small critical set Replace @import chains Fonts and others Choose a font-display value Preload only key font files Preconnect to essential origins Self-host where it makes sense

Third-party tags deserve a regular review. A synchronous script from another company means your page waits on their server. If it stalls, so do you. Load such tags asynchronously and ask whether each still earns its place.

A worked example

Say a page's head contains a full component library stylesheet, a second theme file imported from inside it, a utility script, a slider script, a measurement tag from another company and two font families.

A sensible cleanup goes in this order. Replace the @import with a second link tag so both stylesheets are discovered at once, or merge them in your build. Remove the library components the site never uses. Add defer to the utility and slider scripts, keeping them in dependency order. If the slider appears on only one template, stop loading it everywhere. Mark the measurement tag async. Reduce the fonts to the weights the design uses, set font-display: swap, and preload only the body font.

After each step, reload and test the page's interactive parts. Small, separate changes make it obvious which one broke a menu. When you finish, compare the median of several lab runs with your starting point, and keep the changes that earned their complexity.

None of these steps needs a new framework or a rebuild of the site. Most are edits to the template that produces the head.

Server-side helpers

Delivery settings can shorten the wait for the files that must block. HTTP 103 Early Hints lets a server send preload or preconnect suggestions while it is still preparing the final page, which helps when generating the HTML is slow. Support differs among browsers, servers and delivery networks, so confirm yours.

Long cache lifetimes on versioned CSS and JavaScript mean that returning visitors don't wait on the network for them at all. A blocking stylesheet that is already stored on the device costs very little compared with one fetched across a slow mobile link.

What can go wrong

  • A flash of unstyled content. Your critical CSS misses something visible. Regenerate it after design changes and test several screen widths.
  • "Not defined" errors. An inline script uses a library that is now deferred. Defer the dependent code too, or wait for DOMContentLoaded.
  • Shifting text. The fallback font and web font have very different widths. Choose a closer fallback or use CSS font metric overrides.
  • Preload warnings in the console. You preloaded something the page didn't use soon, or the as value doesn't match.
  • Optimizer plugins that reorder files. Automatic combining and deferring can break dependencies. Change one setting at a time.

Checking the result

Measure before and after under the same conditions: same device profile, same network throttling, several runs. Watch First Contentful Paint for the direct effect and Largest Contentful Paint for the visitor's experience. Then confirm on real traffic, since your test machine is probably faster than many phones.

If painting is quick but the server response itself is slow, the fix lies elsewhere; see why websites load slowly.

Common questions

Should I combine all CSS and JavaScript into one file?

With HTTP/2 and HTTP/3, many requests can share one connection, so extreme bundling matters less than it did. A few sensibly grouped files often cache better than one giant bundle. Hundreds of tiny files still carry overhead.

Is a perfect score necessary?

No. A tool's warning is a prompt to investigate. If a small stylesheet blocks for a barely measurable time, complicating your build to remove the warning is a poor trade.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in HTTP, TLS & Web Performance →

Latest news on this topic