Cascading style sheets use “id” or “class”

后端 未结 15 2418
夕颜
夕颜 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:30

    As far a HTML is concerned, an id identifies a single element in the DOM and each element can only have one unique id, whereas a class classifies one or many elements in the DOM and each element can have multiple classes.

    ...

    Also from a CSS perspective, remember that the order of importance goes from least specific to most specific. So, #id properties override .class properties, which in turn override tag properties. Therefore, #id styles trump .class styles unless certain class properties are flagged !important.

    
    
    

    This text is red.

    This text is blue.

提交回复
热议问题