Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS class repetition to increase specificity

According to the CSS docs: http://www.w3.org/TR/CSS21/cascade.html#specificity

Specificity is defined by (amongst other things) the number of attributes and pseudo-classes in the selector.

So, my question is, is it possible to increase specificity by repeating the same classname over and over again?

For instance:

would

.qtxt.qtxt.qtxt.qtxt.qtxt
{
}

have a higher specificity than

.qtxt.lalgn
{
}

or

.lalgn .qtxt//(space added to create child selector)
{
}

?

like image 393
Georges Oates Larsen Avatar asked Jul 20 '12 02:07

Georges Oates Larsen


People also ask

How do you increase the specificity of a class in CSS?

As a special case for increasing specificity, you can duplicate weights from the CLASS or ID columns. Duplicating id, class, pseudo-class or attribute selectors within a compound selector will increase specificity when overriding very specific selectors over which you have no control.

Which CSS selector adds the highest level of specificity?

Inline styles have the highest specificity. In our specificity weight system, they have a value of 1000. Let's try to make sense of it. The property values of selectors with a higher weight will always be applied over a selector with a lower weight.

Which CSS rule has the most specificity Main?

Since the third rule (C) has the highest specificity value (1000), this style declaration will be applied.

What is the ordering of CSS entries in increasing precedence and specificity and why?

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.


2 Answers

Yes, it is possible and intentionally so. While this is not mentioned in the CSS2 spec, it is explicitly mentioned in the Selectors 3 spec:

Note: Repeated occurrances [sic] of the same simple selector are allowed and do increase specificity.

Therefore browsers must increase the specificity when encountering repeated simple selectors, as long as the selector is valid and applicable. This not only applies to repeated classes, but also applies to repeated IDs, attributes and pseudo-classes.

Given your code, .qtxt.qtxt.qtxt.qtxt.qtxt will have the highest specificity. The other two selectors are equally specific; combinators have no bearing in specificity calculations at all:

/* 5 classes -> specificity = 0-5-0 */ .qtxt.qtxt.qtxt.qtxt.qtxt  /* 2 classes -> specificity = 0-2-0 */ .qtxt.lalgn  /* 2 classes -> specificity = 0-2-0 */ .lalgn .qtxt 

Also, the space in your last selector is the descendant combinator; the child combinator is >.

like image 158
BoltClock Avatar answered Sep 25 '22 14:09

BoltClock


.qtxt.qtxt.qtxt would have the highest specificity...

http://jsfiddle.net/nXBTp/1/

However, this is only the case if you repeat the class name more times that any other selector, for example:

http://jsfiddle.net/nXBTp/2/

like image 21
smilly92 Avatar answered Sep 25 '22 14:09

smilly92