css pseudo-selector learning pseudo-class selector analysis
In the previous article "css pseudo-selector learning: Pseudo-element selector analysis", we learned about pseudo-element selectors, and today we learn more about pseudo-class selectors. I hope it will be useful to everyone. Helps!
Pseudo-class selector
Pseudo-class selector is a type of selector that allows passing code that is not included in HTML Element state information to locate the usage of HTML elements. Pseudo-class selector The specific usage is to add keywords to the existing selector to represent the status information of the positioned HTML element. [Recommended learning: css video tutorial]
Through pseudo-classes, developers can set the dynamic state of elements, such as hover (hover), click (active), and other elements that cannot be passed in the document. The elements selected by the selector (these elements have no ID or class attributes), such as the first child element (first-child) or the last child element (last-child).
For example:hover
The pseudo-class selector can be used to change the color of the button when the user hovers the mouse over the button. As shown in the following sample code:
/* 所有用户指针悬停的按钮 */ button:hover { color: blue; }
The name of the pseudo-class is not case-sensitive, but needs to start with a colon :
. In addition, pseudo-classes need to be used in conjunction with selectors in CSS. The syntax format is as follows:
选择器:伪类 { 属性 : 属性值; }
The specific syntax format of pseudo-class selector is :pseudo-class
, Don’t forget :
here.
CSS provides a variety of pseudo-classes, as shown in the following table:
Selector | Example | Example description | |||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
:active | a :active | match clicked link | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:checked | input:checked | match the selected | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:disabled | input:disabled | matches every disabled <input> element | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:empty | p:empty | Matches any element that has no child elements |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
:enabled | input:enabled | Matches every enabled <input> element | |||||||||||||||||||||||||||||||||||||||||||||||||||||
:first-child | p:first-child | Matches the first child element in the parent element, must be the first child element in the parent element |
|||||||||||||||||||||||||||||||||||||||||||||||||||||
p:first-of-type | Matches the first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:focus | Matches the focused <input> element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
a:hover | Match the element on which the mouse is hovering | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:in-range | Match elements with the specified value range <input> element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
input:invalid | Matches all <input> elements | ## that have an invalid value | |||||||||||||||||||||||||||||||||||||||||||||||||||||
p:lang(it) | Match every element whose lang attribute value starts with "it" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:last-child | Matches the last child element in the parent element, must be The last child element in the parent element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:last-of-type | matches the The last element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
a:link | matches all unvisited links | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
:not(p) | Match every element that is not a element |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-child(2) | Matches the second child element in the parent element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-last-child(2) | Matches the penultimate child element of the parent element | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
p:nth-last-of-type(2) | Matches the penultimate element of the parent element Child element | ##:nth-of-type(n) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
match The second child element of the parent element | :only-of-type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the parent The only | :only-child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the only child element in the parent element | :optional | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements without the "required" attribute | :out-of-range | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements with values outside the specified range | :read-only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements that specify the "readonly" attribute | :read-write | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements without the "readonly" attribute | :required | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches <input> elements that specify the "required" attribute | :root | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches the root element of the element. In HTML, the root element is always HTML | :target | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches the currently active #news element (clicked on a URL containing this anchor name) | :valid | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Matches all An <input> element with a valid value | :visited | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
matches all visited links |
Function | |
---|---|
selector:first-child
| is used to locate a group of sibling elements The first element of |
selector:last-child
| is used to locate the last element in a group of sibling elements|
selector:nth-child(n)
| Used to locate the nth element in a group of sibling elements|
selector:nth-last-child(n)
| Used to locate the nth element in a group of sibling elements in reverse order|
selector:first-of-type
| Used to locate the first element in a group of sibling elements of the same type|
selector:last-of-type
| Used to locate the last element in a group of sibling elements of the same type|
selector: nth-of-type(n)
| Used to locate the nth element in a group of sibling elements of the same type|
selector: nth-last-of-type(n)
| Used to locate the nth element in reverse order among a group of sibling elements of the same type|
selector:only-child
| Used to locate an element without any sibling elements|
selector:only-of-type
| Used to locate an element that does not have any sibling elements of the same type |
selector:empty
| Used to locate an element An element that has no child elements and does not have any text content|
selector:root
| is used to locate the root of the HTML page Element () |
元素中的第一个元素。<li> 关键字: 格式为 如下示例代码展示了 nbsp;html> <meta> <meta> <meta> <title>nth-child伪类</title> <style> table { border-collapse: collapse; border-spacing: 0; width: 100%; } th, td { border-top: 1px solid lightcoral; text-align: center; } /* 最后一行单元格在底部增加一个边框效果 */ tr:last-child td { border-bottom: 1px solid lightcoral; } /* 实现隔行换色 */ tr:nth-child(even) { background-color: aquamarine; } </style>
代码运行结果如下图所示:
:empty
如下示例代码展示了 nbsp;html> <meta> <meta> <meta> <title>empty伪类</title> <style> body { /* 开启flex布局 */ display: flex; } .box { background: pink; height: 80px; width: 80px; margin: 0 20px; } .box:empty { background: lime; } </style> <div></div> <div>这个元素的背景是粉色的</div> <div> <!-- 这是一个注释 --> </div> 代码运行结果如下图所示: :root CSS中的 如下代码展示的 :root { height: 100vh; width: 100vw; background-color: dodgerblue; } 代码运行结果如下图所示: 3、UI元素状态伪类选择器 使用UI伪类选择器可以根据元素的状态匹配元素,下方列表将简单总结这类选择器:
4、输入伪类选择器 关于表单输入的伪类,主要介绍三种常用的,具体如下:
:enabled和:disabled
如下代码展示了 nbsp;html> <meta> <meta> <meta> <title>:enabled和:disabled的用法</title> <style> input:enabled { outline: none; } input:disabled { /* 禁用状态背景为灰色 */ background-color: gray; } </style> <input> <input> 代码运行结果如下所示: 由上图我们看到禁用状态的 :read-only和:read-write
nbsp;html> <meta> <meta> <meta> <title>:read-only和:read-write</title> <style> input:read-write { outline: none; } /* 只读状态 */ input:read-only { color: red; outline: none; } </style> <input> <input> 代码运行结果如下所示: 我们可以看到,只读的 :checked
nbsp;html> <meta> <meta> <meta> <title>checked伪类</title> <style> input:checked { /* 为选中的增加阴影 */ box-shadow: 2px 2px 2px 2px lightcoral; } </style> <input> <input> 关于 示例代码如下: nbsp;html> <meta> <meta> <meta> <title>开关</title> <style> [type="checkbox"] { width: 44px; height: 26px; position: absolute; opacity: 0; pointer-events: none; } /* 开关样式 */ .cs-switch { display: inline-block; width: 44px; height: 26px; border: 2px solid; border-radius: 26px; background-color: currentColor; box-sizing: border-box; color: silver; transition: all .2s; cursor: pointer; } .cs-switch::before { content: ""; display: block; width: 22px; height: 22px; border-radius: 50%; background-color: #fff; transition: margin-left .2s; } :checked+.cs-switch { color: blueviolet; } /* 选中移动 */ :checked+.cs-switch::before { margin-left: 18px; } /* 禁用状态 */ :disabled+.cs-switch { opacity: .4; cursor: not-allowed; } </style> <!-- 普通状态 --> <input> <label></label> <!-- 选中状态 --> <input> <label></label> <!-- 禁用状态 --> <input> <label></label> <!-- 选中禁用状态 --> <input> <label></label> 运行效果如下所示: 5、逻辑组合伪类
优先级为0,优先级由括号中的表达式决定; 可以不断的级联; 不可出现多个表达式,也不支持选择符; :not()的巨大的用处在于告别重置的问题; 重置web中的样式,就好比我们在项目中经常使用到的:添加 .cs_li { display: none; } .cs_li.active { display: block; } 而我们可以使用 .cs_li:not(.active) { display: none; } 在列表中的设置 .cs_li:not(:nth-of-type(5n)){ margin-right: 10px; // 除5的倍数项都设置右边的外边距 }
平时我们开发中经常会用到类似下面的语法: .cs_li_a > img, .cs_li_b > img, .cs_li_c > img { display: none; } 使用 :is(.cs_li_a, .cs_li_b, .cs_li_c) > img { display: none; } 还有一种嵌套之间的关系,相互嵌套,交叉组合得出结论;如下方所示 ol ol li, ol ul li, ul ul li, ul ol li { margin-left: 20px; } 使用 :is(ol, ul) :is(ol, ul) li{ margin-left: 20px; }
使用的方法与:is()完全相同,但优先级永远是0;底下的括号中的优先级完全被忽略,俩句是同一个优先级,并且级别等同于 :where(.article, section) .conten {} :where(#article, #section) .conten { (学习视频分享:web前端入门) |
The above is the detailed content of css pseudo-selector learning pseudo-class selector analysis. For more information, please follow other related articles on the PHP Chinese website!

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.