In CSS, when should identifiers be used and when should classes be used?

Possible duplicate:
CSS best practices regarding identifier and class?

It seems to me that both achieve the same. So, are there any rules or standards regarding when identifiers are more suitable for classes and vice versa.

I know that this issue can be regarded as subjective, I have no preferences over this or that, and I'm not trying to trade anything here. I am sincerely curious as I am new to web interface design and would like to know what to use and when.

+4
source share
3 answers

If you need to give a few things to an attribute / style, use a class, if it is singular, use an identifier. If this is what I want to readily identify, for example LoginBtn , then this is an identifier, if it is a style, for example, an anchor that is blueLink , this is a class.

Another consideration for many developers is javascript. for example, the jQuery $('#id') selector is much faster than $('.class') , so if you are dealing with only one element, this is also an advantage.

+1
source

This is not subjective. Classes can be used to give two separate objects in html (e.g. two divs) the same style. The identifier is unique and therefore can only stylize one element at a time.

edit: clarification

0
source

Source: https://habr.com/ru/post/1302556/


All Articles