Table of Contents
Preface
Pseudo-class
HTMLAnchorElement
Home Web Front-end CSS Tutorial In-depth understanding of CSS pseudo-class selectors (code examples)

In-depth understanding of CSS pseudo-class selectors (code examples)

Aug 29, 2018 am 11:18 AM
css

This article brings you knowledge about in-depth understanding of CSS 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 such as :link, ::after and content , Pseudo-element selector. I found something lacking in this aspect when I was reading a book recently, so I decided to study it a little more in-depth. 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, they are :link, :visited, :hover, :active, :focus, :focus-within, :target, :root, and :checked. The four classic pseudo-classes of

HTMLAnchorElement

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

  2. :visited, used to set the style after the link is clicked;

  3. :hover, used to set the style of the link when the mouse is hovering over the link;

  4. :active, used to set the style of the link when the mouse button is pressed but not released , the style of the link.

I think you are all like me. The first thing you came into contact with were the above four pseudo-categories, 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 the page now has the element

Target

, then as long as #title is entered in the address bar, the browser will continue to scroll (scrolling will not stop) There must be a tween animation) until the element h3#title is located at a specific position in the visual area. (Note: Please do not confuse it with UI Routing) The above-mentioned positioned page resource is called
target element or currently 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.}
Copy after login
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:

  1. Mouse click;

  2. Tab key;

  3. Pass JavaScript's

    HTMLElement.prototype.focus() method.

Then 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]
Copy after login
Note: If the

tabindex attribute value is less than 0, the 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
Copy after login

There is also a misleading attribute

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

  1. :root, used for setting The style of the element is supported starting from IE9.

  2. :checked, used to set the selected style of radio and check controls, supported starting from IE9. Combined with the pseudo-element ::before and content attributes, flexible and efficient custom radio and check controls can be realized.

  3. :empty, used to set the style of elements without child nodes. p{ } is an element with TEXT_NODE child node, while p{} is an element with no child node.

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

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

Related recommendations:

CSS pseudo-class selector

About CSS pseudo-class selection Device_html/css_WEB-ITnose

The above is the detailed content of In-depth understanding of CSS pseudo-class selectors (code examples). 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 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 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.

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

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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-

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

See all articles