Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Rule Priorities

Tags:

css

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?

like image 790
Adam Ritenauer Avatar asked May 20 '10 18:05

Adam Ritenauer


People also ask

What is the order of priority of CSS?

The location order of precedence is: browser default rules, external style sheet rules, embedded styles, and inline style rules.

Which rule has highest priority in CSS?

CSS specificity rule 1) Inline style: Inline style has highest priority among all. 2) Id Selector: It has second highest priority. 3) Classes, pseudo-classes and attributes: These selectors has lowest priority.

What are the 3 main components of a CSS rule?

The components of css style are: 1)Selecter:HTML element name, id name, class name. 2)Property:It's like an attribute such as background color,font-size,position,text-align,color,border etc. 3)Values:which defines property or values allocate for properties.


2 Answers

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.

like image 79
Robusto Avatar answered Nov 05 '22 06:11

Robusto


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.

like image 34
SweenEy Avatar answered Nov 05 '22 05:11

SweenEy