日本語

Does a Sticky Header Increase CLS in Core Web Vitals?

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

Even when I built a sticky header that stays at the top of the screen as you scroll, using position: sticky, CLS (Cumulative Layout Shift) in Core Web Vitals did not increase. In my test environment, I scrolled 100px at a time ten times with the mouse wheel. No layout shift was recorded, and CLS stayed at 0.

CLS increased only when I scrolled past 100px and JavaScript switched the header to position: fixed. The header left the normal flow, so the content moved up by 64px, and CLS became 0.0778. I also show the results for changing the height of a sticky header and for hiding the header, with the numbers.

Note

The test environment was Ubuntu 24.04, Node.js 22, and web-vitals 5.3.0. The browser was Chromium 158 (headless) bundled with Playwright 1.63.0, and the screen was 412px wide and 823px high. I scrolled with Playwright's page.mouse.wheel and keyboard.press('PageDown'). I did not test scrolling with a finger on a phone, the released Chrome or browsers other than Chromium, Lighthouse, or real user data (PageSpeed Insights field data or Search Console).

The page used for the test

The page had 12 pairs of paragraphs and cards below a 64px-high header. I switched the way the header was written with the URL parameter mode.

css
/* sticky: keep it in the normal flow and stick it to the top of the screen */
.sticky header { position: sticky; top: 0; }
/* fixed: place it outside the normal flow from the start, and reserve the space with padding on body */
.fixed body { padding-top: 64px; }
.fixed header { position: fixed; top: 0; left: 0; right: 0; }

I also prepared a mode that changes the header's class with JavaScript when scrolling. In every case, the class is added once, when scrollY exceeds 100px in a scroll event. The following code is a shortened version of what I used in the test.

js
addEventListener('scroll', () => {
  if (changed || scrollY <= 100) return;
  hd.classList.add('is-fixed'); // depending on the mode, add is-small, is-hidden-top, etc.
  changed = true;
}, { passive: true });

In the page, I received layout-shift entries with PerformanceObserver and recorded the time, the value, hadRecentInput, and the shifted elements. I also registered onCLS from web-vitals with reportAllChanges: true and recorded the CLS values it reported. The measurement was done with a Playwright script. After opening the page and waiting 1 second, I scrolled 100px at a time ten times with the wheel, with 50ms between each scroll. The times in the output below are all milliseconds since the page was opened.

Here is a summary of CLS when scrolling with the wheel:

Horizontal bar chart of CLS for each header approach. A sticky header, a fixed header with padding, a sticky header hidden with top, a shrinking sticky header, and a fixed header with a same-height spacer are all 0.0000. A shrinking sticky header with overflow-anchor: none is 0.0292, and switching to fixed on scroll is 0.0778. The labels in the image are in Japanese.
CLS for each header approach when scrolling 100px at a time ten times with the wheel

A sticky header caused no shift when scrolling

Even with the header set to position: sticky, no shift was recorded, the same as with a normal header (static) or a fixed header.

console
== static (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=static)
  shift なし
  onCLS report at=45 value=0.0000 entries=[]
== sticky (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=sticky)
  shift なし
  onCLS report at=46 value=0.0000 entries=[]
== fixed (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=fixed)
  shift なし
  onCLS report at=39 value=0.0000 entries=[]

When you scroll, the content moves up on screen. A sticky header, on the other hand, moves down within the page. The draft specification for the Layout Instability API only counts an element as a shift candidate when it moves within the viewport and also moves within the whole page (the initial containing block). The content does not move within the page, and the sticky header does not move within the viewport, so under the specification neither one counts as a shift. (How Chromium treats sticky elements is covered in a later section.)

Switching to fixed with JavaScript raised CLS to 0.0778

With the approach where the header switches to position: fixed when scrolling, a shift was recorded.

css
header.is-fixed { position: fixed; top: 0; left: 0; right: 0; }
console
== js-fixed (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=fixed)
  クラスを付けた t=1091 scrollY=200
  shift t=1092 value=0.0778 hadRecentInput=false scrollY=200 sources=main#main(0→0, h 823→823)
  onCLS report at=42 value=0.0000 entries=[]
  onCLS report at=1093 value=0.0778 entries=[1092]

When the header leaves the normal flow, the main element below it moves up by the height of the header. This shift happened right after the class was added, and onCLS reported 0.0778.

The main#main(0→0, h 823→823) in the output is the rectangle of the visible part of main. In this test page, main fills the screen, so its rectangle did not change before and after. I measured the amount of movement separately. I set the scroll position to 90px, where the class is not added automatically, and then added the same class with JavaScript directly. The top edge of main went from -14px to -78px, so it moved up 64px.

With the wheel, scrolling input does not set hadRecentInput. The draft specification says that a shift less than 500ms after an input such as mousedown or keydown gets hadRecentInput=true, but events that only start or continue a scroll are not included in those inputs. So this shift during scrolling was counted in CLS as it is.

When I made the header fixed and, at the same time, placed an empty 64px-high frame where the header had been, the content did not move and no shift was recorded.

console
== js-fixed-spacer (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=fixed)
  クラスを付けた t=1090 scrollY=200
  shift なし
  onCLS report at=42 value=0.0000 entries=[]

A sticky header stays in the normal flow and sticks to the top of the screen, so you don't need to prepare such a frame with JavaScript.

When I shrank a sticky header, scroll anchoring cancelled the shift

