div rollover getting child divs to change color

Emma Linnery

I have 3 child divs inside a parent div, at present only parts of the child divs change color on mouse over, I want all the child divs to change color on part mouse hover. thanks

I have tried the code below

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Untitled Document</title> 
        <style>
        .container {
            display: inline-block;
            cursor: pointer;
            width: 35px;
        }
        .bar1, .bar2, .bar3 {
            width: 35px;
            height: 5px;
            background-color: #333;
            margin: 6px 0;
            transition: 0.4s;
        }
        .bar1:hover, .bar2:hover, .bar3:hover {
            width: 35px;
            height: 5px;
            background-color: #e5e5e5;
            margin: 6px 0;
            transition: 0.4s;
        }
        </style>
    </head>
    <body>
        <div style=" text-align: right; padding-right: 30px;">
        <div class="container">
            <div class="bar1"></div>
            <div class="bar2"></div>
            <div class="bar3"></div>
        </div>
    </body>
</html>
Nikolas Stevenson-Molnar

You can accomplish this by selecting children of your parent node when hovered.

Your current code:

.bar1:hover, .bar2:hover, .bar3:hover {
    width: 35px;
    height: 5px;
    background-color: #e5e5e5;
    margin: 6px 0;
    transition: 0.4s;
}

... will apply to each element individually (i.e., .bar3 itself will need to be hovered to trigger that state).

Instead, you want to select child elements of .container:hover:

.container:hover .bar1, .container:hover .bar2, .container:hover .bar3 {
    width: 35px;
    height: 5px;
    background-color: #e5e5e5;
    margin: 6px 0;
    transition: 0.4s;
}

This will apply the styles to:

  1. .bar1, when container is hovered
  2. .bar2, when container is hovered
  3. .bar3, when container is hovered

EDIT: I didn't realize at first that bar1 is a sibling of the other two. Updated my answer to apply style based on container:hover.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Change color of a div based on divs color background

How to change attributes of only first child divs within a div

flip a div and change the color of the inside divs when flipped?

how to select specific multiple divs inside other div and change their color

Change font color on all other divs when hover one div

Getting 2 divs to change colour when hovering over 1 div

css: change background color of child-div but not parent div

Change background color of child div on hover of parent div?

How do i change color on dinamically created divs by selecting 3 divs and double click to change color on the third div with jquery?

Change the text color of child div one mouse over

Change border color of parent div if child input is focused - TailwindCSS

Closing divs by clicking on child div

Parent Div not expanding with child Divs

Order divs by the ID of a child div

On hover on the parent div, Change the color of all the child div as well as parent background color

change color of a group of divs on click

Div overflowing on another Div having child Divs

Change parent div height according to child img height, without exceeding parent divs max-width

change background of a div containing divs

Find div and change color

How to change div color?

Add parent DIV css properties to child DIVs

Append Div with Multiple Child Divs Using For Loop

How to make outer div fit child divs?

Hide parent div if an child divs are empty

Center child divs with tables inside a parent div

change border color of multiple divs with button

How to change the color of two divs for a matching game?

How do I change the color and width of divs?