


What is a pseudo-class selector in css? A brief introduction to pseudo-class selectors
This chapter will tell you what is a pseudo-class selector in CSS? A brief introduction to pseudo-class selectors. Let everyone understand the role of pseudo-class selectors in css, what are the classifications of css pseudo-class selectors, and other knowledge. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. (Related recommendations: "CSS Tutorial")
##1. Introduction to pseudo-class selectors
Pseudo-class selector (referred to as: pseudo-class) is defined by a colon, which defines the state of the element, such as click pressed, click completed, etc. The style can be modified for the state of the element through pseudo-classes. The function of the pseudo-class is similar to the element style in the general DOM, but unlike the element style in the general DOM, it does not change any DOM content. It just inserts some modified elements, which are visible to the user but not to the DOM. The effect of a pseudo class can be achieved by adding an actual class.What is a pseudo-class?It means that the css built-in class css itself gives it some features and functions, that is, you don’t need class=... or id=... You can use it directly. Of course, you can also change some of its attributes, such as: a:link{color:#FF0000;}
2. Pseudo-class selector The classification of
Pseudo-class selectors can be mainly divided into: dynamic pseudo-class selectors, UI element state pseudo-class selectors, structural pseudo-class selectors, and negative pseudo-class selectors. The selector syntax of these categories is introduced in detail below:1. Dynamic pseudo-class selector syntax
- E: link (link pseudo-class selector): Selects the matching E element, and the matching element is defined as a hyperlink and has not been visited. Commonly used on link description points
- E:visited (link pseudo-class selector): Selects the matching E element, and the matching element is defined with a hyperlink and has been visited. Commonly used on link drawing points
- E:active (user behavior selector): Select the matching E element, and the matching element is activated. Commonly used on link strokes and buttons
- E:hover (user behavior selector): Select the matching E element, and the user's mouse stays on the element E. IE6 and below browsers only support a:hover
- E:focus (user behavior selector): Select the matching E element, and the matching element gets focus
- :link "Link": Before the hyperlink was clicked
- :visited "Visited": After the link was visited
- :hover "Hover": When the mouse is placed on the label
- :active "Activate": When the mouse clicks on the label but does not let go.
/*让超链接点击之前是红色*/ a:link {color: red;} /*让超链接点击之后是橙色*/ a:visited {color: orange;} /*鼠标悬停,放到标签上的时候是绿色*/ a:hover {color: green;} /*鼠标点击链接,但是不松手的时候*/ a:active {color: black;}
2.UI elements Status pseudo-class selector
- E:checked (selected status pseudo-class selector): Matches the selected check button or radio button form element
- E:enabled (enabled status pseudo-class selector): matches all enabled form elements
- E:disabled (unavailable status pseudo-class selector): matches All disabled form elements
input[type="text"]:disabled {border:1px solid #999;background-color: #fefefe;}
3. Structural pseudo-class selector
- E: fisrt-child: The element E that is the first child element of the parent element. Equivalent to E:nth-child(1)
E:last-child :作为父元素的最后一个子元素的元素E。与E:nth-last-child(1)等同
E:root:选择匹配元素E所在文档的根元素。在HTML文档中,根元素始终是html,此时该选择器与html类型选择器匹配的内容相同
E F:nth-child(n):选择父元素E的第n个子元素F。其中n可以是整数(1,2,3)、关键字(even,odd)、可以是公式(2n+1),而且n值起始值为1,而不是0.
E F:nth-last-child(n):选择父元素E的倒数第n个子元素F。此选择器与E:nth-child(n)选择器计算顺序刚好相反,但使用方法都是一样的,其中:nth-last-child(1)始终匹配最后一个元素,与last-child等同
E:nth-of-type(n) :选择父元素内具有指定类型的第n个E元素
E:nth-last-of-type(n):选择父元素内具有指定类型的倒数第n个E元素
E:first-of-type:选择父元素内具有指定类型的第一个E元素,与E:nth-of-type(1)等同
E:last-of-tye :选择父元素内具有指定类型的最后一个E元素,与E:nth-last-of-type(1)等同
E:only-child :选择父元素只包含一个子元素,且该子元素匹配E元素
E:only-of-type:选择父元素只包含一个同类型子元素,且该子元素匹配E元素
E:empty: 选择没有子元素的元素,而且该元素也不包含任何文本节点
结构伪类选择器,可以根据元素在文档中所处的位置,来动态选择元素,从而减少HTML文档对ID或类的依赖,有助于保持代码干净整洁。
结构伪类选择器很容易遭到误解,需要特别强调。如:
p:first-child;
它表示的是:选择父元素下的第一个子元素 p,而不是选择 p 元素的第一个子元素。
注意:
结构伪类选择器中,子元素的序号是从 1 开始的,也就是说,第一个子元素的序号是 1,而不是 0。换句话说,当参数 n 的计算结果为 0 时,将不选择任何元素。
4.否定伪类选择器
E:not(F):匹配所有除元素F外的E元素
例:对form中所有input加边框,但又不想submit也起变化,就可以这样写:
input:not([type="submit"]) {border: 1px solid red;}
The above is the detailed content of What is a pseudo-class selector in css? A brief introduction to pseudo-class selectors. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

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.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.
