English

Lighthouseでページの幅が画面に合わないという指摘を直す方法

​​Lighthouse​CSS,HTML,Playwright,トラブルシュート

Lighthouse で「Content is not sized correctly for the viewport」(日本語のレポートでは「コンテンツのサイズとビューポートのサイズが一致していません」)と出たら、ページの中にスマホの画面より右にはみ出している要素があるか、meta viewport がないために、ページが画面より広い幅で描かれています。Lighthouse と同じスマホの幅で開くと、ページの幅が画面の幅より広く、横にスクロールできる状態のときに出る指摘です。はみ出している要素を見つけて、画面の幅に収まるように CSS を直せば消えます。

私は検証環境で、幅を 640px に固定した表と、折り返さない長いコマンドを置いたページを作りました。Lighthouse 11.7.1 では、画面の幅 412px に対して、ページの幅が 656px になり不合格でした。表の幅を 100% にし、コマンドの箱に overflow-x: auto を付けると、ページの幅は 412px になり、指摘は消えました。

この監査は PWA のカテゴリーの中にあり、Lighthouse 12.0.0 で PWA のカテゴリーごと削除されています。指摘が出るのは Lighthouse 11 までです。

補足

検証環境は Ubuntu 24.04、Node.js 22.23.5 です。Lighthouse は、この監査がある最後の版の 11.7.1 と、12.8.2、最新の 13.6.0 を npm で入れ、CLI から使いました。ブラウザは、CHROME_PATH で指定した Google Chrome for Testing 158 を、ヘッドレスモードで動かしています。特に断りのない結果は、11.7.1 の PWA のカテゴリーだけを、初期設定のモバイル向けの条件(画面の幅 412px)で実行したものです。ページは検証環境で作り、Node.js で書いた小さなサーバーで 127.0.0.1 から配信しました。日本語のレポートの文言は、Lighthouse の翻訳ファイルで確かめました。Chrome の DevTools の Lighthouse パネルと PageSpeed Insights、実機のスマホでの表示は試していません。

表とコマンドが画面より広いページで試した

検証には、猫のごはんの量を記録したページを作りました。<head> には <meta name="viewport" content="width=device-width, initial-scale=1"> を入れてあります。表には幅 640px を指定し、コマンドを書いた <pre> には何も指定していません。

