


In-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!

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

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



:hover in CSS is a pseudo-class selector used to apply specific styles when the user hovers over a specific element. When the mouse hovers over an element, you can add different styles to it through :hover to enhance user experience and interaction. This article will discuss in detail: the meaning of hover and give specific code examples. First, let us understand the basic usage of :hover in CSS. In CSS, you can use a selector to select the element to which the :hover effect is to be applied, and add after it

There are two ways to remove dots from li tags in CSS: 1. Use the "list-style-type: none;" style; 2. Use transparent images and "list-style-image: url("transparent.png"); "style. Both methods can remove the dots of all li tags. If you only want to remove the dots of certain li tags, you can use a pseudo-class selector.

The role of hover in HTML and specific code examples In web development, hover refers to triggering some actions or effects when the user hovers the cursor over an element. It is implemented through the CSS :hover pseudo-class. In this article, we will introduce the role of hover and specific code examples. First, hover enables an element to change its style when the user hovers over it. For example, when hovering the mouse over a button, you can change the button's background color or text color to remind the user what to do next.

The :: pseudo-class selector in CSS is used to specify a special state or behavior of an element, and is more specific than the pseudo-class selector : and can select specific attributes or states of an element.

How to use:nth-child(-n+5) pseudo-class selector to select the CSS style of child elements whose position is less than or equal to 5. In CSS, the pseudo-class selector is a powerful tool that can be selected through a specific selection method. Certain elements in an HTML document. Among them, :nth-child() is a commonly used pseudo-class selector that can select child elements at specific positions. :nth-child(n) can match the nth child element in HTML, and :nth-child(-n) can match

Usage of content attribute in CSS The content attribute in CSS is a very useful attribute, which is used to insert additional content in pseudo classes. The content attribute can generally only be used in pseudo-class selectors (such as ::before and ::after). It can be used to insert content such as text or images. We can achieve some very cool effects through the content attribute. The following are some uses of the content attribute and specific code examples: Insert text content through

The hover pseudo-class in CSS is a very commonly used selector that allows us to change the style of an element when the mouse is hovering over it. This article will introduce the usage of hover and provide specific code examples. 1. Basic Usage To use hover, we need to first define a style for the element, and then use the :hover pseudo-class to specify the corresponding style when the mouse is hovering. For example, we have a button element. When the mouse hovers over the button, we want the background color of the button to change to red and the text color to white.

Use the :nth-last-child(2) pseudo-class selector to select the style of the penultimate child element. Specific code examples are required. In CSS, the pseudo-class selector is a very powerful tool that can be used to select the document tree. specific elements. One of them is the :nth-last-child(2) pseudo-class selector, which selects the second-to-last child element and applies styles to it. First, let's create a sample HTML document so that we can use this pseudo-class selector in it. by
