日本語

How Skeleton Placeholders Reduce CLS in Core Web Vitals

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

On pages that load data from an API after the page opens, such as product lists or article lists, the content below is pushed down when the data arrives, and the CLS (Cumulative Layout Shift) in Core Web Vitals goes up. On the page I tested, CLS was 0.2571 when nothing was shown while loading. With a skeleton that had the same shape as the loaded content, CLS dropped to 0.0053. When the text height was also fixed, CLS was 0.

However, the reduction only happened when the skeleton was visible from the first render. On a page where the same skeleton was added by JavaScript after 300ms, the shift caused by inserting the skeleton was counted. CLS was 0.2693, which is larger than the 0.2571 with no placeholder.

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 viewport was 412px wide and 823px high. The test page was served from a small server running in the same environment (localhost:8080). Instead of a real data fetch, the list was replaced with its content 1000ms after the page opened. I did not test React or other frameworks, Firefox or Safari, real smartphones, Lighthouse, or real user data (such as PageSpeed Insights field data or Search Console).

The page and the loading states

I placed a list of three cat cards under a header and a heading called "This week's cats." Below the list was a text section titled "Cat grooming," which was visible before loading finished. Each loaded card had a 64px square image, a name, and a one-line description.

I compared four variations of what the list showed while loading:

  • Nothing (the list is empty)
  • A single line of text reading "Loading…"
  • A rough skeleton (cards with a fixed content height of 48px, each with one gray bar)
  • A skeleton with the same shape as the loaded content (a square for the image, a bar for the name, and two bars for the description)

The same-shape skeleton used the same elements and class names as the cards, with gray bars in place of text. The bars were display: inline-block so they sat inside the text line, and I animated the background gradient so a highlight appeared to move across them.

html
<div class="card">
  <div class="sk thumb"></div>
  <div class="body">
    <p class="name"><span class="sk" style="width:5em"></span></p>
    <p class="desc"><span class="sk" style="width:100%"></span><span class="sk" style="width:60%"></span></p>
  </div>
</div>

On the page, I used PerformanceObserver to receive layout-shift entries and recorded the positions of the shifted elements before and after the shift. I also registered onCLS from web-vitals with reportAllChanges: true. Finally, I made document.visibilityState appear as hidden and fired a visibilitychange event, so that onCLS reported its final value. I measured each condition twice, and both runs gave the same values.

Nothing gives 0.2571, the same-shape skeleton gives 0.0053

The results are summarized in the chart below.

Horizontal bar chart comparing CLS for each loading state. Nothing: 0.2571. "Loading…" one line: 0.2136. Rough skeleton: 0.0235. Same-shape skeleton: 0.0053. Same-shape with fixed description height: 0.0000. Same skeleton added after 300ms: 0.2693. A dotted line marks 0.1, which web.dev treats as a good threshold.
CLS by loading state (the dotted line is 0.1, the threshold web.dev considers good)

With nothing shown while loading, the list grew to 294px when the data arrived, and the "Cat grooming" section moved from 118px to 424px, a shift of 306px. The 12px more than the list height comes from the bottom margin of the last card (margin-bottom: 12px). There was one shift, and CLS was 0.2571.

console
== none (読み込み中は何も出さない, ?mode=none)
  start afterTop=118 listH=0
  onCLS report value=0.0000 entries=0
  data t=1015 afterTop=424 listH=294
  shift t=1017 value=0.2571 hadRecentInput=false sources=1
      source after prev(y=118,h=311) cur(y=424,h=311)
  onCLS report value=0.2571 entries=1

(afterTop is the top edge of the "Cat grooming" section, and listH is the height of the list. t is the time in milliseconds since the page opened.)

The web.dev explanation of CLS says that the value of one shift is the impact fraction multiplied by the distance fraction. Applied to this page, the section (380px wide and 311px high) covered y=118 to 735 before and after the move, a range of 617px. Its share of the viewport area was 380×617 ÷ (412×823), about 0.69. The distance moved, 306px, divided by the longer viewport dimension (the 823px height here), is about 0.37. Multiplying the two gives 0.2571.

With the single "Loading…" line, the list only reserved the 24px height of that line. The section moved 270px, and CLS was 0.2136.

