Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other CSS help on ID and Class

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #186259
    sakii
    Participant

    Hi,

    i would like to ask why i do see some css is coded in this way :

    `
    #id .myclass .myclass2 {

    my property
    }
    `

    is it referring to setting .myclass2 property if so why can’t we just use .myclass2{ } instead and have to put #id .myclass .myclass2 ?

    #186261
    Kingslayer
    Participant

    Specificity is the word you are looking for ;).

    https://css-tricks.com/multiple-class-id-selectors/

    Here Chris explains it in an easy way.

    Here is some more to follow up =).

    http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

    #186262
    sakii
    Participant

    ahhh learned something new today :)

    THANKS !

    #186263
    sakii
    Participant

    hmm so does it means that it will select all the element within .myclass2 ?

    If i use #id .myclass .myclass2 { } as what was stated in the first URL u mention.

    #186268
    Paulie_D
    Member
    
    #id .myclass .myclass2 {
    my property
    }
    

    This selector will select an element has a class of myclass2 that is within an element with a class of myclass that is within an element with an ID of id.

    
    <div id="id">
         <div class="myclass">
               <div class="myclass2"></div> /* this one */
         </div>
    </div>
    

    It won’t select this

    
    <div id="id2">
         <div class="myclass3">
               <div class="myclass2"></div> /* not this one */
         </div>
    </div>
    
    #186272
    sakii
    Participant

    Hmm how about if the person do in this way:

    #myid .myclass2 {
    };

    it is a short cut to myclass2 skipping myclass ?

    #186274
    Paulie_D
    Member

    it is a short cut to myclass2 skipping myclass ?

    Yes…it does but that’s not always what you want. It entirely depends on your requirements.

    That’s why specificity and class nesting is so important.

    #186306
    __
    Participant

    When you see this kind of selector, keep in mind that while it might have been done on purpose for a specific need, it also may have been done out of ignorance and/or laziness.

    Like @Paulie_D says, “[i]t entirely depends on your requirements.”

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘Other’ is closed to new topics and replies.