日本語

Loading CSS Late Increases CLS in Core Web Vitals

​​Core Web Vitals​CSS,HTML,Lighthouse,Playwright,web-vitals

When layout-defining CSS loads late, Cumulative Layout Shift (CLS) can increase. The page first renders without the CSS, and when the CSS arrives, font sizes and spacing change and the body text moves. On the page I tested, loading the CSS late with <link rel="preload"> and onload raised CLS from 0 to 0.1906. This is the situation to check if Lighthouse flagged the CSS as a "render-blocking request," you delayed loading it, and PageSpeed Insights then rated CLS as "needs improvement."

Note

The test environment was Ubuntu 24.04, Node.js 22, web-vitals 5.3.0, and Lighthouse 13.6.0. For the browser, I used Chrome for Testing 158, which comes with Playwright 1.63.0. I served the test pages from a small server (localhost:8080) and delayed only the CSS response by a set amount of time. CLS was measured with Playwright at a viewport of 412px wide and 823px high, using web-vitals' onCLS, and I read the value 1.5 seconds after the CSS arrived. I ran the measurements three times at this width. I did not test real-user data (PageSpeed Insights field data or Search Console), browsers other than Chrome, or web font loading.

The test page and CSS

The page is an article with a heading and eight paragraphs. The site-wide CSS (site.css) contains layout rules and color rules.