console
== text (「読み込み中…」の1行だけを出す, ?mode=text)
  start afterTop=154 listH=24
  onCLS report value=0.0000 entries=0
  data t=1014 afterTop=424 listH=294
  shift t=1016 value=0.2136 hadRecentInput=false sources=1
      source after prev(y=154,h=311) cur(y=424,h=311)
  onCLS report value=0.2136 entries=1

With the rough skeleton, the cards were shorter than the real cards, so the list grew from 246px to 294px and the section moved 48px. CLS was 0.0235. With the same-shape skeleton, the shift dropped to 12px, and CLS was 0.0053.

console
== rough (形を合わせていないスケルトン(中身の高さを48pxに決め打ちしたカードに帯1本), ?mode=rough)
  start afterTop=376 listH=246
  onCLS report value=0.0000 entries=0
  data t=1011 afterTop=424 listH=294
  shift t=1013 value=0.0235 hadRecentInput=false sources=1
      source after prev(y=376,h=311) cur(y=424,h=311)
  onCLS report value=0.0235 entries=1
== skeleton (読み込み後と同じ形のスケルトン, ?mode=skeleton)
  start afterTop=436 listH=306
  onCLS report value=0.0000 entries=0
  data t=1023 afterTop=424 listH=294
  shift t=1024 value=0.0053 hadRecentInput=false sources=1
      source after prev(y=436,h=311) cur(y=424,h=311)
  onCLS report value=0.0053 entries=1

In these four variations, no layout-shift was recorded during the roughly one second of loading. The bars in the skeleton were moving with the background animation, but the animation was not counted as a shift. The only recorded shift was the one caused by the data arriving.

Removing the remaining 12px from the same-shape skeleton

The 12px left in the same-shape skeleton came from the difference in list height: 306px with the skeleton and 294px with the loaded content. The skeleton's description had two lines' worth of bars, but all three real descriptions fit on one line. The body of each card shrank from 68px to the 64px of the image, so each card was 4px shorter, and across three cards that was 12px, which moved the section below up.

When I fixed the description height at two lines (40px) and clipped longer text to two lines, the list stayed at 306px both before and after loading, and the section did not move from 436px. No layout-shift entry was recorded, so CLS was 0.

