Home Web Front-end CSS Tutorial Explanation of the specific usage of CSS :focus-within

Explanation of the specific usage of CSS :focus-within

Sep 20, 2017 am 09:48 AM
Instructions

This article mainly introduces relevant information on the specific use of CSS :focus-within, which has certain reference value. Interested friends can refer to it

The world of CSS is really a magical world . Many front-end developers may have heard of :focus but not :focus-within. So what the hell is :focus-within? In this article, we will talk about :focus-within.

:What is focus-within

In CSS: focus-within is a pseudo-class, which has now been included in the CSS selector (CSS Level 4 selector). It's like you know :focus or :hover . :focus-within is very convenient for handling the focus state. Elements with the :focus-within pseudo-class are effective when the element itself or one of its descendants has focus.

If the text is difficult to understand, let's look at a simple example first:


<p class="form-group">
    <label for="email">
        Your email:
    </label>
    <input type="email" name="email" id="email" placeholder="Enter your email address"/>
</p>
Copy after login

The above is a simple form with an input form control.


.form-group *:focus {
    background: yellow;
    color: #000;
}
.form-group {
    padding: 10px;
    border: 3px solid transparent
}

.form-group:focus-within {
    border-color: #000;
}
Copy after login

In the above example, when .form-group gets focus, the border-color color is changed to #000. But this does not mean receiving keyboard focus, that is because

does not necessarily get keyboard focus and tabindex is not set. Or if the link or input element in p receives focus, then the .form-group element will change the border color.

Seeing the above example, don’t you think it’s amazing? To achieve similar effects in the past, we needed to use JavaScript scripts. Through the mouse or keyboard events of the descendant elements of .form-group, and then add the corresponding style to its parent element (or ancestor element).

:Usage scenarios of focus-within

:focus-within is very powerful, mainly because the pseudo-class will be activated when any of its elements obtains focus. . Use this pseudo-class with caution when the element contains many subcomponents.

With :focus-within, some common interactive behaviors have become very simple, especially the interactive behaviors that previously required JavaScript keyboard events, we can use :focus-within instead.

Next, let’s look at some common examples of :focus-within.

Highlighting of table rows

A common example is the behavior of table row highlighting, that is, when the mouse is hovering over the rows of the table, the color becomes highlighted . This interactive style can help visually impaired users better read a complex table or long form. Because of the highlighting, these users can more easily keep track of the table row they are currently reading. The way that was often implemented in the past was to use :hover to change the style of table rows. This solution is an easy task for normal users, but if your users have difficulty operating the mouse, then :hover It's a bit painful. If you're looking for perfection, you might add styles to table rows via JavaScript keyboard events.

So with :focus-within, we can say goodbye to JavaScript. We can style it like this:

The above example shows how to use :focus-within to highlight an entire row of a table. If there is an element available that can get focus in a particular table row, this refers to getting focus via the keyboard. For example, in the above example, there is an element in the table. You know, the a element can get focus through the keyboard.

At this time you only need to add in the style:


Drop-down menu

The drop-down menu is ours One of the most common web components. When I first saw the :focus-within pseudo-class, the first use case that came to mind was drop-down menus.

In the above example, JavaScript is used to track the user's keyboard focus in a navigation drop-down menu. If JavaScript detects that keyboard focus is on a link, add a .has-focus class to .nav__list_drop. When li has the .has-focus class, its child elements will be displayed, that is, the drop-down menu will be displayed.

For this effect, we can directly replace the function of JavaScript script through :focus-within. Use .nav__list__drop:focus-within instead of .nav__list__drop.has-focus . Drop-down menus appear when you manipulate the keyboard to give focus to a drop-down menu item.


Off-screen Nav

Off-screen Nav is a common effect on mobile terminals. Then we can also achieve this effect through :focus-within. For detailed code, you can see the following example:

Browser Compatibility

: focus-within Although it is very interesting, many students are worried about its compatibility. Through Caniuse, we can see the browser's support for it:

Summary

It's not surprising if you are familiar with :focus, but you know:focus-within If , it means that you are constantly paying attention to new features related to CSS. Of course this property refreshed the world of CSS again which is really weird. If you are interested, why not write the demo yourself. You will love this property.

The above is the detailed content of Explanation of the specific usage of CSS :focus-within. 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 use DirectX repair tool? Detailed usage of DirectX repair tool How to use DirectX repair tool? Detailed usage of DirectX repair tool Mar 15, 2024 am 08:31 AM

The DirectX repair tool is a professional system tool. Its main function is to detect the DirectX status of the current system. If an abnormality is found, it can be repaired directly. There may be many users who don’t know how to use the DirectX repair tool. Let’s take a look at the detailed tutorial below. 1. Use repair tool software to perform repair detection. 2. If it prompts that there is an abnormal problem in the C++ component after the repair is completed, please click the Cancel button, and then click the Tools menu bar. 3. Click the Options button, select the extension, and click the Start Extension button. 4. After the expansion is completed, re-detect and repair it. 5. If the problem is still not solved after the repair tool operation is completed, you can try to uninstall and reinstall the program that reported the error.

