CSS specificity
The specificity as a ranking of CSS rules, which CSS rules are higher, is applied to the element.
What is CSS Specificity?
If there are two or more CSS rules that conflict with the same element, the browser will follow some rules to determine the priority for rules, which ones have the highest priority. will be applied.
We have the following example to make it easier to imagine:
In the code below, the background-color section for the div element was simultaneously reviewed twice in both #test selector with red background and selector div with yellow background, running the result will produce The output has a red background because the id #test has a higher priority than the other object.
To make it easier to imagine, imagine your Specificity as a ranking of CSS rules, which CSS rule is higher, and it applies to the element.
So what is that ranking like? Let's find out in TipsMake.com in the Specificity Hierarchy hierarchy below.
Specificity rankings - Specificity Hierarchy
If there are two or more CSS rules that target one element, then the browser will follow only one rule, see in the CSS rule list which is the highest rule to select and apply to the element.
Here we will have the rankings for CSS rules so that the browser obeys when applying CSS, arranged in priority order from top to bottom:
- Inline style : set CSS properties directly inside an element.
For example: - ID: set CSS properties for a unique identifier in a page.
For example: #menu, #header - Class, attribute, pseudo-class: eg class like .menu, .header ., attributes like a [target] and pseudo-class like: hover,: focus .
- Element, pseudo-element: element such as h1, h2, div, p . and pseudo-element such as :: before, :: after, :: selection.
Calculation Specificity
Calculate the value of CSS Specificity to see which rules apply to the element, making it easy to make mistakes when writing CSS.
Specificity is counted as positive integers. As follows:
- Inline style is 1000.
- ID is 100.
- Class, attribute, pseudo-class is 10.
- Element, pseudo-element is 1.
For example:
A: h1
B: #content h1
C:
Heading
- A is 1 (an element is h1).
- B is 101 (an ID is content, an element is h1).
- C is 1000 (inline style).
Because 1 rule (C) has the highest priority, so the background color for h1 element is white (#ffffff).
Rules for Specificity
1. If there are equal Specificities, the last write rule will be applied
Where we have two equal CSS rules like this:
Website TipsMake.com - Heading 1
Obviously, the element's specificity value is the same (all h1 elements) now we will rule which selector is closest to that element to be applied. So the result will be:
2. ID selector will have a higher specificity value than Attribute selector
In case we have CSS rules like this:
Website TipsMake.com
By law, ID selector will be applied. The result is:
3. Class selector will have a higher specificity value than element selector
A class selector like .intro will "defeat" every single element like h1, p, div .
Website TipsMake.com
4. Specific context selector will have higher specificity value
For example, in the following situation:
From external CSS file:
In the HTML file:
The second rule will prevail.
5. Universal selector (*) and inherited value with specificity of 0
Universal selector or CSS's entire selection symbol (*) will select all objects of any type.
/* Chọn tất cả đối tượng */
* {
color: green;
}
Universal selector has specificity of 0 and properties have values that are inherited, also have specificity of 0.
Last lesson: Unit - Unit of measurement in CSS
Next lesson: Rounded Corner in CSS
You should read it
- How to set priorities for applications in Windows 10
- Change traffic traffic through priority mode with DD-WRT
- How to highlight important notifications on iPhone
- How to set up preferred WiFi connection mode on Mac
- How to set up Wifi priority mode on Android and iPhone
- How to manage the priority of I / O process in Linux
- Bitsadmin command getowner, get priority and getproxybypasslist
- Plugins on well-known editing tools can give hackers priority
- Set up preferred bandwidth for games, applications on Linksys router
- Using your digital camera's aperture and speed-priority shooting modes
- How to set up the app update order on iOS 10
- How to arrange network connection on Windows 10
Maybe you are interested
Delete file with 'Access is denied' - Fixing tips Quality Assurance or Quality Control: Difference Explained Detecting 50 square holes of ancient houses in Sichuan, China Funny photo series in Photoshop battle of photo-making experts (Part 1) 25 misleading images make you stare and look back 20+ images prove that children are an indispensable source of entertainment