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
8
votes
1 answer

CSS :hover effect not working when I set an ID to the paragraph

I have the following css3 transition with ease effect: HTML

CSS * { padding: 0; margin: 0; } …
user1096494
8
votes
1 answer

Order by CSS Specificity

My main goal is to try to reorder a CSS style block based on specificity. I previously had helped from SO and I managed to produce this function. See gist. Here is an example: function specificity($selector){ //…
Abs
  • 56,052
  • 101
  • 275
  • 409
7
votes
6 answers

text-decoration:none doesn't remove text decoration

Consider the following code HTML: Home[2] CSS: .c1 { text-decoration:underline; } #id1 { text-decoration:none !important; } Now I expected Home to have an underline while the superscript [2]…
sasidhar
  • 7,523
  • 15
  • 49
  • 75
7
votes
1 answer

Why don't media queries override normal CSS?

Do I need to add !important to all properties in the media queries I've written for my site like in the example below? I had the CSS below at the bottom of my stylesheet, but I found that these properties did not reflect my design until I added the…
grantfoster
  • 73
  • 1
  • 7
7
votes
4 answers

Why doesn't my child element inherit color from its parent when parent has more specific selector?

Why in the following code world is blue rather than red? The specificity of .my_class is 0,0,1,0, but it should inherit the color of #my_id whose specificity is higher at (0,1,0,0). #my_id { color: red; } .my_class { color: blue; }
Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
7
votes
4 answers

:nth-of-type selector overrides all other CSS selectors

I am trying to revise for my exam on HTML, JavaScript and CSS3; and I am a little confused at CSS selectors and which take priority. I have the following CSS: table { border: 1px solid black; } tr:nth-child(odd) {…
user3284707
  • 3,033
  • 3
  • 35
  • 69
6
votes
3 answers

How does CSS specificity decide which styles to apply?

How does CSS determine when to apply one style over another? I have been through the W3 CSS3 selectors document a few times, and that has helped me understand how to better use CSS selectors in jQuery, but it has not really helped me understand when…
Jonathan
  • 5,495
  • 4
  • 38
  • 53
6
votes
6 answers

CSS !important rule not overriding text alignment

a { font-size: 8pt; text-align: left !important; text-decoration: none; } .main { text-align: center; }
Mathias Schnell
  • 884
  • 3
  • 13
  • 22
6
votes
2 answers

How can I specifically target this element id with CSS?

content

I have a CSS selector #checkout form.center already being used i would like to override this specifically for the confirmation form, but none of the things I…
Damon
  • 10,493
  • 16
  • 86
  • 144
6
votes
7 answers

Best Practices for CSS Specificity?

I am creating a contact form that will be included on several different sites. The styles of the contact form and the styles of the site will both be included and I can't very well predict the styles of the site. I want the styles of the contact…
Jo Sprague
  • 16,523
  • 10
  • 42
  • 62
6
votes
3 answers

Specificity issue in CSS

So I have a list of items inside a div with the class book-select and one of the li's in my unordered list has the class selected. According to the CSS rules I've defined, the li's in the div has the background color skyblue and the one li with the…
Jonathan Kuhl
  • 699
  • 9
  • 23
6
votes
2 answers

How can I override a selector with many negations (:not)?

For example to style standard inputs I write something like: input:not([type="checkbox"]):not([type="radio"]) { background-color: blue; } However that increases the specificity a lot so if I want to override it using a class I have to do…
joshhunt
  • 5,197
  • 4
  • 37
  • 60
6
votes
3 answers

Overwrite CSS class selectors without !important

I'm just writing a javascript UI dialog for a web app. The problem is that users can create there own themes for the web app, which may include element css selectors (h1 {...}, div {...} etc.) which overwrite my css formatting for the UI dialog. The…
wowpatrick
  • 5,082
  • 15
  • 55
  • 86
6
votes
4 answers

How to make css selectors with the same specificity be applied in order of HTML tags parenthood?

There are a bunch of same-level CSS styles and an HTML code with nested blocks to which the styles are applied: .style1 a { color: red; } .style2 a { color: green; }
Style 2
Finesse
  • 9,793
  • 7
  • 62
  • 92
6
votes
1 answer

Override Bootstrap table border-collapse style

Bootstrap has a table {border-collapse: collapse; border-spacing:0;} style. I want to override this so I've create a class and applied it to the table in question: table.FormGroupContainer { border-collapse: separate; border-spacing:…
1 2
3
24 25