Arad
Arad

Reputation: 12823

"Don't use IDs in selectors (CSS)" then, what to use instead of IDs?

One of CSS lint rules is: "It's better to not use IDs in selectors". So, what should we use instead of IDs to point to a unique element? For example, say I have dozens of elements that have a class named my-class, and I want only one of them to have a special CSS property. What can I do?

Upvotes: 10

Views: 24229

Answers (3)

Plippie
Plippie

Reputation: 2886

CSS-lint should be 'fixed' or rather updated to modern standard because its based on more than 10 year old code base where support for IE6 and IE7 where still preferable.

Nowadays everyone should know ID's are the fastest css selectors followed by Classes, Dom tags, adjacent siblings etc. And that CSS reads from right to left. So the shortest selector is the fastest. Since #ID is the fastest selector and #ID (should be) unique its ridicule to not use the #id as selector in CSS.

Upvotes: 18

code-orange
code-orange

Reputation: 127

You could add an additional class to the element and specify it in your css instead of ids. ID selectors have a higher specificity than attribute selectors but using ids in css isn't recommended and can't be reused. CSS classes can do everything IDs can.

Upvotes: 0

Manuel Otto
Manuel Otto

Reputation: 6540

give them another class for example:

<div class="myClass special"></div>

.myClass.special{
  color: red;
}

Upvotes: 6

Related Questions