css
.desc { margin: 4px 0 0; font-size: 14px; line-height: 20px; }
/* Fix the description to two lines' height and clip longer text to two lines */
.clamp .desc {
  height: 40px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
console
== skeleton-clamp (同じ形のスケルトン。紹介文を2行の高さに固定する, ?mode=skeleton&clamp)
  start afterTop=436 listH=306
  onCLS report value=0.0000 entries=0
  data t=1020 afterTop=436 listH=306
Two screenshots of the same page stacked vertically. The top one shows the loading state, where the three cards are gray-bar skeletons. The bottom one shows the loaded state, where the cards show names and descriptions. In both, the top edge of "Cat grooming" is at the dotted line at 436px.
Loading and loaded states of the skeleton with a fixed description height (the dotted line and numbers are overlaid on the image)

Conversely, when the real description is longer than the skeleton, the card grows and pushes the section below down. On a page where the third card's description wrapped to four lines, the section moved 32px without a fixed height, and CLS was 0.0149. With the height fixed, it stayed at 0.

console
== skeleton-long (同じ形のスケルトン。3枚目の紹介文が4行になる, ?mode=skeleton&long)
  start afterTop=436 listH=306
  onCLS report value=0.0000 entries=0
  data t=1019 afterTop=468 listH=338
  shift t=1021 value=0.0149 hadRecentInput=false sources=1
      source after prev(y=436,h=311) cur(y=468,h=311)
  onCLS report value=0.0149 entries=1
== skeleton-long-clamp (同じ形のスケルトン。3枚目の紹介文が長いが、紹介文を2行の高さに固定する, ?mode=skeleton&long&clamp)
  start afterTop=436 listH=306
  onCLS report value=0.0000 entries=0
  data t=1019 afterTop=436 listH=306

The web.dev guide on optimizing CLS suggests reserving space with min-height for content that loads late. I applied the same idea to the description, using min-height: 40px instead of height so the text was not clipped. Only the card with the long description grew, moving the section 40px, and CLS was 0.0191. Short-description cards kept two lines' height, so the shift was larger than with no height set at all (32px).

css
.minh .desc { min-height: 40px; }
console
== skeleton-long-minh (同じ形のスケルトン。3枚目の紹介文が長く、紹介文に min-height: 40px を付ける(切らない), ?mode=skeleton&long&minh)
  start afterTop=436 listH=306
  onCLS report value=0.0000 entries=0
  data t=1020 afterTop=476 listH=346
  shift t=1021 value=0.0191 hadRecentInput=false sources=1
      source after prev(y=436,h=311) cur(y=476,h=311)
  onCLS report value=0.0191 entries=1

In a design that shows all the text without clipping, a long description still caused a shift. On this page, CLS reached 0 only when the description height was fixed so that the length of the content did not change the list height.

Point

What reduced CLS was that the skeleton reserved space at the same height as the loaded content. Gray bars that only looked similar left a 12px shift. Fixing the parts whose height could change, such as the number of text lines, brought CLS to 0.

A skeleton added by JavaScript causes a shift itself

All the measurements so far had the skeleton in the page from the first render. In a JavaScript app where the skeleton is drawn only after the script loads, the list is still empty at first render, and the content below is pushed down when the skeleton is inserted. In the test, I reproduced this delay by inserting the skeleton with setTimeout after 300ms.

When the skeleton with a fixed description height was inserted after 300ms, the section moved from 118px to 436px, a shift of 318px, and CLS was 0.2693. The section did not move when the data arrived, but the shift from inserting the skeleton alone was larger than the 0.2571 of the page that showed nothing.

console
== skeleton-late (同じ形のスケルトン(紹介文は2行の高さに固定)を、JavaScript で 300ms 後に入れる, ?mode=skeleton&clamp&skeletonAt=300)
  start afterTop=118 listH=0
  onCLS report value=0.0000 entries=0
  skeleton t=312 afterTop=436 listH=306
  shift t=314 value=0.2693 hadRecentInput=false sources=1
      source after prev(y=118,h=311) cur(y=436,h=311)
  onCLS report value=0.2693 entries=1
  data t=1022 afterTop=436 listH=306

If the skeleton is added later, reserve the height in the container

For a setup where the skeleton cannot be included in the HTML that arrives first, I tried reserving in the list container the same height the skeleton would take.

Setting only min-height: 306px on the list container (the list height with the skeleton) still shifted the section 12px when the skeleton was inserted, so CLS was 0.0053. The container stayed at 306px, but the bottom margin of the last card (margin-bottom: 12px) fell outside the container, and the section moved by that amount.

console
== skeleton-late-reserve (同じスケルトンを 300ms 後に入れるが、一覧の入れ物に min-height: 306px を付けておく, ?mode=skeleton&clamp&skeletonAt=300&reserve)
  start afterTop=424 listH=306
  onCLS report value=0.0000 entries=0
  skeleton t=310 afterTop=436 listH=306
  shift t=312 value=0.0053 hadRecentInput=false sources=1
      source after prev(y=424,h=311) cur(y=436,h=311)
  onCLS report value=0.0053 entries=1
  data t=1016 afterTop=436 listH=306

When I added display: flow-root to the container so the last card's margin stayed inside it, and set min-height: 318px to include that margin, the section stayed at 436px both when the skeleton was inserted and when the data arrived. No layout-shift was recorded, so CLS was 0.

css
#list { display: flow-root; min-height: 318px; }
console
== skeleton-late-reserve2 (同じスケルトンを 300ms 後に入れるが、一覧の入れ物に display: flow-root と min-height: 318px を付けておく, ?mode=skeleton&clamp&skeletonAt=300&reserve2)
  start afterTop=436 listH=318
  onCLS report value=0.0000 entries=0
  skeleton t=314 afterTop=436 listH=318
  data t=1016 afterTop=436 listH=318

The reserved height, including margins, had to match the height after the skeleton was inserted. Even a 12px difference moved the content below and was recorded as a layout-shift, so CLS was not 0.

​Posted by 86