What's the difference between an id and a class?

前端 未结 17 2302
长情又很酷
长情又很酷 2020-11-22 01:55

What\'s the difference between

and
when it comes to CSS? Is it alright to use
<
17条回答
  •  名媛妹妹
    2020-11-22 02:22

    Class is used for multiple elements which have common attributes.Example if you want same color and font for both p and body tag use class attribute or in a division itself.

    Id on the other hand is used for highlighting a single element attributes and used exclusively for a particular element only.For example we have an h1 tag with some attributes we would not want them to repeat in any other elements throughout the page.

    It should be noted that if we use class and id both in an element,*id ovverides the class attributes.*Simply because id is exclusively for a single element

    Refer the below example

    
    
    
    
       
        
    

    Lorem ipsum dolor sit amet consectetur adipisicing elit. Perspiciatis a dicta qui unde veritatis cupiditate ullam quibusdam! Mollitia enim, nulla totam deserunt ex nihil quod, eaque, sed facilis quos iste.

    We generate the output

    Output

提交回复
热议问题