Why is this an issue?

CSS treats only spaces, tabs, and line breaks as whitespace. Other Unicode space characters, such as the no-break space (U+00A0), the zero-width space (U+200B), or the ideographic space (U+3000), look like an ordinary space or are not visible at all in most editors, but CSS does not treat them as whitespace. These characters usually get into a stylesheet by accident, when code is copied from a web page, a word processor, or a chat message, or typed with a keyboard shortcut that inserts a no-break space.

Depending on the character, the browser reads it as part of an adjacent name or as an unexpected character. Either way, the code does not mean what it looks like:

The stylesheet looks correct when you read it, so this kind of bug is hard to find.

This rule raises an issue on each irregular whitespace character in CSS code. It also checks the strings of grid-template-areas, grid-template, and grid, where whitespace separates the cells of the grid.

Replace the character with an ordinary space, or remove it if the code needs no whitespace at that position. Many editors have an option to show invisible characters, which helps you find them.

Code examples

In the following examples, [NBSP] stands for a no-break space (U+00A0), which would otherwise look like an ordinary space.

Noncompliant code example

.menu[NBSP].item { /* Noncompliant: no-break space before ".item" */
  padding: 8px[NBSP]16px; /* Noncompliant: no-break space before "16px" */
}

Compliant solution

.menu .item {
  padding: 8px 16px;
}

Exceptions

This rule does not raise an issue for irregular whitespace characters in:

In the following example, the no-break space in the content string keeps the number and the unit on the same line. It does not raise an issue:

.distance::after {
  content: "10[NBSP]km"; /* Compliant: text inside a string */
}

.price::before {
  content: "\a0"; /* Compliant: escape sequence */
}

Resources

Documentation

Related rules