Speaking of pseudo-selectors, I really realized how powerful CSS is. It’s so powerful that I don’t even seem to know CSS anymore. It’s a bit of a shock to us with some syntactic sugar in C# 6.0. . . First
We can preview it in VS in advance.
As you can see, there are many, many pseudo-classes above, so many that I am almost blind. . . Let’s talk about some of the more practical ones below.
1 :nth-child pseudo-selector
We know that there is a selector in jquery called "subclass selector", and the corresponding one is:nth-child , :first-child, :last-child, :only-child, this time it can also be done in CSS
, which can be said to relieve the pressure of jquery to a certain extent. Here is a simple example.
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title></title> 5 6 <style type="text/css"> 7 ul li:nth-child(1) { 8 color: red; 9 }10 </style>11 </head>12 <body>13 <ul>14 <li>1</li>15 <li>2</li>16 <li>3</li>17 <li>4</li>18 <li>5</li>19 <li>6</li>20 </ul>21 </body>
You can see that when I fill in :nth-child(1), the color of the first li of ul has changed. It’s red. If it’s more complicated, you can change 1 to n. When the browser parses the CSS pseudo-class
selector, it should internally call the corresponding method to parse the corresponding dom node. First of all, you must understand that n increases from 0 with a step size of 1. This is similar to jquery's nth-child. There is nothing
to say. Then let's try: first-child and last-child.
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title></title> 5 6 <style type="text/css"> 7 ul li:first-child { 8 color: red; 9 font-weight:800;10 }11 12 ul li:last-child {13 color: blue;14 font-weight: 800;15 }16 </style>17 </head>18 <body>19 <ul>20 <li>1</li>21 <li>2</li>22 <li>3</li>23 <li>4</li>24 <li>5</li>25 <li>6</li>26 </ul>27 </body>28 </html>
Two: checked, :unchecked,:disabled,:enabled
Also in jquery, There is a set of selectors called "form object properties", we can look at jquery's online documentation.
We are also very happy to find that these attributes also exist in css. . . Are you starting to get a little drunk? . . Still a sneak peek.
1. disabled,enabled
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 7 <style type="text/css"> 8 input[type='text']:enabled { 9 border: 1px solid red;10 }11 12 input[type='text']:disabled {13 border: 1px solid blue;14 }15 </style>16 17 </head>18 <body>19 <form>20 <input type="text" disabled="disabled" />21 <input type="text"/>22 </form>23 </body>24 </html>
2. checked, unchecked
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 7 <style type="text/css"> 8 form input[type="radio"]:first-child:checked { 9 margin-left: 205px;10 }11 </style>12 13 </head>14 <body>15 <form>16 <input class="test" type="radio" value="女" /><span>女</span><br/>17 <input class="test" type="radio" value="男" /><span>男</span>18 19 </form>20 </body>21 </html>
3. selected
Although this is not original in css, it can be replaced by option:checked, such as the following .
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 7 <style type="text/css"> 8 option:checked { 9 color: red;10 }11 </style>12 13 </head>14 <body>15 <form>16 <select>17 <option>1</option>18 <option>2</option>19 <option>3</option>20 </select>21 </form>22 </body>23 </html>
Three empty pseudo-selectors
This selector is a bit interesting, it is called "content" in jquery "Selector" is used to find empty elements. If you play with jquery's empty, there is no problem with this.
Here is an example to change the background of the first empty p.
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <title></title> 6 7 <style type="text/css"> 8 9 p:first-child{10 width:500px;11 height:20px;12 }13 14 p:empty {15 background:red;16 }17 </style>18 19 </head>20 <body>21 <p></p>22 <p>他好</p>23 </body>24 </html>
Four: not(xxx) pseudo-selector
This is also a very classic not selector, called in jquery "Basic selector", do you remember it? ? ?
In general, when you read the above, do you feel that some "script processing behavior" has been integrated into CSS3? This feeling is that CSS is no longer the CSS you once knew.