English

Lighthouseで名前のないボタンを指摘されたときの直し方

​​Lighthouse​HTML,アクセシビリティ,トラブルシュート

Lighthouse のアクセシビリティの監査で「Buttons do not have an accessible name」(日本語のレポートでは「ボタンにユーザー補助機能名が指定されていません」)と出たときは、ページの中に、読み上げ用の名前が付いていないボタンがあります。ボタンに aria-label を付けるか、中の img に alt を付けるか、画面には出さない文字をボタンの中に入れると、指摘は消えます。名前のないボタンになりやすいのは、ハンバーガーメニューや検索、閉じるの「×」のように、中に文字がなくアイコンだけが入ったボタンです。

私は検証環境で、アイコンだけのボタンが3つあるページに Lighthouse を実行し、指摘が出ることと、直すと消えることを確かめました。

補足

検証環境は Ubuntu 24.04、Node.js 22.23.4、Lighthouse 13.6.0(npm で入れた CLI。中で使われている axe-core は 4.14.0)、ブラウザは Google Chrome for Testing 153 のヘッドレスです。アクセシビリティの項目だけ(--only-categories=accessibility)を、初期設定のモバイル向けの条件で実行しました。ページは手元で作り、127.0.0.1 から配信しました。Chrome の DevTools の Lighthouse パネルや PageSpeed Insights、実際のスクリーンリーダーでの読み上げは試していません。

指摘が出るページ

検証に使ったのは、アイコンだけのボタンを3つ並べたページです。アイコンの入れ方は、SVG、img、CSS で描いたものの3通りにしました。

html
<!-- 1. SVG のアイコンだけのボタン -->
<button type="button" class="menu">
  <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="M3 6h18M3 12h18M3 18h18" stroke="#1f2a30" stroke-width="2"/></svg>
</button>
<!-- 2. alt のない img だけのボタン -->
<button type="button" class="search"><img src="icon.svg" width="24" height="24"></button>
<!-- 3. CSS で描いたアイコン(中身が空)のボタン -->
<button type="button" class="close"><span class="icon-close"></span></button>

このページを python3 -m http.server で配信し、次のように Lighthouse を実行しました。

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

HTML のレポートを開くと、3つのボタンがすべて「Failing Elements」に並びました。

Lighthouse のレポートで「Buttons do not have an accessible name」を開いた画面。button.menu、button.search、button.close の3つが Failing Elements に並び、それぞれのボタンの位置が枠で示されている
直す前のページのレポート。名前のない3つのボタンが並ぶ

JSON のレポートから、アクセシビリティの点数と、この監査の結果を取り出すと次のとおりです(3つのボタンの理由は同じなので、1つ目だけ載せています。Lighthouse のバージョンや点数の内訳などの行も省いています)。

text
accessibility score: 77
button-name title: Buttons do not have an accessible name
button-name score: 0
- body > main > button.menu | <button type="button" class="menu">
   Fix any of the following:
     Element does not have inner text that is visible to screen readers
     aria-label attribute does not exist or is empty
     aria-labelledby attribute does not exist, references elements that do not exist or references elements that are empty
     Element has no title attribute
     Element does not have an implicit (wrapped) <label>
     Element does not have an explicit <label>
     Element's default semantics were not overridden with role="none" or role="presentation"
failed accessibility audits: button-name, image-alt

「Fix any of the following」の下に並んでいるのが、ボタンに名前を付ける方法の一覧です。どれか1つを満たせば、そのボタンは合格になります。この判定は Lighthouse が中で使っている axe-core の button-name のルールで、ボタンの中の文字、aria-label、aria-labelledby、title 属性、label 要素との関連付けなどのうち、どれか(any)があればよいと決められています。

2つ目のボタンは、中の img(button.search > img)に alt がないので、「Image elements do not have [alt] attributes」(image-alt)でも同時に指摘されました。

3つの直し方

次のように直すと、指摘は消えました。ボタンの見た目は変わりません。

html
<!-- 1. aria-label で名前を付ける -->
<button type="button" class="menu" aria-label="メニューを開く">
  <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="M3 6h18M3 12h18M3 18h18" stroke="#1f2a30" stroke-width="2"/></svg>
</button>
<!-- 2. img に alt を付ける -->
<button type="button" class="search"><img src="icon.svg" width="24" height="24" alt="検索"></button>
<!-- 3. 画面には出さないテキストを入れる -->
<button type="button" class="close"><span class="icon-close"></span><span class="visually-hidden">閉じる</span></button>

