search
HomeWeb Front-endHTML TutorialCSS Journey--The Third Stop Powerful Pseudo-Selector_html/css_WEB-ITnose

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.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft