Difference between Class & ID in CSS?

What's the difference between class and id in CSS , I can only find out that class is for particular group of elements and id is for unique element, but please explain me with example and how can't we use one element over other?

Replies

  • Anoop Kumar
    Anoop Kumar
    Id is used to select a single element and apply properties.
    #divId
    {
    text-align:center;
    }
    
    Element
    will get above property.

    while class used to apply on group of element wherever that property found.
    .center {text-align:center; color:red} 
    For any element you add class="center" will get above property.

    #-Link-Snipped-#
  • Whats In Name
    Whats In Name
    ianoop
    Id is used to select a single element and apply properties.
    #divId
    {
    text-align:center;
    }
    
    Element
    will get above property.
    Does this mean that now,we cannot use id="divid" for any other element?
  • Anoop Kumar
    Anoop Kumar
    you can give same ID to different elements but while identifying by ID , 1st occurrence will considered and others will be discarded, since HTML parsed as DOM tree model.
    Best Html practice is to always give ID property and identify by ID rather than name.

You are reading an archived discussion.

Related Posts

Hello everyone, I am pursuing B.E in Mechanical and planning and planning for doing MS in USA. Here, I am facing a difficulty in selecting my major. I have shortlisted...
Who else but the one and only...? (check out the tricknometry while you are at it)
Beautiful, evocative gallery of winners of NatGeo's Photo Contest 2012. https://ngm.nationalgeographic.com/n...t=multi_20130121&utm_campaign=MultiProduct#/0
SSDs are super fast in transferring data. This fascinating video shows how SSDs are manufactured and tested. https://news.cnet.com/8301-17938_105...ium=feed&utm_campaign=Feed:+cnet/pRza+(Crave)
I Have a serious doubt 😀 VECTOR QUANTITIES | SCALAR QUANTITIES Velocity=Displacement/Time | Speed=Distance/Time Acceleration=Velocity/Time | ??????=Speed/Time I Want to know what is speed/time in term of a scalar quantity..