日本語

How to Keep Notice Banners from Hurting CLS in Core Web Vitals

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

A notice banner that JavaScript inserts after the page has loaded pushes down all the content below it, which raises the CLS (Cumulative Layout Shift) score in Core Web Vitals. On the page I tested, a single 96px-high banner brought CLS to 0.106. When I reserved the banner's space in advance, CLS became 0. This is how to fix the layout shift and improve CLS on a site that fetches campaign or maintenance notices from a server and shows them at the top of the page.

Note

The test environment was Ubuntu 24.04, Node.js 22, Lighthouse 13.6.0, and web-vitals 5.3.0. For the browser, I used Chrome for Testing 153, which is bundled with Playwright 1.63.0. I served the test pages from a small server running in the test environment (localhost:8080), and I delayed only /notice.json, the endpoint that returns the notice content, by 0.8 seconds. I report CLS both from Playwright, which opened the page at 412px wide and 823px high, and from Lighthouse run from the command line with its default settings (mobile, the same 412×823). I did not test real-user data (such as field data from PageSpeed Insights or Search Console), desktop settings, or results on a live site.

Inserting the banner raised CLS to 0.106

The test page is just a header followed by a heading and paragraphs of body text. After the page loads, it requests /notice.json, and when the response arrives it inserts a banner div right after the header.