I also tried shrinking the header from 64px to 40px when scrolling. A sticky header is in the normal flow, so when it shrinks, the content's position within the page also moves up by the amount it shrank. However, no shift was recorded.

console
== sticky-shrink (ホイールで 100px×10回、最後の scrollY=976、ヘッダーの position=sticky)
  クラスを付けた t=1090 scrollY=176
  shift なし
  onCLS report at=41 value=0.0000 entries=[]

Only this mode ended with a scrollY of 976, while the other modes ended at 1000. The browser moved the scroll position back by the 24px the header shrank, so the content did not move on screen. This is scroll anchoring, a standard browser adjustment. As described in MDN's overflow-anchor page, you can stop it with overflow-anchor: none.

When I did the same operation with overflow-anchor: none set on html and body, scrollY stayed at 1000, and the content's movement was counted as CLS 0.0292. As in the previous section, I set the scroll position to 90px and then added the class directly to measure. The content had moved up by 24px. With scroll anchoring working, the content's position did not change, and scrollY went back from 90 to 66.

console
== sticky-shrink&anchor=none (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=sticky)
  クラスを付けた t=1090 scrollY=200
  shift t=1091 value=0.0292 hadRecentInput=false scrollY=200 sources=main#main(0→0, h 823→823)
  onCLS report at=49 value=0.0000 entries=[]
  onCLS report at=1091 value=0.0292 entries=[1091]

On the other hand, with the approach that switches to fixed, even with scroll anchoring working, scrollY stayed at 1000 and the shift was counted as 0.0778 (the output in the previous section). The draft CSS Scroll Anchoring specification lists cases where adjustment is not done (suppression triggers). Among them are when an element's position changes so that it becomes absolutely positioned, and when the scroll position is 0.

When I opened the page, waited 1 second, and then, without scrolling, increased the header height from 64px to 112px, the same 0.0529 was counted for both the sticky header and a normal header.

console
== sticky-grow (スクロールしない、最後の scrollY=0、ヘッダーの position=sticky)
  クラスを付けた t=1000 scrollY=0
  shift t=1009 value=0.0529 hadRecentInput=false scrollY=0 sources=main#main(76→124, h 747→699)
  onCLS report at=42 value=0.0000 entries=[]
  onCLS report at=1010 value=0.0529 entries=[1009]
== static-grow (スクロールしない、最後の scrollY=0、ヘッダーの position=static)
  クラスを付けた t=1000 scrollY=0
  shift t=1010 value=0.0529 hadRecentInput=false scrollY=0 sources=main#main(76→124, h 747→699)
  onCLS report at=45 value=0.0000 entries=[]
  onCLS report at=1011 value=0.0529 entries=[1010]

When the header height changes with the scroll position at 0, the same value was counted as a shift where the content moves down, whether or not the header is sticky.

Movement of the sticky header itself was not counted

I tried three ways to hide the header when scrolling: moving it off-screen with top: -64px on the sticky header, hiding half of it with top: -32px, and shifting it up with transform: translateY(-100%). None of them recorded a shift.

console
== sticky-hide-top (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=sticky)
  クラスを付けた t=1099 scrollY=200
  shift なし
  onCLS report at=53 value=0.0000 entries=[]
== sticky-hide-half (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=sticky)
  クラスを付けた t=1089 scrollY=200
  shift なし
  onCLS report at=41 value=0.0000 entries=[]
== sticky-hide-transform (ホイールで 100px×10回、最後の scrollY=1000、ヘッダーの position=sticky)
  クラスを付けた t=1088 scrollY=200
  shift なし
  onCLS report at=40 value=0.0000 entries=[]

The draft specification says that changing only transform does not make an element count as having shifted, so it compares positions that exclude transform. For changes to top, the layout_shift_tracker.cc file in the Chromium source (main branch as of November 2026) leaves sticky elements out of the shift calculation. Its comments say to ignore sticky elements that move on scroll, and to find a way to detect shifts of these elements, which marks it as future work. This is Chromium's current implementation, not a behavior decided by the specification.

web.dev's explanation of CLS recommends using transform: translate() instead of changing top and similar properties when you move an element with an animation.

Pressing PageDown to scroll removed the shift from switching to fixed

I also measured scrolling by pressing the PageDown key twice instead of using the wheel. As with the wheel, the sticky header had no shift. What differed was the approach that switches to fixed.

console
== js-fixed (PageDown×2回、最後の scrollY=1440、ヘッダーの position=fixed)
  keydown t=1012 key=PageDown
  keydown t=1316 key=PageDown
  クラスを付けた t=1059 scrollY=144
  shift t=1060 value=0.0778 hadRecentInput=true lastInputTime=1011 scrollY=144 sources=main#main(0→0, h 823→823)
  onCLS report at=44 value=0.0000 entries=[]

The shift's size was the same 0.0778 as with the wheel, but hadRecentInput=true was set. onCLS reported 0 at first and did not report a new value afterward. lastInputTime is 1011, almost the same time as the first PageDown keydown (t=1012). The draft specification lists keydown among the inputs that feed hadRecentInput. In the web-vitals source (LayoutShiftManager.ts), onCLS skips layout shifts that have hadRecentInput set, so this shift was not included in CLS.

Point

Just because CLS was 0 when I tested with the keyboard does not mean the switch to fixed causes no shift. When I scrolled with the wheel, the same shift was counted.

​Posted by 86