Questions tagged [css-specificity]

In CSS, specificity is a measure that determines the strength of a selector. The properties within selectors with the highest specificity get applied, overriding the same properties of selectors with lower specificity, regardless of their position within a stylesheet.

In CSS, specificity is a measure applied to a style rule that provides a ranking, based on its selector, for resolving conflicts between its style declarations and the declarations of other style rules.

Calculating Specificity

As defined Section 6.4.3 of the CSS2.1 specification and improved upon in Selectors Level 3:

A selector's specificity is calculated as follows:

  • count the number of ID selectors in the selector (= a)
  • count the number of class selectors, attributes selectors, and pseudo-classes in the selector (= b)
  • count the number of type selectors and pseudo-elements in the selector (= c) ignore the universal selector
  • Selectors inside the negation pseudo-class are counted like any other, but the negation itself does not count as a pseudo-class.

Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.

Examples:

*/* a=0 b=0 c=0 -> specificity =   0 */
LI              /* a=0 b=0 c=1 -> specificity =   1 */
UL LI           /* a=0 b=0 c=2 -> specificity =   2 */
UL OL+LI        /* a=0 b=0 c=3 -> specificity =   3 */
H1 + *[REL=up]  /* a=0 b=1 c=1 -> specificity =  11 */
UL OL LI.red    /* a=0 b=1 c=3 -> specificity =  13 */
LI.red.level    /* a=0 b=2 c=1 -> specificity =  21 */
#x34y           /* a=1 b=0 c=0 -> specificity = 100 */
#s12:not(FOO)   /* a=1 b=0 c=1 -> specificity = 101 */

Note: Repeated occurrences of the same simple selector are allowed and do increase specificity.

You may calculate and test the specificity of your selectors here

361 questions
22
votes
1 answer

Can an entire CSS ".class" have its specificity as '!important'?

I am working heavily in jQuery UI widgets and theming is becoming hard because of an oversight that I need to work around. Consider this problem:
Then this…
MacLovin
  • 237
  • 1
  • 2
  • 6
20
votes
3 answers

Did Chrome 40 break justify-content CSS overriding?

I noticed with today's Chrome 40 update that justify-content does not seem to get properly overriden by subsequent style declarations. See this fiddle for an example:
Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
17
votes
2 answers

How to view browser's calculated CSS specificity?

Greetings: I have spent days trying to find a tool that would display the exact CSS specificity number for each CSS rule as calculated by the browser. I have already looked at many online resources including Tools to see CSS specificity - the links…
14
votes
3 answers

Tool to see CSS specificity

Does anyone know if there is some kind of tool to see/pick the best CSS selector based on CSS specificity to target a particular div? I know what has higher specificity, but sometimes when working on other people projects where they have deeply…
CodeDevelopr
  • 1,267
  • 3
  • 17
  • 30
12
votes
1 answer

Why do 'foo bar' and 'foo > bar' have the same specificity in CSS?

I'm curious why using > or other combinators does not affect the specificity of CSS selectors, i.e. why div span (matching a span somewhere inside a div) and div > span (matching a span which is the immediate child of a div) are considered equal…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
12
votes
4 answers

Nesting CSS selectors without increasing specificity

Let's take these three selectors, sorted from the highest specificity to the lowest: .special-section p { } .weird-font { } p { } Many CSS gurus recommend against nesting like in the first selector .special-section p,…
Flimm
  • 136,138
  • 45
  • 251
  • 267
10
votes
4 answers

Why is the # selector of lesser specificity than anything?

Big bold caps-lock TL;DR: I KNOW HOW SELECTOR SPECIFICITY IS DETERMINED, I THINK IT USES FLAWED ASSUMPTIONS AND I CAN BACK MY IRRITATIONS UP WITH VALID SET THEORY RELATIONS, PLEASE DO NOT RESPOND EXPLAINING W3 CALCULATION RULES FOR SPECIFICITY,…
DavidJFelix
  • 728
  • 1
  • 6
  • 22
10
votes
2 answers

Same specificity, after taking placement into consideration, :first-letter always wins?

Take a look at this jsfiddle: http://jsfiddle.net/ZNddz/ .intro:first-letter { font-size: 130px; } span.letter { background-color: red; font-size: 30px; } p { font-size: 80px; } The first rule consist of one class selector and one…
Romed7442
  • 139
  • 1
  • 8
9
votes
5 answers

Using two css files in the same html file

Is it possible to use 2 CSS classes that have the same name for the selectors, etc. in the same HTML file? If so, how do you differentiate between the two when styling elements?
Xaisoft
  • 45,655
  • 87
  • 279
  • 432
9
votes
1 answer

Can type selectors be repeated to increase specificity?

The spec states regarding calculating CSS specificity: (bold mine) Note: Repeated occurrences of the same simple selector are allowed and do increase specificity. So for example .class.class {} has twice the specificity than .class {} -…
Danield
  • 121,619
  • 37
  • 226
  • 255
9
votes
3 answers

Why can't I override existing pseudo-elements?

I have two CSS rules following each other: .some td:first-child:before { content:url('path/to/image.png')" " ; } .some .other:before { content:url('path/to/image2.png')" " ; } Here's the HTML snippet:
DanMan
  • 11,323
  • 4
  • 40
  • 61
8
votes
2 answers

How element selector is more specific than id selector?

As I understand elements are least specific. (element vs id). Please help me in understanding the specificity of selectors generally.
FIRST Div inside CONTAINER
SECOND Div…
Shawn Taylor
  • 3,974
  • 4
  • 17
  • 23
8
votes
3 answers

More specific CSS rule is not working

In my following code, I have defined more specific rule for h1 as #inner h1 and less specific rule as #container h1 is also defined. But if #container h1 is put after #inner h1 then it takes effect and #inner h1 is ignored while it shouldn't be…
Shawn Taylor
  • 3,974
  • 4
  • 17
  • 23
8
votes
1 answer

Which CSS pseudo-classes don't have specificity?

I'm studying a bit of CSS and from reading there are some pseudo-classes that don't have specificity like where() and not(). Are there more?
ghost2092000
  • 347
  • 1
  • 4
  • 15
8
votes
5 answers

How do I give a CSS class priority over an id?

I have a element like this: #idname{ border: 2px solid black; } .classname{ border: 2px solid gray; }
it is a test
I want to give a bigger priority to its CSS class instead of its CSS id. Is…
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
1
2
3
24 25