css
body { margin: 0; padding: 16px; font-family: sans-serif; line-height: 1.6; }
.log { width: 640px; border-collapse: collapse; }
.log th, .log td { border: 1px solid #9aa5ab; padding: 4px 8px; }
.code { background: #eefafd; padding: 8px; }
html
<pre class="code">feed --morning 30 --night 30 --cat tama --note "完食" --weight 4.2 --date 2026-11-21</pre>

ページを 127.0.0.1:8000 から配信し、Lighthouse 11.7.1 を次のように実行しました。

bash
npx lighthouse http://127.0.0.1:8000/bad.html --only-categories=pwa \
  --output=json --output=html --output-path=./out/bad \
  --chrome-flags="--headless=new --no-sandbox" --quiet

検証では、環境変数 CHROME_PATH に Chrome for Testing の実行ファイルの場所を指定してから実行しました。--no-sandbox は、検証用の環境で Chrome を起動するために付けたものです。

実行すると、PWA の「PWA Optimized」(日本語のレポートでは「PWA の最適化」)の欄に指摘が出ました。

Lighthouse のレポートで「Content is not sized correctly for the viewport」を開いた画面。赤い三角の印と、The viewport size of 656px does not match the window size of 412px という説明が出ている
直す前のページのレポート。ページの幅(656px)と画面の幅(412px)が並んで出る

JSON のレポートから、点数と content-width の結果を自作の小さなスクリプトで取り出すと、次のとおりです。

text
pwa score: 25  failed: installable-manifest, splash-screen, themed-omnibox, content-width, maskable-icon
[content-width] weight=1 group=pwa-optimized score=0 (binary)  title: Content is not sized correctly for the viewport  explanation: The viewport size of 656px does not match the window size of 412px.

PWA の点数が低いのは、このページにマニフェストなどがないためです。この記事では content-width だけを見ます。

説明に出る2つの数字の意味

説明の「The viewport size of 656px does not match the window size of 412px.」は、日本語のレポートでは「ビューポートのサイズ(656 ピクセル)がウィンドウのサイズ(412 ピクセル)と一致していません。」と出ます。

Lighthouse は、viewport-dimensions.js でページの中の window.innerWidth と window.outerWidth を読みます。監査のソース(content-width.js)では、この2つが等しければ合格、違えば不合格にしています。公式の解説にも、window.innerWidth が window.outerWidth と等しくないと不合格になる、と書かれています。

412px は、Lighthouse がまねているスマホの画面の幅です。656px は、ページの中身がいちばん右まで届いている位置でした。表の左端は body の padding の 16px のところにあるので、640px の表の右端は 16+640=656px になります。この検証では、ページの中で画面からいちばん右にはみ出した要素の右端が、そのまま説明の最初の数字(ビューポートのサイズ)に出ていました。

はみ出している要素を Playwright で探す

レポートには、どの要素がはみ出しているかは出ません。そこで、Lighthouse と同じ画面の条件(幅 412px、モバイル)で Playwright からページを開き、画面の幅より右に出ている要素を一覧にするスクリプトを書きました。

js
// find-wide.cjs
const { chromium } = require('playwright');
(async () => {
  const b = await chromium.launch({ channel: 'chromium' });
  const ctx = await b.newContext({ viewport: { width: 412, height: 823 }, deviceScaleFactor: 1.75, isMobile: true, hasTouch: true });
  const p = await ctx.newPage();
  for (const name of process.argv.slice(2)) {
    await p.goto('http://127.0.0.1:8000/' + name);
    const r = await p.evaluate(() => {
      const cw = document.documentElement.clientWidth;
      const wide = [...document.body.querySelectorAll('*')]
        .map(el => {
          const right = Math.round(el.getBoundingClientRect().right);
          const parentRight = el.parentElement.getBoundingClientRect().right;
          const contentRight = Math.round(el.getBoundingClientRect().left + el.scrollWidth);
          const name = `${el.tagName.toLowerCase()}${el.className ? '.' + el.className : ''}`;
          if (right > cw && parentRight <= cw) return `${name}  box right=${right}`;
          if (right <= cw && contentRight > cw && getComputedStyle(el).overflowX === 'visible') return `${name}  content right=${contentRight} (box right=${right})`;
          return null;
        })
        .filter(Boolean);
      return { innerWidth: innerWidth, outerWidth: outerWidth, clientWidth: cw, scrollWidth: document.documentElement.scrollWidth, wide };
    });
    console.log(`== ${name}  innerWidth=${r.innerWidth} outerWidth=${r.outerWidth} clientWidth=${r.clientWidth} scrollWidth=${r.scrollWidth}`);
    for (const w of r.wide) console.log('  ' + w);
    if (!r.wide.length) console.log('  (no element wider than clientWidth)');
  }
  await b.close();
})();

はみ出し方は2種類を拾っています。1つは、要素の箱そのものが画面の右に出ているものです(box right)。親の箱がはみ出していない、いちばん外側の要素だけを出しています。もう1つは、箱は画面に収まっているのに、中の文字が箱の外に出ているものです(content right)。ページは、Lighthouse のときと同じく 127.0.0.1:8000 から配信しておきます。直す前のページで実行した結果は、次のとおりです。

bash
node find-wide.cjs bad.html
text
== bad.html  innerWidth=656 outerWidth=412 clientWidth=412 scrollWidth=656
  table.log  box right=656
  pre.code  content right=570 (box right=396)

表は箱ごと 656px まで出ていました。<pre> の箱は 396px で画面に収まっていますが、折り返さないコマンドの文字が 570px まで出ていました。innerWidth は Lighthouse の説明と同じ 656 でした。

chromium.launch() に channel: 'chromium' を付けているのは、Lighthouse に指定したものと同じ Chrome で開くためです。Playwright のドキュメントによると、Playwright はヘッドレスのとき、初期設定では通常の Chromium とは別の「chromium headless shell」を使います。付けずに開いたときは、同じページでも <pre> の等幅の文字の幅が違い、Lighthouse と合わない結果が出ました。

表の幅を 100% にし、コマンドは箱の中でスクロールさせる

表の幅を、固定の 640px から 100% に変えました。コマンドは、折り返すと読みにくくなるので、<pre> に overflow-x: auto を付け、箱の中だけを横にスクロールさせます。

css
.log { width: 100%; border-collapse: collapse; }
.code { background: #eefafd; padding: 8px; overflow-x: auto; }

直した後は content-width が合格になり、Playwright のスクリプトでも、はみ出した要素は見つかりませんでした。

text
[content-width] weight=1 group=pwa-optimized score=1 (binary)  title: Content is sized correctly for the viewport
text
== fixed.html  innerWidth=412 outerWidth=412 clientWidth=412 scrollWidth=412
  (no element wider than clientWidth)

同じ条件(幅 412px)で撮ったページは、次のようになりました。直す前は表の右の「メモ」の列が画面の外にあり、直した後は表が画面の幅に収まっています。

猫のごはんの記録のページを、スマホの幅で直す前と直した後に分けて縦に並べた画面。直す前は表の右端とコマンドの後ろが画面の外に切れていて、直した後は表の4つの列がすべて画面に収まっている
直す前(上)と直した後(下)。スマホの幅で表示した同じページ

表やコマンドのほかに、画像も、画面より広く表示すると同じようにはみ出すことがあります。web.dev の「レスポンシブ ウェブデザインの基本」には、画面より広く表示した画像が横スクロールの原因になること、画像にはすべて max-width: 100% を付けるのがおすすめであることが書かれています。

はみ出しが1か所でも残ると不合格のまま

表だけを直し、<pre> の overflow-x: auto を付けないページでも試しました。結果は不合格で、説明の最初の数字は、コマンドの文字の右端と同じ 570px でした。

text
[content-width] weight=1 group=pwa-optimized score=0 (binary)  title: Content is not sized correctly for the viewport  explanation: The viewport size of 570px does not match the window size of 412px.

直す前のページでは、表の 656px に隠れて、コマンドのはみ出しは説明の数字に出ていませんでした。1か所を直しても不合格のままで、数字だけが変わったときは、別の要素がまだはみ出しています。

body に overflow-x: hidden を付けても消えない

はみ出した分を隠すつもりで、直す前のページの body に overflow-x: hidden を足しただけのページでも試しました。

css
body { overflow-x: hidden; }

結果は直す前と同じで、不合格のままでした。Playwright のスクリプトでも、innerWidth は 656 のままでした。

text
[content-width] weight=1 group=pwa-optimized score=0 (binary)  title: Content is not sized correctly for the viewport  explanation: The viewport size of 656px does not match the window size of 412px.
text
== hidden.html  innerWidth=656 outerWidth=412 clientWidth=412 scrollWidth=656
  table.log  box right=656
  pre.code  content right=570 (box right=396)

はみ出した要素そのものの幅を直さないと、この指摘は消えませんでした。

meta viewport がないページとデスクトップの設定

直した後のページから <meta name="viewport"> を消して実行すると、content-width と viewport の2つの監査が不合格になりました。はみ出した要素はないのに、ページの幅が 981px と出ました。

text
pwa score: 0  failed: installable-manifest, splash-screen, themed-omnibox, content-width, viewport, maskable-icon
[content-width] weight=1 group=pwa-optimized score=0 (binary)  title: Content is not sized correctly for the viewport  explanation: The viewport size of 981px does not match the window size of 412px.
[viewport] score=0  title: Does not have a `<meta name="viewport">` tag with `width` or `initial-scale`

Playwright のスクリプトで開くと、clientWidth が 980 で、ページそのものが画面より広い幅で描かれていました。この場合は CSS を直す前に、<head> の中に <meta name="viewport" content="width=device-width, initial-scale=1"> を入れます。

また、デスクトップ向けの設定(--preset=desktop)では、直す前のページでも content-width は「該当なし」(notApplicable)になりました。この指摘は、モバイル向けの条件で調べたときだけ出ます。

Lighthouse 12 以降のレポートには出ない

同じ直す前のページに、Lighthouse 12.8.2 と 13.6.0 を実行しました。どちらのレポートにも content-width の監査はありませんでした。

text
== bad12.report.json  (Lighthouse 12.8.2, HeadlessChrome/158.0.0.0, formFactor mobile)
content-width: (no such audit in this report)  audits containing "width": (none)
== bad13.report.json  (Lighthouse 13.6.0, HeadlessChrome/158.0.0.0, formFactor mobile)
content-width: (no such audit in this report)  audits containing "width": (none)

npm で取り出した各版の中を見ると、content-width の監査のファイルは 11.7.1 にはあり、12.0.0 にはありませんでした。Lighthouse の変更履歴の 12.0.0 の節には、PWA のカテゴリーを削除したことが書かれています。公式の解説にも、Lighthouse での PWA のテストは非推奨になった、とあります。

Lighthouse 12 以降で点数が良くても、ページが画面の幅に収まっているとは限りません。この検証の 640px の表のページも、12.8.2 と 13.6.0 では、ページの幅についての指摘は出ませんでした。確かめたいときは、上の Playwright のスクリプトのように innerWidth と outerWidth を比べるか、npm i -D lighthouse@11.7.1 で 11 を入れて実行します。

​Posted by 86