Preface If CSS is the basic skill of front-end development, then "selectors" are the foundation of the foundation. If you are copying or learning these confusing selectors, then you have come to the right place, my old friend. This article will directly compare their features to help you quickly master them:
first-child & last-child These two selectors will match the first element in a set of siblings: Note: There are actually three conditions that need to be met for this selector to work:
I won't go into details about first-of-type & last-of-type These two selectors will match the first (last) element of the same type regardless of whether the element is actually the first (last) element in the group: Note: There are actually two conditions that need to be met for this selector to work:
I won't go into detail about only-child & only-of-type The "isolated" elements in the above figure are the first Because the first nth-child & nth-last-child The most interesting thing about these pseudo-class selectors is that they can pass in a formula In fact, our way of thinking is solidified by CSS, because it is very easy to figure out the rules from a mathematical point of view. For example, there is the following code: <style> p:nth-child(2n+1){ color:blue; } </style> <body> <p>First line</p> <p>Line 2</p> <p>Line 3</p> </body> Thinking Mode:
result: |
formula | explain |
---|---|
2n | All even elements |
2n+1 | All odd elements |
n & n+1 | All Elements |
n+2 | Elements after the second element (including the second element) |
n+3 | Elements after the third element (including the third element) |
0n | Nothing matches |
3n+4 | 4,7,10,13 .... |
1 | Only matches the first element |
-n+2 | Only matches the first two elements |
nth-child(odd) | Odd elements |
nth-child(even) | Even elements |
But don't forget nth-child
still matches the same set of sibling elements, but what's interesting is nth-child
will use the selector to filter, but when applying the style, it does not apply the style to the matched elements:
In the above figure, the two groups of <p>
elements in <div>
are matched as sibling elements, but interestingly, the third <p>
element "the third row" is also matched, which means that the style will be applied directly to the group of sibling elements instead of the matched <p>
element. However, it should be noted that if <p>
in the "third group" in the picture is <div>
, the different type styles will not be applied.
nth-last-child
is the back-to-front version, which I will not give a detailed example here:
MDN also gives an interesting example, which can control the style of elements according to the number of elements:
li:nth-last-child(n+3), li:nth-last-child(n+3) ~ li { color: red; }
<h4>A list of four items (styled):</h4> <ol> <li>One</li> <li>Two</li> <li>Three</li> <li>Four</li> </ol> <h4>A list of two items (unstyled):</h4> <ol> <li>One</li> <li>Two</li> </ol>
nth-of-type & nth-last-of-type
nth-of-type
matches:
Did you notice that nth-of-type
is different from nth-child
? After calculation, the style is applied to the matched element, not to sibling elements.
nth-last-of-type
-front version, which will not be described in detail here:
The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.
>>: Detailed explanation of deploying Elasticsearch kibana and ik word segmenter in docker
Table of contents Layout part: <div id="a...
Table of contents Reactive Function usage: toRef ...
Overview Nginx can use variables to simplify conf...
describe: When the Tabs component switches back a...
Demand scenario: The boss asked me to use the cra...
CocosCreator version: 2.3.4 Cocos does not have a...
Table of contents Basic usage of Promise: 1. Crea...
Block-level element features : •Always occupies a ...
Table of contents 1. Traversal Class 1. forEach 2...
Table of contents Overview Type Assertions in syn...
This command modifies the data table ff_vod and a...
Table of contents Preface Simulating data Merged ...
1. Docker pull pulls the image When using $ docke...
Preface I just bought a new VPS. The data disk of...
This article describes how to use MySQL to export...