search
HomeWeb Front-endCSS TutorialIn-depth understanding of the usage of CSS pseudo-class selectors (code examples)

This article brings you an in-depth understanding of the usage of pseudo-class selectors (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Preface

In the past, I have sporadically understood and used pseudo-classes and pseudo-element selectors such as :link, ::after, and content. Recently, I found something lacking in this aspect while reading a book, so I decided to Let’s study a little more deeply. The following is a summary of the pseudo-class part.

Pseudo-class

Pseudo-class selectors essentially allow designers to set different visual effects based on the specific state of the element. Specifically: link, :visited, :hover, :active, :focus, :focus-within, :target, :root and :checked. The four classic pseudo-classes of

HTMLAnchorElement

:link, used to set the style of the initial link state;

:visited, Used to set the style of the link after it is clicked;

:hover, used to set the style of the link when the mouse hovers over the link;

:active, used to set the mouse button press , but not released when the linked style.

I believe you are all like me. The first thing you came into contact with were the above four pseudo-classes, right? ! And I have to memorize the setting sequence (LVAH) by heart, haha.

Set the current target element style

Do you remember the pound sign in the URL? Starting from the pound sign (#) to the end of the URL is called the hash or fragment of the URL, which is used to locate a certain resource within the page. Assume that there is an element of

Target

on the page now. As long as #title is entered in the address bar, the browser will continue to scroll (scrolling may not necessarily have tweening effects) until the element h3#title Located at a specific location in the viewport. (Note: Please do not confuse it with UI Routing)
The above-mentioned positioned page resource is called the target element or the current active element! Its style can be set via :target.
Compatibility: Supported by IE9.

Example:

// 当前URL为http://foo.com#1
:target {
    color: red;
}
.title{
    color: blue;
    
    &:target{
        border: solid 1px red;
    }
}

.title{I'm not target element.}
.title#1{Yes, I'm.}

Set the style when the element gets focus

: focus is used to set the style when the element is in focus.
Compatibility: IE8 starts to support.
So which elements support the focus state? Then you need to first figure out what operations can be used to achieve focus.
They are:

Mouse click;

Tab key;

Through JavaScript's HTMLElement.prototype.focus() method.

The elements that traditionally support the focus state must be a, button, input, select, and textareas.
In HTML5, when the element is set with the contenteditable or tabindex attribute, the element supports the focus state.
That is, elements that match the following selectors support the focus state.

a,button,input,select,textarea,[contenteditable],[tabindex]

Note: If the tabindex attribute value is less than 0, focus cannot be obtained through the Tab key. But the element can gain focus via mouse click or script.

JS gets the element that is currently focused

/* 
 * 加载完成时默认返回body
 * 若某元素获得焦点时,则返回该元素
 */
document.activeElement :: HTMLElement

There is also a misleading attribute

// 用于检测文档是否得到焦点,即用户是否正在与页面交互
// 页面仅仅位于屏幕可视区域,而用户没有与之交互时返回false。
document.hasFocus :: Void -> Boolean

Set the style of the element when the child element gets focus

:focus-within, used to set the style of the element when the child element is in focus.
Compatibility: Chrome63 starts to support.

Example: When confirming the password for the second time, the password box is highlighted

.form-control{
  &:focus-within > input{
    &:focus {
      border: solid 1px skyblue;
    }
    
    &:not(:focus){
      border: solid 1px orange;
    }
  }
}

.form-control>input.pwd[type=password]+input.confirm-pwd[type=password]

Others

:root, used to set the style of the element, from IE9 starts to support.

:checked, used to set the selected style of radio and check controls, supported starting from IE9. Combining pseudo-element ::before and content attributes can realize flexible and efficient customized radio and check controls.

:empty, used to style elements that have no child nodes. p{} is an element with TEXT_NODE child node, while p{} is an element with no child node.

:not, as a predicate expressing the semantics of negation.

:placeholder-shown, used to set the style when the element placeholder is displayed.

The above is the detailed content of In-depth understanding of the usage of CSS pseudo-class selectors (code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
css中hover什么意思css中hover什么意思Feb 22, 2024 pm 01:24 PM

CSS中的:hover是一种伪类选择器,用于在用户悬停在特定元素上时,应用特定的样式。当鼠标悬停在元素上时,可以通过:hover为其添加不同的样式,从而增强用户体验和交互效果。本文将详细讨论:hover的含义以及给出具体的代码示例。首先,让我们了解一下CSS中:hover的基本用法。在CSS中,可以通过选择器来选中要应用:hover效果的元素,并在其后面加上

css中的li标签怎么去除前面的圆点css中的li标签怎么去除前面的圆点Apr 28, 2024 pm 12:36 PM

CSS中去除li标签圆点的方法有两种:1.使用"list-style-type: none;"样式;2.使用透明图片和"list-style-image: url("transparent.png");"样式。两种方法都能删除所有li标签的圆点,如果您只想删除某些li标签的圆点,可以使用伪类选择器。

如何使用:nth-child(-n+5)伪类选择器选择位置小于等于5的子元素的CSS样式如何使用:nth-child(-n+5)伪类选择器选择位置小于等于5的子元素的CSS样式Nov 20, 2023 am 11:52 AM

如何使用:nth-child(-n+5)伪类选择器选择位置小于等于5的子元素的CSS样式在CSS中,伪类选择器是一种强大的工具,可以通过特定的选择方式来选取HTML文档中的某些元素。其中,:nth-child()是一种常用的伪类选择器,可以选择特定位置的子元素。:nth-child(n)可以匹配HTML中的第n个子元素,而:nth-child(-n)可以匹配

css中::什么意思css中::什么意思Apr 28, 2024 pm 03:45 PM

CSS 中的 :: 伪类选择器用于指定元素的特殊状态或行为,并且比伪类选择器 : 更具体,可针对元素的特定属性或状态进行选择。

使用CSS中的content属性使用CSS中的content属性Feb 19, 2024 am 10:56 AM

CSS中content属性的用法CSS中的content属性是一个非常有用的属性,它是用来在伪类中插入额外的内容的。content属性一般只能在伪类选择器(如::before和::after)中使用,它可以用来插入文本或者图片等内容。我们可以通过content属性实现一些非常炫酷的效果。下面是content属性的一些用法以及具体的代码示例:插入文本内容通过

html中的hover的作用html中的hover的作用Feb 20, 2024 am 08:58 AM

HTML中的hover的作用及具体代码示例在Web开发中,hover(悬停)是指当用户将光标悬停在一个元素上时,触发一些动作或效果。它是通过CSS的:hover伪类来实现的。在本文中,我们将介绍hover的作用以及具体的代码示例。首先,hover使元素在用户悬停时可以改变其样式。比如,将鼠标悬停在一个按钮上时,可以改变按钮的背景颜色或者文字颜色,以提示用户当

css中hover怎么使用css中hover怎么使用Feb 23, 2024 pm 12:06 PM

CSS中的hover伪类是一个非常常用的选择器,它允许我们在鼠标悬停在元素上时改变其样式。本文将为大家介绍hover的用法,并提供具体的代码示例。一、基本用法要使用hover,我们需要先为该元素定义一个样式,然后使用:hover伪类来制定鼠标悬停时对应的样式。例如,我们有一个button元素,当鼠标悬停在按钮上时,我们希望按钮的背景色变为红色,文字颜色变为白

使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式Nov 20, 2023 am 11:22 AM

使用:nth-last-child(2)伪类选择器选择倒数第二个子元素的样式,需要具体代码示例在CSS中,伪类选择器是一种非常强大的工具,可以用来选择文档树中特定的元素。其中之一就是:nth-last-child(2)伪类选择器,它可以选择倒数第二个子元素并对其应用样式。首先,让我们来创建一个示例HTML文档,以便我们可以在其中使用这个伪类选择器。以

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.