Home Web Front-end CSS Tutorial What is a pseudo-class selector in css? A brief introduction to pseudo-class selectors

What is a pseudo-class selector in css? A brief introduction to pseudo-class selectors

Sep 17, 2018 pm 02:31 PM
css Pseudo class

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")

What is a pseudo-class selector in css? A brief introduction to pseudo-class selectors

##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

Dynamic pseudo-class selector can be used in the application of hyperlink a tag:

a tag has 4 pseudo-classes (corresponding to four states), as follows:

  • :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.

Code example: There is a certain order for these four pseudo-class selectors of the

/*让超链接点击之前是红色*/
a:link {color: red;}

/*让超链接点击之后是橙色*/
a:visited {color: orange;}

/*鼠标悬停,放到标签上的时候是绿色*/
a:hover {color: green;}

/*鼠标点击链接,但是不松手的时候*/
a:active {color: black;}
Copy after login

a tag. The order between each style is very particular. Once it appears, Arrangement errors are likely to cause overwriting, causing one of the styles to be unable to be displayed. So how do these four pseudo-class selectors of the a tag need to be sorted before they can be used?

In the CSS definition, a:hover must be placed after a:link and a:visited to be effective, and a:active must be placed after a:hover to be effective.

So, the order of the four pseudo-class selectors of the a tag is: a:link, a:visited, a:hover, a:active

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

The UI element status pseudo-class selector is mainly used to operate on the Form element in HTML. The most common example is that our "type="text" has enable and There are two states of disabled, the former is writable and the latter is unavailable; in addition, "type="radio" and "type="checkbox"" have two states: "checked" and "unchecked". Let's look at two examples. For example, if you want to distinguish the "disabled" text box from other text boxes, you can use it like this:

input[type="text"]:disabled {border:1px solid #999;background-color: #fefefe;}
Copy after login

Note: IE6-8 does not support ":checked", ":enabled", ":disabled" are the three selectors.

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

它表示的是:选择父元素下的第一个子元素 p,而不是选择 p 元素的第一个子元素。

注意:

结构伪类选择器中,子元素的序号是从 1 开始的,也就是说,第一个子元素的序号是 1,而不是 0。换句话说,当参数 n 的计算结果为 0 时,将不选择任何元素。

4.否定伪类选择器

E:not(F):匹配所有除元素F外的E元素

例:对form中所有input加边框,但又不想submit也起变化,就可以这样写:

input:not([type="submit"]) {border: 1px solid red;}
Copy after login

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!

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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

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 bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

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.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

See all articles