Skip to main content

HTML Guide

CSS: Lexical error at line X, column Y. Encountered: Z

A lexical error in a CSS block often indicates a typo or invalid character in your inline CSS or a <style> element.

CSS must adhere to standard syntax, using correct property names, values, and punctuation. Common causes include missing semicolons, invalid property names, or special characters that are not allowed in CSS. Only standard ASCII characters should be used, and comments must be properly formatted.

Example of incorrect CSS in HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bad CSS Example</title>
  <style>
    h1 {
      color: blue@
      font-size: 24px;
    }
  </style>
</head>
<body>
  <h1>Heading</h1>
</body>
</html>

In the example above, the @ character after blue is not valid in CSS and will cause a lexical error.

Corrected HTML example:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Good CSS Example</title>
  <style>
    h1 {
      color: blue;
      font-size: 24px;
    }
  </style>
</head>
<body>
  <h1>Heading</h1>
</body>
</html>

Always check for unexpected characters, properly close all CSS declarations with a semicolon, and ensure only valid CSS is included. For external CSS, make sure the file does not contain any invalid characters or typos.

Related W3C validator issues