This question already has answers here:
Is it possible to give one CSS class priority over another?
(7 answers)
Does repeating a classname in a CSS rule increase its priority?
(2 answers)
Can type selectors be repeated to increase specificity?
(1 answer)
Closed 2 years ago.
There is a situation that I need to change the class priority. For example:
.one {
color: green;
}
.comment {
color: gray;
}
.red {
color: red !important;
}
<div class="one two red">Content</div>
<div class="comment two red">Content</div>
At the moment, .red color is applied. Is it possible to force .comment.red to get the color from .comment?
Note: The .comment color is unknown (comes from changeable themes).
Yes, you can use multiple classes to make it more specific. Also, we know that .comment.red needs to have min two classes. So I feel this is allowed:
.one {
color: green;
}
.comment.comment {
color: gray;
}
.red {
color: red;
}
<div class="one two red">Content</div>
<div class="comment two red">Content</div>
But please get rid of !important.
The .comment's rule, instead of it being like this:
We can make it like this:
More Explanation
Modern browsers compute the style contexts using the rule tree. If multiple rules have the same weight, origin and specificity, the one written lower down in the stylesheet is considered and wins.
When there's only one selector here:
.class1 {}
The weight of the above rule is 0010 as per CSS specificity. The same way, if there are two classes:
.class1.class2 {}
.class1.class1 {}
Note that in the second line, I have written twice the same class. Both will be computed to 0020, which is higher than the first one, in spite of the "unknown" class, we have two classes in the rule now.
This is the same trick I used in the above example to make the theming easier.
Related
This question already has answers here:
How to create a css rule for all elements except one class?
(3 answers)
Closed 8 months ago.
How do I select all but one instance of an element?
for example, my link or <a> elements have a particular color, but there's this one a element I don't want the color to to apply.
I want the link element to have the same color as the remaining text
There is a :not selector in CSS. This will apply styles to everything, except elements that the not selector will target.
So, you could style the colors of all anchor tags except ones that you put a specific class on like:
a:not(.dontBeRed) {
color: red;
}
You can also target exceptions other ways. Lets say you have a few custom color utility classes, you could ignore elements that have any class with a certain prefix.
This will style all anchors red unless they have a class that starts with u-color on them.
a:not([class*='u-color']) {
color: red;
}
.u-color--green {
color: green;
}
.u-color--blue {
color: blue;
}
This question already has answers here:
Does the order of classes listed on an item affect the CSS?
(5 answers)
Closed 1 year ago.
If We do something like this in our css
.class01.class02.class03{ background-color: lavenderblush; }
.class03.class01.class02{ background-color: brown; }
and this in our html
<article class="class01 class02 class03"></article>
<article class="class03 class01 class02"></article>
the 2nd rule will be applied to both elements. Is there a way to make these rules apply according to the order?
It won't work because: Order of CSS classes in HTML doesn't matter but there is a way using attribute selector
article[class="class01 class02 class03"]{ background-color: lavenderblush; }
article[class="class03 class01 class02"]{ background-color: brown; }
<article class="class01 class02 class03">Test1</article>
<article class="class03 class01 class02">Test2</article>
You can use the [class="class1 class2 class3"] attribute in the css by specifying the order of classes you want.
article[class="class01 class02 class03"] {
background-color: lavenderblush;
}
article[class="class03 class01 class02"] {
background-color: brown;
}
<article class="class01 class02 class03">text</article>
<article class="class03 class01 class02">text</article>
Interesting question. I don't suggest doing it as classes aren't meant to be used like this, but I guess there's at least one way for accomplishing the desired result...
article {
width: 50px;
height: 50px;
}
article[class*="class01 class02 class03"] {
background: lavenderblush;
}
article[class*="class03 class01 class02"] {
background: brown
}
<article class="class01 class02 class03"></article>
<article class="class03 class01 class02"></article>
So we're checking that the class contains the exact provided substring, while the wildcard allows the class list to contain other classes before and after as well. When an exact match is needed we could use article[class="class03 class01 class02"].
The answer to the question is no: it is not possible to the normal common notation. Only the trick using the attribute selector as given in the other answers does work on the first look. But head up: it is not as easy as it seems.
EXPLANATION: HOW IT WORKS
Indeed you are able to calculate/controll it.
In HTML/CSS wins the element/class/id construct with highest specifity. It is a slightly complex ranking on the first view but if only used classes it is not as complicated.
For class construction a.b.c the specifity counts:
special combination off this three classes which overrides i.e. background setting of a.b.x
the total count of used classes what means this three classes overwrite i.e. background setting off a.b
NOTE ATTRIBUTE SELECTOR: One attribute selector counts as one class. So, if you have an attribute seletor [class="a b c"] it lose against c.b.a noted on another place in your stylesheet.
Order in HTML does not count for the specifity: a b c is the same combination and the same number of classes as c b a.
If a class construct (combination) with the same specifity noted twice in a CSS i.e. a.b.c: red and c.b.a: green the last notation overwrites the notation done before.
Some thinking:
Importand: the decision which concept is used to style a page is up to the coder himself!!! But even the attribute selector is a chance to realize that concept it is not the intendend way to use CSS. I asume using the rules of specifity in the intended css way could be a better/more intuitive way to control the design.
Links with information about specifity:
https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
https://specifishity.com/
This question already has answers here:
Is it possible to reference one CSS rule within another?
(6 answers)
Closed 5 years ago.
Suppose I have a CSS class:
.text-red {
color: red;
}
This class is defined elsewhere, may be supercomplex, and is not editable.
In my DOM I have several paragraphs. I want to apply text-red class to all paragraphs. Of course I may write that directly:
<p class="text-red">XXX</p>
<p class="text-red">YYY</p>
<p class="text-red">ZZZ</p>
<p class="text-red">WWW</p>
but it is so redundant. I'd like to write in my CSS file something like:
p {
.text-red
}
so that all "p" elements have that class applied.
Clearly this is not a CSS valid rule. How may I do?
You can easily do so with SASS pre-processor by using #extend.
https://sass-lang.com/guide
Ohterwise, you could use JavaScript (jQuery) as well:
$('p').addClass('.text-red')
But maybe the easiest would be to copy the properties to the p selector?
This is valid CSS:
p.text-red { color: red; }
Referring you to Child combinator:
https://developer.mozilla.org/en-US/docs/Web/CSS/Child_selectors
example
.text-red > p {
...
}
this selects all the p children of .text-red class
This question already has answers here:
Why does .foo a:link, .foo a:visited {} selector override a:hover, a:active {} selector in CSS?
(3 answers)
Closed 5 years ago.
Why does w3schools say that when we write anchor pseudo-classes in CSS we should write a:link first then a:visited followed by a:hover and finally a:active to be effective?
From: https://www.w3schools.com/css/tryit.asp?filename=trycss_link
How does the order of pseudo-classes effect the effectiveness?
Because there is a priority order.
if hover was before visited, then hover wouldn't get ever applied, because it would be "forever" overwritten by visited style (if it has been really visited), that was applied after.
Same goes for :active (mouse down) - if it's defined before hover, then hover will always overwrite :active(mouse down)
Makes sense?
On the other hand, this is just "conventional rule" - it is not forced. If anyone wants to have :visited higher priority, overriding :hover/:active - you are free to do so - it simply would be just unconventional
And lastly, it is not only order that plays the role of style priority.
Elements that are more explicitly defined have higher priority.
Styles that are !important will have higher priority than explicitly defined styles.
Explicitly defined styles with !important and set last will have "ultimate" priority.
To question "Why would you want to use these to override styles? Wouldn't it be better just to make styles in your CSS file correctly ordered?" - Reason to use overrides by more explicit definition and !important priority overrides comes handy when you use large css/theme/bootstrap, that you haven't created and you have to quickly override/change some stuff... These dirty overrides come as a quick/cheap (not very pretty) solution.
.theBad:active {
color: red;
}
.theBad:hover {
color: green;
}
.theGood:hover {
color: green;
}
.theGood:active {
color: red;
}
the Good - this will turn red on mouse down
<br />
the Bad - this poor little thing will not
<!--#ordermatters, The Ugly is lurking somewhere in the shadows-->
From SitePoint
This isn’t the only useful order, nor is it in any way the “right” order. The order in which you specify your pseudo-classes will depend on the effects you want to show with different combinations of states.
This question already has answers here:
Twitter bootstrap override `a` in `navbar`
(3 answers)
Closed 9 years ago.
bootstrap.css is overriding my style... How can I avoid this to happen? How can I force the css to load first?
One thing that is a problem is that the website where it needs to be is using #import to load the css files (this cannot be changed, the customer doesn't want to change that).
Any ideas?
***Note****
I cannot modify the current site at all. I just have to include bootstrap.css without overriding anything else. Is this possible?
You need to write a more specific selector. Remember that ID has the most weight. You can temporarily mark it with !important to check if you are targeting it correctly.
If you are targeting a anchor in a list item for example then to overwrite the reset styles you can write something like nav ul li a{color: black;}
You simply have to increase the specificity of your rules. This means adding more to your selectors, say you have .elem { color: blue; }, you can make it .parent .elem { color: blue; }
Or using !important, like .elem { color: blue !important; }