Home>Article>Web Front-end> 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 selectoris 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 selectorThe 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 selectoris:pseudo-class
, Don’t forget:
here.
CSS provides a variety of pseudo-classes, as shown in the following table:
element in the parent element
element in the element
Classification of pseudo-class selectors
The CSS version has evolved from the first version to the third version, providingpseudo-class selectors## The number of # is already huge. In particular, the CSS3 version has added a large number ofpseudo-class selectors.
There are so many pseudo-class selectors. In order to better sort out thepseudo-class selectors, we can divide them into the following 5 types according to their uses:
:hover, press
:activeAnd get focus
:focusetc.
:not( ), it means it is not a certain element.
1. User behavior pseudo-class - dynamic pseudo-class selector
Tips: The visited selector matches any link whose href attribute is a URL that the user has visited on all pages, not just your page. The most common use of :visited is to apply a certain style to visited links to distinguish them from unvisited links. For example, when we watch the news, the status of the news that we have read and the news that we have not read are different in the homepage list, which makes it easier for us to distinguish.
2. Structural pseudo-class selector
Structure pseudo-class selector The content contained in the class selector is 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 element |
:empty | p:empty | Matches any element that has no child elements |
:enabled | input:enabled | Matches every enabled 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 element | |
a:hover | Match the element on which the mouse is hovering | |
input:in-range | Match elements with the specified value range element | |
input:invalid | Matches all 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 elements without the "required" attribute | :out-of-range | |
Matches elements with values outside the specified range | :read-only | |
Matches elements that specify the "readonly" attribute | :read-write | |
Matches elements without the "readonly" attribute | :required | |
Matches 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 element with a valid value | :visited | |
matches all visited links | The reason why it is called dynamic pseudo-class Selectors, because they match elements based on changing conditions, are relative to the fixed state of the document. As JavaScript is widely used to modify document content and element state, the boundaries between dynamic selectors and static selectors are becoming increasingly blurred. However, dynamic pseudo-class selectors are still a special type of selector.: The link selector matches hyperlinks, and the :visited selector matches hyperlinks that the user has visited.Usage: There are not many attributes that the visited selector can be applied to link elements. You can change the colors and fonts, but that's about it.: The hover selector matches any element that the user mouse is hovering over.: The active selector matches the element currently activated by the user (usually the element is clicked by the mouse).: The focus selector matches the element that has received focus, and is often used for input elements.Use the structural pseudo-class selector to select elements based on their position in the document. This type of selector is prefixed by a colon character (:), for example: empty. They can be used alone or in combination with other selectors, such as p:empty. |
CSS中的结构伪类选择器是根据HTML页面中元素之间的关系来定位HTML元素,从而减少对HTML元素的id
属性和class
属性的依赖。
:first-child与:last-child
:first-child
伪类用来定义一组兄弟元素的第一个元素而:last-child
伪类则是定位一组兄弟元素的最后一个元素。
如下示例代码展示了:first-child
伪类和:last-child
伪类的用法:
HTML结构如下:
CSS代码如下:
li:first-child { color: red; } li:last-child { color: blue; }
代码运行结果如下图所示:
:first-child
伪类可以使用:nth-child(n)
伪类改写为:nth-child(1)
,而:last-child
伪类可以使用:nth-last-child(n)
伪类改写为:nth-last-child(1)
。
:first-child
伪类和:last-child
伪类经常会引起误解。例如li:first-child
是用来定位所有元素中第一个作为子级元素的,而不是定位
元素的第一个子级元素。
:first-of-type与:last-of-type
:first-of-type
伪类和:last-of-type
伪类一个用于定位一组元素中的第一个兄弟元素,一个用来定位最后一个。
如下示例代码展示了:first-of-type
伪类和:last-of-type
伪类的用法:
HTML结构如下:
狐妖小红娘
涂山红红
涂山苏苏
CSS代码如下:
p:first-of-type { color: red; } p:last-of-type { color: blue; }
代码运行结果如下图所示:
:first-of-type
伪类与:last-of-type
伪类的用法一定要和:first-child
伪类与:last-child
伪类的用法区分开。以:first-of-type
伪类和:first-child
伪类为例来说明:
:first-of-type
伪类是定位一组同类型的兄弟元素中的第一个元素,不管这个元素在兄弟元素中的位置如何。
:first-child
伪类是定位一组兄弟元素中的第一个元素,这些兄弟元素不一定是同类型的。
如果将上述示例代码中的:first-of-type
伪类改写为:first-child
伪类的话,将不会生效。
:nth-child(n)与:nth-last-child(n)
:nth-child(n)
伪类和:nth-last-child(n)
伪类都是CSS3中新增的选择器,这两个选择器的用法基本上是一致的。区别在于:nth-last-child(n)
伪类是倒序方式定位元素,也就是说,:nth-last-child(n)
伪类是从一组元素的结尾开始的。
接下来,主要以:nth-child(n)
伪类为例进行讲解。:nth-child(n)
伪类中的n
参数的含义具有3种情况:
数字值:任意一个大于 0 的正整数。例如#example td:nth-child(1)
表示定位ID为example
的父元素下所有
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 (
) |
|||||||||||||||||||||||||
元素中的第一个元素。 关键字: 格式为 如下示例代码展示了 nbsp;html>
代码运行结果如下图所示:
:empty
如下示例代码展示了 nbsp;html> 代码运行结果如下图所示: :root CSS中的 如下代码展示的 :root { height: 100vh; width: 100vw; background-color: dodgerblue; } 代码运行结果如下图所示: 3、UI元素状态伪类选择器 使用UI伪类选择器可以根据元素的状态匹配元素,下方列表将简单总结这类选择器:
4、输入伪类选择器 关于表单输入的伪类,主要介绍三种常用的,具体如下:
:enabled和:disabled
如下代码展示了 nbsp;html> 代码运行结果如下所示: 由上图我们看到禁用状态的 :read-only和:read-write
nbsp;html> 代码运行结果如下所示: 我们可以看到,只读的 :checked
nbsp;html> 关于 示例代码如下: nbsp;html> 运行效果如下所示: 5、逻辑组合伪类
: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!