css
body { margin: 0; font-family: sans-serif; line-height: 1.7; }
header { height: 56px; padding: 0 16px; display: flex; align-items: center; font-weight: bold; }
main { padding: 0 16px; max-width: 720px; margin: 0 auto; }
h1 { font-size: 24px; margin: 24px 0 16px; }
body { color: #1f2a30; }
header { background: #eefafd; color: #067a98; }
h1 { color: #067a98; border-bottom: 2px solid #00c8e8; }

I made the server return this CSS after a 1-second delay, and compared pages that differed only in how the CSS was loaded. The page that loads normally with a <link> in <head> is below.

html
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>head-link</title>
<link rel="stylesheet" href="/site.css?delay=1000">
</head>

For the late-loading page, I changed the <link> to the pattern in web.dev's "Defer non-critical CSS" guide. According to that guide, rel="preload" with as="style" fetches the CSS asynchronously, and once it finishes loading, onload processes it. In this pattern, the onload handler switches rel to stylesheet, and that is when the CSS takes effect.

html
<link rel="preload" href="/site.css?delay=1000" as="style" onload="this.onload=null;this.rel='stylesheet'">

Loading late raised CLS to 0.1906; a link in head gave 0

The following is the part of the output from running the measurement script (measure.cjs) that covers these two pages. head-link is the normal load, and preload-1000 is the late-loading page.

console
$ node measure.cjs head-link,preload-0,preload-200,preload-1000,preload-3000,media-print,js-inject,body-end-link,critical-inline,critical-missing-h1,preload-nav,head-link-nav,critical-nav-late
head-link (412x823)
  CSS が届いた時刻: 1016ms  FCP: 1048ms  段落(2)の上端: 236px
  layout-shift: なし
  web-vitals onCLS: 0.0000 rating=good
…
preload-1000 (412x823)
  CSS が届いた時刻: 1008ms  FCP: 28ms  段落(2)の上端: 236px
  layout-shift: 1015ms 0.1906 [body/text]
  web-vitals onCLS: 0.1906 rating=needs-improvement

The late-loading page had its first paint (FCP) at 28ms after opening, and one layout shift was recorded at 1015ms, right after the CSS arrived. CLS was 0.1906, and web-vitals rated it needs-improvement. All three runs gave the same CLS value. web.dev's CLS guide says 0.1 or less is "good," and above 0.25 is "poor."

Two screenshots of a 412px-wide screen stacked vertically. The top one, "before CSS arrives (after 0.5 seconds)," shows a large heading and tightly spaced paragraphs in the browser's default style. The bottom one, "after CSS arrives," shows a light blue background on the header, a smaller heading, and wider line spacing in the paragraphs. (The labels in the image are in Japanese.)
The late-loading page before and after the CSS arrives. The heading and paragraphs move the moment it loads.

Before the CSS arrives, the page uses the browser's default styles. The heading is large and the paragraph line spacing is tight. Measuring positions before and after the CSS arrives, the top of the heading moved from 50px to 80px, and the top of paragraph (2) moved from 195px to 236px. The height of each paragraph also changed from 63px to 82px because the line spacing increased.

console
$ node pos.cjs preload-1000,critical-inline,critical-missing-h1,preload-nav,critical-nav-late
preload-1000
  届く前: h1:50+44 p:116+63 p:195+63 p:274+63
  届いた後: h1:80+43 p:139+82 p:236+82 p:334+82

h1:50+44 means the heading's top is at 50px and its height is 44px.

By contrast, the page that loads with a <link> in <head> renders nothing until the CSS arrives, so FCP was 1048ms. As the web.dev guide explains, CSS is a render-blocking resource, and the browser does not paint the page until it has loaded and processed the CSS. The first paint already has the CSS applied, so nothing shifted. Display starts later, but CLS is 0.

CLS was the same value whether the CSS was 200ms or 3000ms late

I changed the CSS response delay to 200ms, 1000ms, and 3000ms, and measured the late-loading page at each.

CSS delay Time CSS arrived FCP CLS
200ms 209ms 28ms 0.1906
1000ms 1008ms 28ms 0.1906
3000ms 3016ms 32ms 0.1906

CLS was 0.1906 in every case, so the length of the delay did not change the value. According to the web.dev guide, the score for a single shift is the "impact fraction" (the share of the screen covered by the moved element) multiplied by the "distance fraction" (how far it moved, as a share of the screen). Time is not part of this formula, so the score is the same no matter when the CSS arrives, as long as the movement is the same.

With a 0ms delay, the results were not consistent across three runs. Two runs recorded no layout shift, and CLS was 0. The remaining run had the CSS arrive at 11ms and FCP at 24ms, but a layout shift was recorded at 38ms, and CLS was 0.1906.

console
preload-0 (412x823)
  CSS が届いた時刻: 11ms  FCP: 24ms  段落(2)の上端: 236px
  layout-shift: 38ms 0.1906 [body/text]
  web-vitals onCLS: 0.1906 rating=needs-improvement

According to the web.dev guide, a layout shift happens when the position of a visible element changes between one rendered frame and the next. In the run where a shift was recorded, the page without CSS was painted at 24ms, and then at 38ms the CSS applied and the body text moved. Even though the CSS arrived at 11ms, the shift still counted.

Warning

If you measure on a server where CSS arrives instantly, as my local server does, you may see CLS of 0 and miss the problem. A CSS delay of only 200ms was enough to give CLS of 0.1906 in all three runs.

Every late-loading method gave the same value

There are other ways to load CSS late besides preload. I delayed the CSS by 1 second and measured these methods as well.

Method FCP CLS
<link rel="stylesheet"> in <head> 1048ms 0
rel="preload" with onload 28ms 0.1906
media="print" with onload="this.media='all'" 32ms 0.1906
Creating a <link> with JavaScript and adding it to <head> 24ms 0.1906
<link rel="stylesheet"> at the end of <body> 32ms 0.1906

Even with the <link> at the end of <body>, Chrome painted the body content that came before it first, and the shift happened when the CSS arrived. With every method that applies CSS late, the switch from the no-CSS display to the CSS display was counted as a shift. CLS was the same value in all three runs.

A bar chart comparing CLS for each way of loading CSS. The head link and the page with layout CSS in head are at 0. The preload pages with delays of 200ms, 1000ms, and 3000ms, the media="print" method, the JavaScript-injected link, and the link at the end of body are all at 0.1906. A page that forgot the heading rule is at 0.0133. A line at 0.1 marks "at or below this is good." (The labels in the image are in Japanese.)
CLS measured at a 412px-wide viewport. Every method that applies CSS late gave 0.1906.

Putting the layout rules in head brought CLS back to 0

As web.dev's critical CSS guide describes, one approach is to write the CSS needed for the first screen directly in <head> and load the rest later. I took the four layout lines from site.css and put them in a <style> block, and created a page that loads only the color and heading underline rules (colors.css) late.

html
<style>
body { margin: 0; font-family: sans-serif; line-height: 1.7; }
header { height: 56px; padding: 0 16px; display: flex; align-items: center; font-weight: bold; }
main { padding: 0 16px; max-width: 720px; margin: 0 auto; }
h1 { font-size: 24px; margin: 24px 0 16px; }
</style>
<link rel="preload" href="/colors.css?delay=1000" as="style" onload="this.onload=null;this.rel='stylesheet'">

This page had an FCP of 32ms, no layout shift was recorded, and CLS was 0. At the point before the CSS arrived (after 0.5 seconds), the heading and paragraphs were already close to their final positions. The heading underline (2px) is added later, so paragraph (2) moved down 2px, from 234px to 236px. This small shift was not recorded as a layout shift.

console
critical-inline
  届く前: h1:80+41 p:137+82 p:234+82 p:332+82
  届いた後: h1:80+43 p:139+82 p:236+82 p:334+82

If even one layout rule is missing, the shift remains

When I removed only the heading rule (h1 { font-size: 24px; … }) from the <style> block, CLS was 0.0133.

console
critical-missing-h1 (412x823)
  CSS が届いた時刻: 1007ms  FCP: 28ms  段落(2)の上端: 236px
  layout-shift: 1010ms 0.0133 [p/p/p/p/p]
  web-vitals onCLS: 0.0133 rating=good

The heading was rendered at the default size, and when the CSS arrived and made it smaller, the paragraphs below moved up 15px (paragraph (2)'s top went from 251px to 236px). This is within the "good" range, but it is not 0. If a rule is missing from <style>, the shift remains by that amount.

When a rule that hides the menu loads late, a shift occurs

I also tested a mobile page where CSS hides the navigation menu (the <ul> inside <nav>). I put the four layout lines in <style>, and the late-loading CSS (nav-only.css) contained only the rule that hides the menu and the color rules.

css
nav ul { display: none; }
body { color: #1f2a30; }
header { background: #eefafd; color: #067a98; }
h1 { color: #067a98; border-bottom: 2px solid #00c8e8; }

Until the CSS arrived, the menu links were visible and the top of the heading was at 232px. When the CSS arrived and the menu disappeared, the heading moved up to 80px, and CLS was 0.1667.

console
critical-nav-late (412x823)
  CSS が届いた時刻: 1009ms  FCP: 28ms  段落(2)の上端: 236px
  layout-shift: 1027ms 0.1667 [main/p/p]
  web-vitals onCLS: 0.1667 rating=needs-improvement

Even with the four layout lines in <head>, a single rule that hides an element in the late-loading CSS was enough to reach the "needs improvement" range. When the CSS containing the menu-hiding rule was loaded with a <link> in <head>, CLS was 0.

At desktop width, late loading also pushed CLS above 0.1

I measured the same pages once each at a viewport of 1350px wide and 940px high.

Page CLS
<link> in <head> 0
Late loading with preload 0.1660
Layout rules in <head> 0
Forgot the heading rule 0.0034

The values were lower than at mobile width, but the late-loading page was still in the "needs improvement" range.

In Lighthouse, using preload to clear the warning lowered the score

I ran Lighthouse from the command line three times per page with its default settings (mobile, 412×823). Lighthouse was set to use Playwright's Chrome through the CHROME_PATH environment variable. I added --quiet, so Lighthouse itself printed nothing. The exit: line is the exit code, and the lines below it were extracted from the result JSON with a small script.

console
$ npx lighthouse http://localhost:8080/preload-1000.html --only-categories=performance --output=json --output-path=./out/preload-1000-1.json --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/preload-1000.html
formFactor: mobile screen: 412x823
performance score: 97
first-contentful-paint: 0.6 s (score 1)
largest-contentful-paint: 0.6 s (score 1)
speed-index: 0.8 s (score 1)
total-blocking-time: 50 ms (score 1)
cumulative-layout-shift: 0.104 (score 0.88)
render-blocking-insight title: Render-blocking requests
render-blocking-insight: score=1 items=0

When run with --locale=ja, the render-blocking-insight item is labeled in Japanese; the label means "Render-blocking requests." items=0 means no files were flagged. The results for the three pages are summarized below. FCP, CLS, and the number of flagged items were the same in all three runs.

Page Performance score (3 runs) FCP CLS Render-blocking requests
<link> in <head> 100, 100, 100 0.8s 0 1 item
Late loading with preload 97, 96, 96 0.6s 0.104 None
Layout rules in <head> 99, 100, 100 0.6s 0 None

The <link> in <head> page scored 100, yet Lighthouse flagged its CSS as render-blocking. Loading the CSS late with preload removed the warning, but CLS rose to 0.104 and the score dropped to 96–97. This page also showed a Total Blocking Time of 50–100ms, so both factors contributed to the score drop. Lighthouse's CLS was different from the 0.1906 I measured with Playwright and web-vitals, but both were above 0.1. The page with layout rules in <head> had no warning and kept CLS at 0. The one run that scored 99 was the run where Total Blocking Time showed 120ms.

​Posted by 86