Skip to content

New Rule: no-id-selector #19

Open
Open
@aryan02420

Description

Rule details

Flag any CSS selector that uses the ID selector

What type of rule is this?

Warns about a potential problem

Example code

#one { /* lint error */
  background-color: yellow;
}

h1#heading { /* lint error */
  color: rebeccapurple;
}

The #id selector has very high specificity and doesn't always play well with other rules. An alternative is to use a .class selector instead.

When using a component-based UI framework, id selectors are often not very useful. You can hardcode an id into a component, but this makes the component non-reusable. The framework-generated unique ids cannot be referenced in CSS. Therefore, using id selectors in these cases is likely a mistake.

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

From MDN:

The ID selector has high specificity. This means styles applied based on matching an ID selector will overrule styles applied based on other selector, including class and type selectors. Because an ID can only occur once on a page and because of the high specificity of ID selectors, it is preferable to add a class to an element instead of an ID. If using the ID is the only way to target the element — perhaps because you do not have access to the markup and cannot edit it — consider using the ID within an attribute selector, such as p[id="header"].

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    • Status

      Evaluating

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions