Home > Web Front-end > CSS Tutorial > In-depth understanding of the usage of CSS pseudo-class selectors (code examples)

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

不言
Release: 2018-09-19 15:57:33
Original
2082 people have browsed it

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

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

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

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template