This question already has answers here:
How are the points in CSS specificity calculated
(7 answers)
Closed 4 years ago.
HTML
<div class="parent">
<div class="child-1">Navigation Bar</div>
<div class="child-2">Background Image</div>
<div class="child-3">Features</div>
</div>
CSS
.parent div {
background: rgb(105, 105, 109);
color: white;
padding: 10px;
border: 5px solid black;
margin: 10px;
}
.child-1 {
position: fixed;
top: 0;
left:0;
margin:0;
width:100%;
box-sizing: border-box;
}
when I inspect the child element in Chrome I expect the margin:0 to be enabled. However the margin:10px is being applied ? How is this behaviour explained ?
Refer this: https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Cascade_and_inheritance#Specificity
Specificity is basically a measure of how specific a selector is — how many elements it could match. As shown in the example seen above, element selectors have low specificity. Class selectors have a higher specificity, so will win against element selectors. ID selectors have an even higher specificity, so will win against class selectors. The only way to win against an ID selector is to use !important.
The amount of specificity a selector has is measured using four
different values (or components), which can be thought of as
thousands, hundreds, tens and ones — four single digits in four
columns:
Thousands: Score one in this column if the declaration is inside a
style attribute (such declarations don't have selectors, so their
specificity is always simply 1000.) Otherwise 0.
Hundreds: Score one
in this column for each ID selector contained inside the overall
selector.
Tens: Score one in this column for each class selector, attribute
selector, or pseudo-class contained inside the overall selector.
Ones: Score one in this column for each element selector or
pseudo-element contained inside the overall selector.
So, going by the rules the second selector .child-1 has only a classname. Hence the score will be 10
The first selector has a class and an element. So the score will be 11
Related
This question already has answers here:
What does a space mean in a CSS selector? i.e. What is the difference between .classA.classB and .classA .classB? [duplicate]
(3 answers)
Closed 8 months ago.
How to remove the element background color using browser extension for a online website? I want to remove this color for add this website in OBS?
I've tried this:
main-content wf100 {
background-color: transparent;
}
.main-content .wf100 {
background: transparent;
}
#main-content .wf100 {
background: transparent;
}
main-content and wf100 are two classes for the same element. So, the code will be like this--
.main-content.wf100{
background: transparent;
}
if this does not work, use this !important flag on CSS value.
Example--
.main-content.wf100{
background: transparent !important;
}
I think you need to use !important in end of your code
Example:
.main-content.wf100 {
background: transparent !important;
}
just write like this:
.main-content {
background-color: transparent;
}
if didn't work add !important after transparent
First, none of your selectors are applied. The first and third one aren't because main-content is a class, so you have to use .main-content.
The second one isn't applyed to your element because you added a space between .main-content and .wf100 wich means :
element with wf100 class inside a main-content element.
Without the the space (.main-content.wf100) you specify :
elements with main-content and wf100 classes.
Now your selector is correct, it still doesn't work. Why ? because inline css has the highest priority after !important property that you need to use here.
Because !important has the highest priority, you can apply it to .main-content.wf100 but also .main-content or .wf100.
/* wrong selector */
.main-content .wf100{
background-color:green;
}
/* correct selector, but not enough priority */
.main-content.wf100{
background-color:green;
}
.main-content.second-content{
background-color:orange!important;
}
.another-content{
background-color:yellow!important;
}
<div class="main-content wf100 "style="background-color:#172132;color:white;">wf100</div>
<br>
<div class="main-content second-content" style="background-color:red;">second content</div>
<br>
<div class="main-content another-content" style="background-color:red;">another content</div>
<br>
<div class="another-content" style="background-color:red;">another content without .main-content</div>
If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will "win", and its style declaration will be applied to that HTML element.
Inline Styles - 1000
ID selectors - 100
Classes, Attributes and Pseudo-classes - 10
Elements and Pseudo-elements - 1
So you can use !important for your CSS code.
.main-content.wf100 {
background: transparent;
}
The correct way to do this is to delete the inline css.
This question already has answers here:
Understanding CSS selector priority / specificity
(4 answers)
Closed 3 years ago.
does use id or class to select the element matter? The whole navigation div should be blue when it is hovered since it is applied after the magenta.
.navigation {
color: salmon;
height: 100px;
}
#navigation:hover {
color: magenta;
}
.navigation:hover {
color: blue;
}
.navigation li a {
text-decoration: none;
color: inherit;
}
<div id="navigation" class="navigation">I am navigation text!
<ul>
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
</div>
Please refer to this resource: Calculating CSS Specificity Value
... Simply using the class name by itself had a lower specificity value and was trumped by the other selector which targeted the unordered list with the ID value. The important words in that sentence were class and ID. CSS applies vastly different specificity weights to classes and IDs. In fact, an ID has infinitely more specificity value! That is, no amount of classes alone can outweigh an ID.
In summary, ID is more specific than Class, therefore an element with the same CSS attribute being specified (e.g. color) within an ID and Class will have the value of the ID attribute specification, not the Class specification.
Inline styles are more specific yet again, and will trump an ID style.
As sourced from: https://css-tricks.com/specifics-on-css-specificity/#article-header-id-0
I'd like to understand how CSS selectors work with property collisions. How is one property selected over another one?
div {
background-color: red;
}
div.my_class {
background-color: black;
}
div#my_id {
background-color: blue;
}
body div {
background-color: green;
}
body>div {
background-color: orange;
}
body>div#my_id {
background-color: pink;
}
<div id="my_id" class="my_class">hello</div>
How does selector priority work?
I'll just toss in a link to the CSS 2.1 spec itself, and how browsers are supposed to calculate specificity:
CSS 2.1 Section 6.4.3:
A selector's specificity is calculated as follows:
count 1 if the declaration is from is a 'style' attribute rather than a rule with a selector, 0 otherwise (= a) (In HTML, values of an element's "style" attribute are style sheet rules. These rules have no selectors, so a=1, b=0, c=0, and d=0.)
count the number of ID attributes in the selector (= b)
count the number of other attributes and pseudo-classes in the selector (= c)
count the number of element names and pseudo-elements in the selector (= d)
The specificity is based only on the form of the selector. In particular, a selector of the form "[id=p33]" is counted as an attribute selector (a=0, b=0, c=1, d=0), even if the id attribute is defined as an "ID" in the source document's DTD.
Concatenating the four numbers a-b-c-d (in a number system with a large base) gives the specificity.
If the specificities are equal, then CSS 2.1 Section 6.4.1 comes into play:
Finally, sort by order specified: if two declarations have the same weight, origin and specificity, the latter specified wins. Declarations in imported style sheets are considered to be before any declarations in the style sheet itself.
Note that this is talking about when the style is defined, not when it is used. If classes .a and .b have equal specificity, whichever is defined last in the stylesheet(s) wins. <p class="a b">...</p> and <p class="b a">...</p> will be styled identically, based on the definition order of .a and .b.
Element
Class selectors
ID Selectors
Inline styles
!important
In order, 1 is the lowest specificity and 5 is the highest.
https://youtu.be/NqDb9GfMXuo will shown details to demo it.
What you are interested in is specificity.
Firebug is a great tool to help inspect this. But other browsers also have built in tools for inspecting the applied CSS rules.
You can refer the full answer here Mozilla documentation
Start from the most specific:
id selectors > class selectors > type selectors(normal h1, p tag and so on..)
!important always wins, but it is considered a bad practice.See the link above.
The best way is to experiment with it:
<!-- start class vs id -->
<p class="class1" id="id1">.class vs #id: The winner is #id</p>
<!-- upper vs bottom -->
<p id="id2">the very bottom is the winner</p>
<!--most specific is the winner -->
<p id="id3">the most specific</p>
<!--pseudo and target selector -->
<h3>pseudo vs type selector</h3>
<!-- !important is more important! -->
<h1 id="very-specific">HI! I am very important!</h1>
</body>
CSS:
#id1{
color: blue;
}
.class1{
color: red;
}
#id2{
color: yellow;
}
#id2{
color : green;
}
body p#id3{
color :orange;
}
body p{
color : purple;
}
body{
color : black;
}
h3::first-letter {
color: #ff0000;
}
h3{
color: CornflowerBlue ;
}
h1{
color: gray !important;
}
body h1#very-specific{
color: red;
}
Here's a test case.
I have this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="stylesheet.css"/>
<title>My Social Network</title>
</head>
<body>
<div id="best_friend" class="friend" ></div>
<div class="family"></div>
<div class="enemy" id="archnemesis"></div>
</body>
</html>
div {
display: inline-block;
margin-left: 5px;
height: 100px;
width: 100px;
border-radius:100%;
border: 2px solid black;
}
#best_friend {
border: 4px solid #00C957;
}
.friend {
border: 2px dashed #008000;
}
.family {
border: 2px dashed #0000FF;
}
.enemy {
border: 2px dashed #FF0000;
}
#archnemesis {
border: 4px solid #CC0000;
}
My question is: Notice how I define border for both class and id. The border that is applied is the one coming from the id. Why so? Why is the border specification in id overriding the one in class.
The browser determines which styles to apply in what order based on Specificity, the higher number determines which will be applied.
Universal selectors have a specificity of 0,0,0,0.
* = 0
HTML selectors have a specificity of 1.
p, div, etc.. = 1 each
So each HTML selector adds to the specificity.
div p = 2, div p span = 3
Class selectors have a specificity of 10.
.class = 10
Class selectors combined with HTML selectors.
div p.class = 12
ID selectors have a specificity value of 100.
#id = 100
ID selectors combined with HTML selectors.
#id div = 101
!important overrides all other styles unless combined with another selector.
table td {height: 50px !important;} Would override any height style applied to only a td within a table.
Inline styles have the highest specificity of 1000.
style= = 1000
Useful resources
CSS Specificity: Things You Should Know
Specificity | HTML Dog
Without including exceptions (e.g. :not, !important), the following list of selector types is by increasing specificity:
Universal selectors (e.g., *) (lowest)
Type selectors (e.g., h1)
Class selectors (e.g., .example)
Attributes selectors (e.g., [type="radio"])
Pseudo-classes (e.g., :hover)
ID selectors (e.g., #example)
Inline style (e.g., style="font-weight:bold") (highest)
Ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
The border that is applied is the one coming from the id. Why so? Why is the border specification in id overriding the one in class.
From this list, you can see that id is higher than class, so the border set in id will be applied.
id has more priority than class.
Following rules are applied for css
1.) When more than 1 overlapping styles are applied to the same element, only the last style is visible
2.) When the !important attribute is used, it has the highest priority
3.) The style that has the highest CSS specificity is applied. The specificity of different elements is defined as follows:
ID attribute = 100
Class attribute = 10
Element = 1
Refer this link
Because id has a higher precedence than class you can verify that in this official documentation.
Specificity hierarchy
Every selector has its place in the specificity hierarchy. There are four distinct categories which define the specificity level of a given selector:
Inline styles (Presence of style in document).
An inline style lives within your XHTML document. It is attached directly to the element to be styled. E.g. <h1 style="color: #fff;">
IDs (# of ID selectors)
ID is an identifier for your page elements, such as #div.
Classes, attributes and pseudo-classes (# of class selectors).
This group includes .classes, [attributes] and pseudo-classes such as :hover, :focus etc.
Elements and pseudo-elements (# of Element (type) selectors).
Including for instance :before and :after.
Ref:http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/
and here : http://fourword.fourkitchens.com/article/css-specificity-id-overrides
.formWrap input[type="text"] {
font-size: 12px;
}
.bigger {
font-size: 14px
}
<div class="formWrap">
<input type="text" class="bigger" />
</div>
Why does the above not make the text of the input field 14px?
When I am using the input[type="text"] selector how can I target a particular class that is a child of formWrap?
Is there a better method to give all my inputs a particular style then be able to make adjustments to certain inputs (hopefully based on a class)?
The answer is Specificity.
Your first selector has higher specificity than your second selector, so its properties are prioritised.
.formWrap input[type="text"] contains a class, an element and attribute selector. As per the CSS selectors specification (linked above), this selector has a specificity of 21. .bigger on the other hand only has a class selector, and therefore only has a specificity of 10. 21 is greater than 10.
From MDN:
Specificity is the means by which a browser decides which property values are the most relevant to an element and gets to be applied. Specificity is only based on the matching rules which are composed of selectors of different sorts.
In your case, an easy way to get your second selector to be prioritised over the first is by increasing this selector to have equal or higher specificity:
input.bigger {} /* 11 */
You can even repeat the same selector multiple times (as also noted in the specification):
input.bigger.bigger {} /* 21 */
Note that if the specificity of both selectors are equal, the selector included later in your stylesheet will be the one prioritised.
The first selector has higher specificity than the .bigger class.
To increase the specificity of the .bigger class, you could do this:
.formWrap input[type="text"]{
font-size: 12px;
}
.formWrap input[type="text"].bigger{
font-size: 14px
}
OR (Not really recommended):
.bigger{
font-size: 14px !important;
}
There is a good article on specificity and how to calculate here. Something that takes a while to click when first starting to write CSS.
Calculating CSS Specificity Value
http://css-tricks.com/specifics-on-css-specificity/