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

Vue defines private filters and basic usage

The methods and concepts of private filters and g...

Should I abandon JQuery?

Table of contents Preface What to use if not jQue...

A brief discussion on HTML table tags

Mainly discuss its structure and some important pr...

Solution for Vue routing this.route.push jump page not refreshing

Vue routing this.route.push jump page does not re...

How to redirect nginx directory path

If you want the path following the domain name to...

MySQL trigger trigger add, delete, modify and query operation example

This article uses examples to describe the add, d...

MySQL 8.0.16 installation and configuration tutorial under Windows 10

This article shares with you the graphic tutorial...

Various transformation effects of HTML web page switching

<META http-equiv="Page-Enter" CONTENT...

JavaScript to achieve uniform animation effect

This article example shares the specific code for...

Step by step guide to build a calendar component with React

Table of contents Business Background Using Techn...

CSS horizontal progress bar and vertical progress bar implementation code

Sometimes it’s nice to see some nice scroll bar e...

Detailed tutorial on how to install mysql8.0 using Linux yum command

1. Do a good job of cleaning before installation ...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

Facebook's nearly perfect redesign of all Internet services

<br />Original source: http://www.a-xuan.cn/...

How to run MySQL using docker-compose

Directory Structure . │ .env │ docker-compose.yml...