1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

!important does not override?

Discussion in 'CSS' started by Macavi, Jan 13, 2014.

  1. #1
    Here's an explanation of override rule: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

    Here's how I understand this rule: this rule is made to take all priority from other specific rules and make current rule of higher priority.

    I am probably wrong because all this rule does, is cancels CSS for specific element. I thought classes have higher priority over selectors, while IDs have higher priority over classes and come first in priority tree. !important seemed to me exception rule that can take priority from IDs further but that seems not be so:

    CSS:

    [​IMG]

    HTML:

    [​IMG]

    Output:

    [​IMG]

    Line 2 (Hello red) without !important rule in "p:last-child" is supposed to be green and not red because selector "p:last-child" has pleudo-class making it higher priority than selector with class "p.awesome"

    In conclusion, !important is just commenting out/stripping CSS lines and not for specifying highest priority?
     
    Macavi, Jan 13, 2014 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    !important doesn't go on selectors! That's your 'problem'. It has NO business where you put it.

    p:last-child {
      color:green !important;
    }
    Code (markup):
    Is what you should be trying to do. It is applied to attribute/value pairs, NOT selectors.
     
    deathshadow, Jan 13, 2014 IP
  3. Macavi

    Macavi Active Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    65
    #3
    Thanks for clearing it up. It seems it does 2 functions: when placed beside selectors, it comments out CSS line and when placed inside attribute/value pairs, it assigns higher priority to CSS line.
     
    Macavi, Jan 14, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #4
    It's not commenting it out -- it's an invalid construct so that's an error behavior, not a planned one. Invalid CSS selectors are ignored. What you are seeing is not intentional or how it's supposed to work, what you are seeing is what happens when there's an error.
     
    deathshadow, Jan 15, 2014 IP
  5. Macavi

    Macavi Active Member

    Messages:
    97
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    65
    #5
    In this case, using invalid construct only makes it harder to interpret CSS for websdesigner and debug if there's a need?
     
    Macavi, Jan 15, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    It can also be inconsistent across browsers. ALLEGEDLY HTML 5 compliant browsers are supposed to behave the same, but that's short-sighted nonsense designed to let people sleaze out code any old way.

    Some might be ignoring it because it's invalid, others might be ignoring it because they are treating !important as a tag selector... and since there's no such tag as !important... some browsers might even obey it by ignoring the invalid value. I've seen that on Blazer and iCab a couple times where an invalid selector results in the invalid part of the selector being ignored as if it's not even typed in, instead of ignoring the ENTIRE selector.
     
    deathshadow, Jan 15, 2014 IP