14

Given the following mark-up...

<div id="Header">
     <a href="#" class="Highlight">foo</a>
</div>

And the following stylesheet...

/******************Exceptions******************/
#Footer, #Header,
#Footer a, #Header a { color: #f8f8f8; }


/******************Classes******************/
.Highlight, a.Highlight { color: #B1D355; }
.Notification, a.Notification { color: Red; }

Why is my link still off-white (F8F8F8) rather than green (B1D355)?

Shouldn't using the class Highlight override the color settings for Header and Footer since it comes after their declarations?

Adam Ritenauer
  • 3,151
  • 4
  • 33
  • 40

4 Answers4

13

It's all about weight. A class selector gets trumped by an ID selector.

#Footer a

will always get precedence over

.Highlight or .Highlight a

Make your selector

#Footer .highlight a

and you should be fine.

Robusto
  • 31,447
  • 8
  • 56
  • 77
8

CSS priority

  1. ID selector > class selector > attribute selector

  2. For the same priority, the later has the higher priority.

    .class1 { color: black; }

    .class2 { color: red; }

    It will be red.

  3. To have more priority, use !important


For your problem, #Footer is an ID selector has higher priority than .Highlight, a class selector.

SweenEy
  • 81
  • 1
  • 2
  • The [use of `!important` should be considered very carefully](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity). – surfmuggle Oct 13 '17 at 11:24
2

ID has a higher priority than class in CSS:

Use #Header a.Highlight { color: #B1D355; }

Amy B
  • 17,874
  • 12
  • 64
  • 83
1

CSS rules are not just applied based on the "last parsed, last applied". It also depends on how specific and unique the rule is to that element. Since you're only specifying a class selector, the path that included the id is getting a higher priority.

Agent_9191
  • 7,216
  • 5
  • 33
  • 57