Home Web Front-end HTML Tutorial CSS Journey--The Third Stop Powerful Pseudo-Selector_html/css_WEB-ITnose

CSS Journey--The Third Stop Powerful Pseudo-Selector_html/css_WEB-ITnose

Jun 24, 2016 am 11:45 AM

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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 of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

See all articles