3つ目の visually-hidden は、文字を1ピクセルの枠に押し込めて、画面では見えなくする CSS です。

css
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0;
}

どの直し方が合うかは、ボタンの作りで決まります。中身が SVG だけなら aria-label が手軽です。アイコンが img なら、alt にボタンの働きを書けば、ボタンの名前にもなります。CSS でアイコンを描いていて中身が空のボタンなら、画面に出さない文字を入れても名前を付けられます。

同じ条件で Lighthouse を実行し直すと、指摘は消え、点数は100になりました(点数の内訳などの行は省いています)。

text
accessibility score: 100
button-name title: Buttons have an accessible name
button-name score: 1
failed accessibility audits: (none)

Playwright の ariaSnapshot() でも、ボタンの名前を確かめました。ページの構造を書き出すと、直す前のボタンは名前のない button で、直した後は aria-label、alt、隠した文字の内容がそれぞれ名前になっていました。

text
--- bad.html
- main:
  - heading "メモの一覧" [level=1]
  - button
  - button:
    - img
  - button
--- good.html
- main:
  - heading "メモの一覧" [level=1]
  - button "メニューを開く"
  - button "検索":
    - img "検索"
  - button "閉じる"

直したつもりで消えない書き方

次の3つの書き方も試しました。Failing Elements から外れたのは、title 属性を付けたボタンだけでした。

html
<!-- 1. display: none の文字を入れる -->
<button type="button" class="menu"><span style="display: none">メニューを開く</span>
  <svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true"><path d="M3 6h18M3 12h18M3 18h18" stroke="#1f2a30" stroke-width="2"/></svg>
</button>
<!-- 2. img に空の alt を付ける -->
<button type="button" class="search"><img src="icon.svg" width="24" height="24" alt=""></button>
<!-- 3. title 属性だけを付ける -->
<button type="button" class="close" title="閉じる"><span class="icon-close"></span></button>

Lighthouse の結果は次のとおりです(それぞれのボタンの下に出る「Fix any of the following」と理由の行は、直す前と同じなので省いています。点数の内訳などの行も省いています)。

text
accessibility score: 89
button-name title: Buttons do not have an accessible name
button-name score: 0
- body > main > button.menu | <button type="button" class="menu">
- body > main > button.search | <button type="button" class="search">
failed accessibility audits: button-name

ariaSnapshot() で書き出した名前は、次のとおりです。

text
--- partial.html
- main:
  - heading "メモの一覧" [level=1]
  - button
  - button
  - button "閉じる"

display: none で隠した文字は、ボタンの名前になりませんでした。ariaSnapshot() でも、1つ目のボタンは名前のない button のままでした。見た目から文字を消したいときは、display: none ではなく、上の visually-hidden のような CSS を使います。

alt="" は、image-alt の指摘は消しますが、ボタンの名前にはなりません。レポートの image-alt の説明にも、飾りの画像は alt を空にすれば読み上げの対象から外せる、と書かれています。ボタンの中の画像がボタンの働きそのものを表しているなら、alt に言葉を入れます。

title="閉じる" だけのボタンは合格になり、ariaSnapshot() でも名前は「閉じる」になりました。axe-core のルールが title 属性を名前の付け方の1つとして認めているためです。

名前のないボタンが1つでも残ると点数は満点にならない

Lighthouse のアクセシビリティの点数は、監査ごとの重みを使った加重平均です。Lighthouse 13.6.0 の設定では、button-name の重みは10で、image-alt と同じく重みがいちばん大きい監査の1つです。

それぞれの監査は、axe-core で1つでも違反が見つかれば0点、なければ1点です。検証でも、1つだけ直して2つが残ったページでは、button-name は0点のままでした。検証で出た点数を、JSON のレポートの重みから計算し直すと次のようになりました。

ページ 落ちた監査 計算 点数
直す前 button-name、image-alt 68 / 88 77
消えない書き方を混ぜたページ button-name 79 / 89 89
直した後 なし 132 / 132 100

分母がページごとに違うのは、ページに当てはまらない監査(aria-label を使っていないページでの ARIA 属性の監査など)が計算から外れるためです。消えない書き方を混ぜたページで分母が89になったのは、alt="" の画像があるため presentation-role-conflict(重み1)が計算に加わったからです。直した後のページは aria-label を使ったので、ARIA 属性の監査が計算に加わり、分母が132になりました。

​Posted by 86