html
<style>
  …
  header { height: 56px; padding: 0 16px; display: flex; align-items: center; background: #eefafd; font-weight: bold; }
  …
  .notice { box-sizing: border-box; padding: 12px 16px; background: #fff3c4; font-size: 16px; line-height: 24px; }
</style>
…
<header>猫の手日記</header>
<main>
<h1>今日の記事</h1>
<p>本文の段落です(1)。…</p>
…(段落は全部で8つ)
</main>
<script>
fetch('/notice.json').then((r) => r.json()).then((data) => {
  const div = document.createElement('div');
  div.className = 'notice';
  div.append(Object.assign(document.createElement('strong'), { textContent: data.title }), document.createElement('br'), data.text);
  document.querySelector('header').after(div);
});
</script>

I opened this page with Playwright, waited 3 seconds, and printed the layout shift records the browser logged (the layout-shift entries) and the value that web-vitals's onCLS reported. The measurement script (measure.cjs) collects layout-shift entries with a PerformanceObserver, calls web-vitals' onCLS, and prints the values. This section shows the output for the page before any fix (before), from a run that measured all four pages together.

console
$ node measure.cjs before reserve reserve-short fixed
before: h1.top 77.4375 -> 173.4375, notice 412x96 at top=56 position=static
  layout-shift entries: [{"t":823,"value":0.1057,"hadRecentInput":false,"sources":["MAIN [77.4375,745.5625]->[173.4375,649.5625]"]}]
  web-vitals onCLS: 0.1057 rating=needs-improvement
…

About 0.8 seconds after the page was opened, a single layout shift occurred. The element that moved (sources) was main. The top of the heading moved from 77px to 173px, a 96px drop equal to the banner's height. CLS was 0.1057. web-vitals rated it needs-improvement, which exceeds the 0.1 threshold for a "good" CLS in web.dev's CLS guide. In web-vitals 5.3.0, the boundaries are fixed as CLSThresholds = [0.1, 0.25].

Two phone-width screenshots (412px wide) stacked vertically, before and after the banner arrives. The top shows the header directly above the heading. The bottom shows a yellow notice banner between the header and the heading, with the heading pushed down by 96px (the banner's labels are in Japanese)
When the banner arrives, the heading and body text are pushed down together by 96px

Lighthouse also reported a CLS of 0.106 for the same page. I used a small script (summarize.mjs) to extract the score, the CLS, and the shifted elements from the JSON results.

console
$ npx lighthouse http://localhost:8080/before.html --only-categories=performance --output=json --output=html --output-path=./out/before --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/before.html
screenEmulation: 412x823 mobile=true
performance score: 97
CLS: 0.106 (numericValue 0.1057, score 0.88)
layout-shifts: score=0 title="Avoid large layout shifts"
  - body > main  score=0.1057
cumulative-layout-shift weight: 25

The performance score was 97, and the CLS audit scored 0.88. The "Avoid large layout shifts" audit lists body > main as the shifted element. The banner itself did not move; the content it pushed down did.

Why one banner raises CLS this much

The layout shift score that contributes to CLS is defined in web.dev's explanation of the formula as "impact fraction × distance fraction." The impact fraction is the combined area that the moving element occupied on screen before and after it moved, divided by the area of the viewport. The distance fraction is the distance the element moved, divided by the larger of the viewport's width and height.

Calculating from the main position in the measurement matches the value the browser reported:

text
before: impact fraction = (823 - 77.4375) / 823 = 0.9059, distance fraction = 96 / 823 = 0.1166, score = 0.1057

Because main spans the full width of the screen, the impact fraction equals the height from the top of the heading to the bottom of the screen, as a share of the viewport height. The banner was inserted right below the header, near the top of the screen, so the impact fraction came out to about 0.91. web.dev's guide to optimizing CLS notes that content inserted near the top of the viewport tends to cause larger shifts than content inserted in the middle, because more elements sit below it.

Fix 1: Reserve the banner's space in advance

The first fix that web.dev's guide lists is to reserve space for late-loading content from the first render. I put an empty div in the HTML and gave it a min-height in CSS to hold the banner's height. Instead of creating a new element, the JavaScript now only fills in the existing div.

html
<style>
  …
  .notice { box-sizing: border-box; padding: 12px 16px; background: #fff3c4; font-size: 16px; line-height: 24px; }
  .notice { min-height: 96px; }
</style>
…
<header>猫の手日記</header>
<div class="notice"></div>
<main>…</main>
<script>
fetch('/notice.json').then((r) => r.json()).then((data) => {
  const slot = document.querySelector('.notice');
  slot.append(Object.assign(document.createElement('strong'), { textContent: data.title }), document.createElement('br'), data.text);
});
</script>

After re-measuring, the heading stayed at 173px the whole time, and there were no layout shift records at all. (This is the part for this page from the same measure.cjs output as in the first section. The same applies to the sections below.)

console
reserve: h1.top 173.4375 -> 173.4375, notice 412x96 at top=56 position=static
  layout-shift entries: []
  web-vitals onCLS: 0.0000 rating=good

Lighthouse also reported a CLS of 0, and the performance score rose to 100.

console
$ npx lighthouse http://localhost:8080/reserve.html --only-categories=performance --output=json --output=html --output-path=./out/reserve --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/reserve.html
screenEmulation: 412x823 mobile=true
performance score: 100
CLS: 0 (numericValue 0.0000, score 1)
layout-shifts: score=null title="Avoid large layout shifts"
cumulative-layout-shift weight: 25
Two phone-width screenshots (412px wide) stacked vertically, before and after the banner arrives. The top shows an empty yellow banner area below the header. The bottom shows text filled into that same area, and the heading stays in the same position as above (the banner's labels are in Japanese)
With the space reserved, the heading does not move when the banner arrives

Until the banner arrives, an empty yellow area is visible. The web.dev guide says that if the reserved space is removed when an ad does not arrive, a shift happens just as it does when inserting. The same applies here: the design keeps the space even on days with no notice.

Warning

Set the reserved height to match how many lines the banner takes up at phone width.

I also tested a page that reserved only one line of banner height (min-height: 48px). At 412px wide, the banner wrapped to three lines (96px), so the body text moved down by the missing 48px.

console
reserve-short: h1.top 125.4375 -> 173.4375, notice 412x96 at top=56 position=static
  layout-shift entries: [{"t":818,"value":0.0495,"hadRecentInput":false,"sources":["MAIN [125.4375,697.5625]->[173.4375,649.5625]"]}]
  web-vitals onCLS: 0.0495 rating=good

CLS was 0.0495. That is within the "good" range of 0.1 or below, but it was not 0. Lighthouse reported a CLS of 0.049 for this page. The distance moved halved from 96px to 48px, so the score roughly halved as well.

Fix 2: Overlay the banner on top of the content

If you cannot reserve space, you can instead take the banner out of the normal flow and overlay it on the content. web.dev's section on banners that appear later at the top or bottom of the page also suggests that, where it fits, you remove the banner from the document flow and place it over the content. I kept the same JavaScript as the original page and added position: fixed in CSS, pinning the banner to the bottom edge of the screen.

css
.notice { position: fixed; left: 0; right: 0; bottom: 0; }
console
fixed: h1.top 77.4375 -> 77.4375, notice 412x96 at top=727 position=fixed
  layout-shift entries: []
  web-vitals onCLS: 0.0000 rating=good

The heading stayed at 77px, and there were no layout shift records. Lighthouse also reported a CLS of 0 and a performance score of 100.

console
$ npx lighthouse http://localhost:8080/fixed.html --only-categories=performance --output=json --output=html --output-path=./out/fixed --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/fixed.html
screenEmulation: 412x823 mobile=true
performance score: 100
CLS: 0 (numericValue 0.0000, score 1)
layout-shifts: score=null title="Avoid large layout shifts"
cumulative-layout-shift weight: 25

However, the banner sits over the bottom 96px of the screen (from 727px down), hiding the body text there. It covers just over a tenth of the 823px-high viewport I measured. Even with CLS at 0, part of the content near the bottom of the screen is still hidden behind the banner.

Values before and after the fix

The values for the four pages I tested are shown below. The Playwright values are from web-vitals' onCLS, and the Lighthouse values are from the default mobile settings.

Page CLS (Playwright) CLS (Lighthouse) Performance score (Lighthouse)
Banner inserted (before fix) 0.1057 0.106 97
96px space reserved 0 0 100
48px space reserved 0.0495 0.049 100
Banner overlaid at bottom 0 0 100

On every page, the two measurement methods agreed, apart from differences in displayed decimal places. The page before the fix had a CLS above 0.1, yet its performance score was still 97. In Lighthouse 13.6.0, CLS is only one of several metrics that feed the performance score (weight 25), so the effect of the banner barely shows in the score. Check the CLS value itself.

​Posted by 86