This article mainly shares with you the priority and weight analysis of CSS selectors, hoping to help you learn more about CSS selectors.
Basic Selector
Selector
Instance
Description
Version
|
| ##:link |
a:link |
Matches unvisited links
1 |
:visited |
a:visited |
| Matches visited links
1 |
:hover |
a:hover |
| Matches the element over which the mouse pointer is floating
1 |
:active |
a:active |
| Matches the element over which the mouse pointer is pressed
1 |
:focus |
input:focus |
| Matches the element that receives focus
2.1 |
The target pseudo-classes |
The target pseudo-class specifies the currently active anchor. You can use the target pseudo-class to set styles for the active anchor. |
|
Selector
Instance
Description
Version
|
##:target |
| #tab1:target |
Matches the active anchor
3
|
The language pseudo-classes
Language pseudo-classes add styles to elements with the specified lang attribute.
Selector |
Instance |
Description |
Version |
##:lang(val) |
#p:lang(en)
| Matches the specified lange="en"
Element
| 3 |
##The UI element states pseudo-classes
The UI element status pseudo-class is mainly used to specify the status of elements in the form.
Selector
Instance |
Description |
Version |
|
##:enabled
input:enabled |
Matches the activated element | 3 |
| :disabled
input:disabled |
Match disabled elements | 3 |
##:checked |
input:checked |
Matches the selected element 3 |
|
| ## The #display and visibility properties have no effect on the UI element state pseudo-class matching enabled/disabled state. Structural pseudo-classes Structural pseudo-classes are used to specify the specific structure of the document.
Selector
InstanceDescription | Version |
|
##:root |
:root
Matches the root element of the document |
3
|
:nth-child(n) |
| :nth-child(n)
Matches the nth child element of its parent element |
3
|
:nth-last-child(n) |
| :nth-last-child(n)
matches its The nth child element from the bottom of the parent class |
3
|
:nth-of-type(n) |
| :nth-of-type (n)
Matches the nth child element of its parent class with the same selector |
3
|
:nth-last- of-type(n) |
| :nth-last-of-type(n)
Matches the nth last child element of its parent class with the same selector |
3
|
:first-child |
| :first-child
matches its parent element The first child element of |
3
|
:last-child |
| :last-child
Match the last child element of its parent element |
3
| ##:last-child |
:last-child |
Match the last child element of its parent type element | 3
| :first-of-type |
:first-of-type |
Matches the first child element of its parent element with the same selector | 3
| : last-of-type |
:first-of-type |
matches the last child element of its parent element that has the same selector | 3
| :only-child |
:only-child |
The only child element matching its parent class | 3
##:only-of-type |
| :only-child |
The only one that matches its parent class Child elements with the same selector
3 |
:empty |
| :empty |
match no children Elements (including text nodes) of elements
3 |
|
:nth-child(n) | , :nth- last-child(n) | , :nth-of-type(n), :nth-last-of-type(n) where n is from For integers starting with 0, the expression can be written as an+b, a and b are 0 or positive integers. The expression is equivalent to dividing each a sub-element into a group and taking the b-th element of each group. ;The writeable expression for taking the odd or even numbered sub-elements is 2n+1 or even, 2n or odd . The negative pseudo-class is used to select all other elements that are not elements of the specified type.
Selector
InstanceDescription
Version
|
| ##:not(s) |
input:not([type="text"]) |
Match all other elements that are not of the specified type3 |
Pseudo-elements |
Pseudo-elements refer to abstractions that do not exist in the document tree. |
|
Selector
Instance
Description
Version
|
##::first-line |
| ::first-line |
Matches the first line of the element's text content
1
|
::first-letter
| ::first-letter |
Matches the first letter of the element's text content |
1
| ##::before
::before |
| before the element | 2.1
| ::after
::after |
| After the element | 2.1
|
In CSS 1 and CSS 2, there is only one ":" in the pseudo-class selector, while CSS 3 has two "::" in order to distinguish pseudo-classes and pseudo-elements. Currently, the two writing methods have the same effect. . ::before and ::after must set the content attribute, otherwise the element will not take effect.
Priority and weight
The weight in CSS is divided into 4 levels:
Inline style (style in HTML document Attribute)
ID selector
Class, pseudo-class, attribute selector
Element , Pseudo-class elements
These 4 levels represent different priorities from high to low. !important can be written after the CSS rules to promote the corresponding rules. to the highest weight.
Related recommendations:
css selector example sharing
Detailed explanation of new usage of CSS selector
Implementation method of CSS selector field parsing
|
The above is the detailed content of Priority and weight analysis of CSS selectors. For more information, please follow other related articles on the PHP Chinese website!