Cascading style sheets use “id” or “class”

后端 未结 15 2494
夕颜
夕颜 2020-12-05 16:11

when styling specific html elements, i tend to always use the class attribute. the css code looks cleaner imo.

why do both exist which one should you use and when ?<

15条回答
  •  清歌不尽
    2020-12-05 16:45

    You go with in this order of preference:

    1. ID (must be only one on the page);
    2. Element and class (eg div.myclass); then
    3. Class with no element qualifier.

    ID is by far the fastest lookup. Next fastest is lookup by tagname. Slowest is by class name.

    As for when to use classes or IDs, you use IDs when you can and classes when you can't.

提交回复
热议问题