Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple !important class declarations and precedence

Tags:

css

Theoretically speaking, if you had this scenario:

<style type="text/css">     .class1 {         color:#F00 !important;     }     .class2 {         color:#00F !important;     } </style>  <p class="class2 class1">Test</p> 

Which color should take precedence? How do browsers determine precedence in this scenario?

like image 886
Ben Dyer Avatar asked Mar 29 '11 14:03

Ben Dyer


People also ask

What are the style precedence rules when using multiple approaches?

Inline CSS has a higher priority than embedded and external CSS. So final Order is: Value defined as Important > Inline >id nesting > id > class nesting > class > tag nesting > tag.

Which type of declaration will take precedence in CSS?

The more specific the CSS selector is, the higher is the precedence of the CSS property declarations inside the CSS rule owning the selector. In general terms, the more specifically (uniquely) a CSS selector targets an HTML element, the higher is its specificity.

Does class or ID take precedence?

The ID selector #first has priority over the class and tag selectors.

What is the important rule in CSS and why should you generally avoid it?

important rule is to include another ! important rule on a declaration with the same (or higher) specificity in the source code - and here the problem starts! This makes the CSS code confusing and the debugging will be hard, especially if you have a large style sheet!


1 Answers

According to this source: http://www.boogiejack.com/CSS_4.html

class2 should override the class1 styling.

Order of Specification: As a last resort, when all other conflict resolution specifications cannot determine which style should take precedence, the last style specified will be the style used.

like image 93
Jan_V Avatar answered Oct 26 '22 02:10

Jan_V