スペルミス・文法エラーに対してスタイルを設定する::spelling-errorと::grammar-error
はじめに
Firefox 140で::spelling-error
と::grammar-error
のCSS擬似要素がサポートされました。これにより、全てのコアブラウザでこれらの擬似要素が利用可能になりました。
これらの擬似要素は、スペルミスや文法エラーとしてフラグ付けされたテキストに対するスタイルを定義するために使用されます。
::spelling-error
スペルミスとしてフラグ付けされたテキストに対するスタイルを定義します。
::spelling-error {
background-color: #ffcccc;
color: #d8000c;
text-decoration-line: underline;
text-decoration-style: wavy;
text-decoration-color: red;
}
::spelling-error
の中では、color
とbackground-color
、cursor
、caret-color
、outline
、outlint-*
、text-decoration
、text-decoration-*
、text-emphasis-color
、text-shadow
のみが使用可能です。
::grammar-error
文法エラーとしてフラグ付けされたテキストに対するスタイルを定義します。
::grammar-error {
color: #d8000c;
text-decoration-line: underline;
text-decoration-style: dotted;
text-decoration-color: red;
}
::grammar-error
も::spelling-error
と同様のプロパティしか定義できません。
例
contenteditable
属性で編集可能なテキストエリアを準備しました。spellcheck
属性がtrue
に設定されているため、スペルミスや文法エラーが自動的に検出されます。
contenteditable
属性の値はplaintext-only
です。これもまた、Baseline 2025の機能ですね(plaintext-only
についても記事を書いているので良ければ読んでください)。
ここで検出されたエラーが::spelling-error
と::grammar-error
の擬似要素でスタイリングされます。
スペルミス・文法エラーのスタイル設定例
Check a speling error and an grammar error.
そのままではspellcheck
属性の確認が行われず、見た目の変化がないことがあります。その場合は、テキストの一部を編集して再確認させることで、エラーが表示されるようになると思います。
また、ブラウザごとにspellcheck
の挙動が異なるため、デフォルトの文章で両方のエラーを確認できない恐れがあります(Safariで確認しました)。期待としては、speling
がスペルミス、an grammar
のan
が文法エラーとして検出されることです。
まとめ
Baseline 2025入りした::spelling-error
と::grammar-error
を紹介しました。
spellcheck
自体が日本語対応していないので、日本語ユーザーにとってはあまり恩恵がないかもしれませんが、英語のコンテンツを扱うときのために覚えておきたいです。