HTML Guides for lexical error
Learn how to identify and fix common HTML validation errors flagged by the W3C Validator — so your pages are standards-compliant and render correctly across every browser. Also check our Accessibility Guides.
A lexical error is a low-level parsing failure. Unlike a syntax error where the structure is wrong but the characters are individually valid, a lexical error means the parser cannot even form valid tokens from the input. The CSS specification defines a precise set of characters and sequences that are meaningful — property names, values, punctuation like ;, :, {, }, and so on. When the parser encounters something outside these expectations, such as @ where a ; should be, a curly (“smart”) quote instead of a straight quote, or a stray Unicode character, it raises a lexical error.
This matters for several reasons. First, browsers handle invalid CSS unpredictably — some may skip the entire rule block, others may ignore only the broken declaration, and the behavior can vary across browser versions. This leads to inconsistent rendering for your users. Second, a single lexical error can cascade, causing the parser to misinterpret subsequent valid CSS as well, potentially breaking styles well beyond the offending line. Third, clean, valid CSS is easier to maintain, debug, and collaborate on.
Common causes of this error include:
- Invalid characters used in place of punctuation — e.g., @, !, or # where a semicolon or colon should be.
- Smart (curly) quotes — pasting CSS from word processors or CMS editors that convert "straight quotes" to "curly quotes" or 'curly apostrophes'.
- Missing semicolons — while not always a lexical error, a missing ; can cause the next line’s property name to be read as part of the previous value, producing unexpected character sequences.
- Non-ASCII invisible characters — byte order marks (BOM), zero-width spaces, or non-breaking spaces that are invisible in most editors but invalid in CSS tokens.
- Copy-paste artifacts — copying code from PDFs, websites, or chat applications that insert hidden formatting characters.
To fix the issue, go to the exact line and column the error references. Look at the character it reports as “Encountered” and determine what the correct character should be. If you can’t see anything wrong, try deleting the characters around the reported position and retyping them manually — this eliminates invisible character problems.
Examples
Invalid character instead of semicolon
The @ symbol after blue is not valid CSS punctuation in this context:
<style>
h1 {
color: blue@
font-size: 24px;
}
</style>
Replace @ with a proper semicolon:
<style>
h1 {
color: blue;
font-size: 24px;
}
</style>
Smart quotes in font-family
Curly quotes copied from a word processor cause a lexical error:
<style>
body {
font-family: \u201CHelvetica Neue\u201D, sans-serif;
}
</style>
Use straight double quotes instead:
<style>
body {
font-family: "Helvetica Neue", sans-serif;
}
</style>
Stray character in an inline style
An accidental backtick in a style attribute triggers the error:
<p style="color: red`; margin: 0;">Hello</p>
Remove the invalid character:
<p style="color: red; margin: 0;">Hello</p>
Invisible non-breaking space
Sometimes the error points to what looks like empty space. A non-breaking space (\u00A0) pasted from another source can hide between tokens:
<style>
.box {
display:\u00A0flex;
}
</style>
Delete the space and retype it as a normal ASCII space:
<style>
.box {
display: flex;
}
</style>
If you suspect invisible characters, use your text editor’s “show whitespace” or “show invisible characters” feature, or paste the CSS into a hex editor to inspect the raw bytes. Configuring your editor to save files in UTF-8 without BOM also helps prevent encoding-related lexical errors.
Ready to validate your sites?
Start your free trial today.