Skip to main content

Code reviews rule: Use of spaces in attribute class selectors

Written by David Martin
Updated this week

Use of spaces in attribute class selectors

Why is this an issue?

The use of class selectors with spaces (e.g., .class1 .class2 when intending .class1.class2) is deprecated in Spring '25 and will no longer work. This was previously tolerated but is now strictly enforced.

Examples

Example of incorrect code:

// Spaces in class selector - deprecated
renderedCallback() {
const element = this.template.querySelector('[class="class1 class2"]');
// This may fail in Spring '25+
}

Example of correct code:

// Correct: descendant selector (space means "inside")
renderedCallback() {
const element = this.template.querySelector('.parent .child');
}

// Correct: multiple classes on same element (no space)
renderedCallback() {
const element = this.template.querySelector('.class1.class2');
}

// Correct: use data attributes for specificity
renderedCallback() {
const element = this.template.querySelector('[data-id="myElement"]');
}

How can I fix violations?

  1. Review selectors: Determine if spaces are intentional (descendant) or accidental.

  2. Remove extra whitespace: Clean up unintentional spaces in selectors.

  3. Use data attributes: For more reliable element selection.

Resources

Did this answer your question?