Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    CSS: Specificity

    Can someone please explain the concept of specificity to me, or point me to an article that explains it thoroughly. I understand the basics of specificity:

    A class beats an element selector.
    An ID selector beats a class and element selector.
    The style attribute beats the ID, class and element selectors.

    I also know that a Universal Selector has a specificity of 0, 0, 0, 0, which I've read is not the same as having no specificity.

    I've been using this website as reference to try and learn more about the specificity concepts of CSS:
    http://css-tricks.com/specifics-on-css-specificity/

    I'm particularly referring to the following example:
    Code:
    #footer *:not(nav) li
    There are 0 style selectors, one ID selector, a universal selector, a psuedo-class selector, and two element selectors, however the outcome is:
    0, 1, 0, 2

    Is this because the universal selector has a specificity of 0, 0, 0, 0, and therefore, this is applied to whatever it is attached to?

    So basically when people use universal selectors as a reset (bad, but using it as an example), it would be: 0, 0, 0, 0

    If someone is using a universal selector on an element, (*ul li {font-style: italic;}) it would be: 0, 0, 0, 0

    If someone is using a universal selector on a class, (*.error p {color: red;}) it would be: 0, 0, 0, 1

    And so on an so forth, correct?

    Thank you for any information that would help me further understand how this works!

  • #2
    Registered User
    Join Date
    Jun 2011
    Posts
    1,063
    Thanks
    12
    Thanked 241 Times in 240 Posts
    These numbers aren't helping. I recommend not using them lol...

    Here's how the lineup would look like from weak to strong.
    Code:
    #footer *:not(nav) li
    #footer div:not(nav) li
    #footer .class:not(nav) li
    #footer #id:not(nav) li
    Quote Originally Posted by LRRoberts0122 View Post
    So basically when people use universal selectors as a reset (bad, but using it as an example), it would be: 0, 0, 0, 0
    If someone is using a universal selector on an element, (*ul li {font-style: italic;}) it would be: 0, 0, 0, 0
    If someone is using a universal selector on a class, (*.error p {color: red;}) it would be: 0, 0, 0, 1
    The class is stronger... so *.error p.

    Code:
    * {}
    *div { }
    *.class { }
    *#id {}
    To be honest, none of this matters, you will probably never and shouldn't ever need to know which is stronger for these odd selectors.
    Last edited by Sammy12; 10-01-2012 at 03:05 AM.

  • #3
    Regular Coder
    Join Date
    May 2009
    Posts
    813
    Thanks
    123
    Thanked 24 Times in 24 Posts
    I thought the id is stronger always! What do I know?

  • #4
    New Coder
    Join Date
    Apr 2012
    Posts
    44
    Thanks
    0
    Thanked 14 Times in 14 Posts
    In-line css overwrite id's. Unless !important is used.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •