CSS Spelling and Grammar Styling
CSS Spelling and Grammar features enable sites to customize the appearance of the markers that browsers render when a spelling or grammar error is detected, and to control the display of spelling and grammar markers that the site itself defines.
- The text decoration
spelling-error
andgrammar-error
line types render native spelling or grammar markers controlled by CSS. The primary use case is for site-specific spell-check or grammar systems, allowing sites to render native looking error markers for the errors the site itself detects, rather than errors the browser detects. They are part of the CSS Text Decoration Module Level 4 Working Draft Specification. - The
::spelling-error
and::grammar-error
pseudo elements allow sites to apply custom styling to the browser rendered spelling and grammar error markers. The target use case enables sites to override the native markers should custom markers offer a better user experience on the site. They are part of the CSS Pseudo-Elements Module Level 4 Working Draft Specification.
These features are available for all users in Chrome 121 and upward, and corresponding Edge versions. You can also enable “Experimental Web Platform features” with chrome://flags in earlier versions. The features should be coming soon to Safari and Firefox.
CSS Text Decorations for Spelling and Grammar Errors #
Imagine a site that uses a specialized dictionary for detecting spelling errors in user content. Maybe users are composing highly domain specific content, like botany plant names, or working in a language not well represented by existing spelling checkers. To provide an experience familiar to users, the site might wish to use the native spelling and grammar error markers with errors detected by the site itself. Before the error line types for CSS Text Decoration were available, only the browser could decide what received native markers.
Here’s an example of how you might control the rendering of spelling markers.
<style>
.spelling-error {
text-decoration-line: spelling-error;
}
</style>
<div id="content">Some content that has a misspeled word in it</div>
<script>
const range = document.createRange();
range.setStart(content.firstChild, 24);
range.setEnd(content.firstChild, 33);
const newSpan = document.createElement("span");
newSpan.classList.add("spelling-error");
range.surroundContents(newSpan);
</script>
How it works:
- Create a style for the spelling errors. In this case a spelling error will have a text decoration added with the line type for native spelling errors.
- As the user edits contents, JS can track the text, process it for spelling errors, and then trigger a method to mark the errors.
- The script above is one way to apply the style to the error, assuming you have
the start and end offset of the error within its
Text
node. A range is created with the given offset, and then the contents of the range are pulled out of theText
and put within a span, that is then re-inserted where the range once was. The span gets the spelling-error style.
The result will vary across browser and platform, depending on the native conventions for marking errors.
Using other text-decoration properties or, rather, not #
All other text decoration properties are ignored when text-decoration-line: spelling-error
or grammar-error
are used. The native markers rendered for these decorations are not, in
general, styleable, so there would be no clear way to apply additional properties (such as
text-decoration-color or text-decoration-thickness).
Spelling and Grammar Highlight Pseudos #
The ::spelling-error
and ::grammar-error
CSS pseudo classes allow sites to customize the
markers rendered by the browser for browser-detected errors. The native markers are
not rendered, being replaced by whatever styling is defined by the pseudo class applied
to the text content for the error.
For the first example, let’s define a red background tint to replace the native spelling markers:
<style>
::spelling-error {
background-color: rgba(255,0,0,0.5);
text-decoration-line: none;
}
</style>
<textarea id="content">Some content that has a misspeled word in it</textarea>
<script>
content.focus();
</script>
Two things to note:
- The example defines the root spelling error style, applying to all elements. In general, sites should use a single style for spelling errors to avoid user confusion.
- The
text-decoration-line
property is set to none to suppress the native spelling error marker. Without this, the style would inherit thetext-decoration-line
property from the user agent, which definestext-decoration-line: spelling-error
.
A limited range of properties may be used in ::spelling-error
and ::grammar-error
; see
the spec for details. The limits address two potential concerns:
- The spelling markers must not affect layout of text in any way, otherwise the page contents would shift when a spelling error was detected.
- The spelling markers must not be too expensive to render; see Privacy Concerns below.
The supported properties include text decorations. Here’s another example:
<style>
::spelling-error {
text-decoration: 2px wavy blue underline;
}
textarea {
width: 200px;
height: 200px;
}
</style>
<textarea id="content">Some content that has a misspeled word in it</textarea>
<script>
content.focus();
</script>
One consequence of user agent styling is that you must provide a line type for any custom text decoration. Without one, the highlight will inherit the native spelling error line type. As discussed above, all other properties are ignored with that line type. For example, this renders the native spelling error, not the defined one:
<style>
::spelling-error {
text-decoration-style: solid;
text-decoration-color: green;
}
</style>
<textarea id="content">Some content that has a misspeled word in it</textarea>
<script>
content.focus();
</script>
Accessibility Concerns #
Modifying the appearance of user agent features, such as spelling and grammar errors, has significant potential to hurt users through poor contrast, small details, or other accessibility problems. Always maintain good contrast with all the backgrounds present on the page. Ensure that error markers are unambiguous. Include spelling and grammar errors in your accessibility testing.
CSS Spelling and Grammar styling is your friend when the native error markers pose accessibility problems, such as poor contrast. It’s arguably the strongest motivation for supplying the features in the first place.
Privacy Concerns #
User dictionaries contain names and other personal information, so steps have been taken to ensure that sites cannot observe the presence or absence of spelling or grammar errors. Computed style queries in Javascript will report the presence of custom spelling and grammar styles regardless of whether or not they are currently rendered for an element.
There is some potential for timing attacks that render potential spelling errors and determine paint timing differences. This is mitigated in two ways: browsers do not report accurate timing data, and the time to render error markers is negligible in the overal workload of rendering the page. The set of allowed properties for spelling and grammar pseudos is also limited to avoid the possibility of very expensive rendering operations.
Thanks #
The implementation of CSS Spelling and Grammar Features was undertaken by Igalia S.L. funded by Bloomberg L.P.