How to use CSS pseudo-elements to control the style of several consecutive elements

How to use CSS pseudo-elements to control the style of several consecutive elements

When using CSS pseudo-elements to control elements, you often need to change the styles of some elements. There are many blogs on the Internet that talk about how to control the change of one element, but in the actual writing process, I found that more often I need to control the changes of multiple consecutive elements.

Use pseudo elements to control (take :hover as an example). When the mouse stays on A, the styles of BCD... change.

A and BCD....are adjacent and of the same level, requiring A to be at the top of BCD

<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>

//The corresponding CSS code of A controlling BCD. A:hover + .B{
        background-color: orange;
    }
    .A:hover + .B+ .C{
        background-color: orange;
    }
    .A:hover + .B+ .C+ .D{
        background-color: orange;
    }

If you move A to another position, the effect will not be achieved; or if you only write the control code at the bottom of the CSS, you can only control the style change of the third element, and multiple elements cannot be changed together.

A is BCD....is a father-son relationship

<div class="A">
    <div class="B"></div>
    <div class="C"></div>
    <div class="D"></div>
</div>

//Corresponding CSS code.A:hover .B{
        background-color: orange;
    }
    .A:hover .B+ .C{
        background-color: orange;
    }
    .A:hover .B+ .C+ .D{
        background-color: orange;
    }

The first one is actually very easy to understand, because element+element is to control adjacent elements. Since A and CD are not directly adjacent, I will search one level at a time, first to B, because BC are adjacent, so I can start to control it, and the same goes for D.

In the second code, element element is the method by which the parent node controls the child node. A can directly control B. If you need to control C, then find B first. Since BC are adjacent, I will use the method of adjacent element control to control C. The same goes for D.

This concludes this article on how to use CSS pseudo-elements to control the styles of several consecutive elements. For more information about CSS pseudo-elements controlling elements, please search previous articles on 123WORDPRESS.COM or continue browsing the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Two methods to disable form controls in HTML: readonly and disabled

>>:  Vue Basics Listener Detailed Explanation

Recommend

MySQL5.6.31 winx64.zip installation and configuration tutorial

#1. Download # #2. Unzip to local and modify nece...

How to make a centos base image

Preface Now the operating system used by my compa...

Use href in html to pop up a file download dialog box when clicking a link

I learned a new trick today. I didn’t know it befo...

How to parse the attribute interface of adding file system in Linux or Android

The first one: 1. Add key header files: #include ...

Creating private members in JavaScript

Table of contents 1. Use closures 2. Use ES6 clas...

Vue implements the question answering function

1. Request answer interface 2. Determine whether ...

CentOS 8 custom directory installation nginx (tutorial details)

1. Install tools and libraries # PCRE is a Perl l...

Steps for importing tens of millions of data into MySQL using .Net Core

Table of contents Preliminary preparation Impleme...

Windows Server 2016 Standard Key activation key serial number

I would like to share the Windows Server 2016 act...

How to view the network routing table in Ubuntu

What are Routing and Routing Table in Linux? The ...

Vue3 AST parser-source code analysis

Table of contents 1. Generate AST abstract syntax...

React Native environment installation process

react-native installation process 1.npx react-nat...

Comprehensive summary of mysql functions

Table of contents 1. Commonly used string functio...

How to implement two-way binding function in vue.js with pure JS

Table of contents First, let's talk about the...