Introduction to HTTP 525 status code: explore its definition and application Introduction to HTTP 525 status code: explore its definition and application Feb 18, 2024 pm 10:12 PM

Introduction to HTTP 525 status code: Understand its definition and usage HTTP (HypertextTransferProtocol) 525 status code means that an error occurred on the server during the SSL handshake, resulting in the inability to establish a secure connection. The server returns this status code when an error occurs during the Transport Layer Security (TLS) handshake. This status code falls into the server error category and usually indicates a server configuration or setup problem. When the client tries to connect to the server via HTTPS, the server has no

How to use Baidu Netdisk-How to use Baidu Netdisk How to use Baidu Netdisk-How to use Baidu Netdisk Mar 04, 2024 pm 09:28 PM

Many friends still don’t know how to use Baidu Netdisk, so the editor will explain how to use Baidu Netdisk below. If you are in need, hurry up and take a look. I believe it will be helpful to everyone. Step 1: Log in directly after installing Baidu Netdisk (as shown in the picture); Step 2: Then select "My Sharing" and "Transfer List" according to the page prompts (as shown in the picture); Step 3: In "Friend Sharing", you can share pictures and files directly with friends (as shown in the picture); Step 4: Then select "Share" and then select computer files or network disk files (as shown in the picture); Fifth Step 1: Then you can find friends (as shown in the picture); Step 6: You can also find the functions you need in the "Function Treasure Box" (as shown in the picture). The above is the editor’s opinion

Learn to copy and paste quickly Learn to copy and paste quickly Feb 18, 2024 pm 03:25 PM

How to use the copy-paste shortcut keys Copy-paste is an operation we often encounter when using computers every day. In order to improve work efficiency, it is very important to master the copy and paste shortcut keys. This article will introduce some commonly used copy and paste shortcut keys to help readers perform copy and paste operations more conveniently. Copy shortcut key: Ctrl+CCtrl+C is the shortcut key for copying. By holding down the Ctrl key and then pressing the C key, you can copy the selected text, files, pictures, etc. to the clipboard. To use this shortcut key,

What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? What is the KMS activation tool? How to use the KMS activation tool? How to use KMS activation tool? Mar 18, 2024 am 11:07 AM

The KMS Activation Tool is a software tool used to activate Microsoft Windows and Office products. KMS is the abbreviation of KeyManagementService, which is key management service. The KMS activation tool simulates the functions of the KMS server so that the computer can connect to the virtual KMS server to activate Windows and Office products. The KMS activation tool is small in size and powerful in function. It can be permanently activated with one click. It can activate any version of the window system and any version of Office software without being connected to the Internet. It is currently the most successful and frequently updated Windows activation tool. Today I will introduce it Let me introduce to you the kms activation work

How to correctly use the win10 command prompt for automatic repair operations How to correctly use the win10 command prompt for automatic repair operations Dec 30, 2023 pm 03:17 PM

The longer the computer is used, the more likely it is to malfunction. At this time, friends need to use their own methods to repair it. So what is the easiest way to do it? Today I will bring you a tutorial on how to repair using the command prompt. How to use win10 automatic repair command prompt: 1. Press "Win+R" and enter cmd to open the "command prompt" 2. Enter chkdsk to view the repair command 3. If you need to view other places, you can also add other partitions such as "d" 4. Enter the execution command chkdskd:/F. 5. If it is occupied during the modification process, you can enter Y to continue.

How to merge cells using shortcut keys How to merge cells using shortcut keys Feb 26, 2024 am 10:27 AM

How to use the shortcut keys for merging cells In daily work, we often need to edit and format tables. Merging cells is a common operation that can merge multiple adjacent cells into one cell to improve the beauty of the table and the information display effect. In mainstream spreadsheet software such as Microsoft Excel and Google Sheets, the operation of merging cells is very simple and can be achieved through shortcut keys. The following will introduce the shortcut key usage for merging cells in these two software. exist

How to use potplayer-How to use potplayer How to use potplayer-How to use potplayer Mar 04, 2024 pm 06:10 PM

Potplayer is a very powerful media player, but many friends still don’t know how to use potplayer. Today I will introduce how to use potplayer in detail, hoping to help everyone. 1. PotPlayer shortcut keys. The default common shortcut keys for PotPlayer player are as follows: (1) Play/pause: space (2) Volume: mouse wheel, up and down arrow keys (3) forward/backward: left and right arrow keys (4) bookmark: P- Add bookmarks, H-view bookmarks (5) full screen/restore: Enter (6) multiple speeds: C-accelerate, 7) Previous/next frame: D/

See all articles