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 Preface 1. Prepare new partitio...
1: Statement order of grouping function 1 SELECT ...
MySQL bidirectional backup is also called master-...
1. Concept 1. The difference between hot backup a...
<br />Original: http://uicom.net/blog/?p=762...
Project scenario: When running the Vue project, t...
As a tester, you may often need to install some s...
MYSQL officially provides an Installer method to ...
I believe that many users who make websites will ...
Question: How to achieve a rounded rectangle usin...
Environment: CentOS 7 Official documentation: htt...
By default, Flash will always be displayed at the ...
MySQL installation (4, 5, 6 can be omitted) State...
When using XAML layout, sometimes in order to make...
Downloaded the unzipped version of mysql-5.6.37-w...