0

I have 2 divs, i want to hover the first one to affect both of them, i found out on the forum how to affect the second by hovering the first one, but not both of them.

There is my code:

        .red{
                height: 150px;
                width: 150px;
                background-color: red;
                float:left;
                margin-right: 20px;
        }

        .blue{
                height: 150px;
                width: 150px;
                background-color: blue;
                float:left;
        }

        .red:hover ~ .blue{
                transition: all .2s ease-in-out; 
                transform: scale(1.1); 
        }
<html>
<head>
</head>
<body>
    <div class="red"></div>
    <div class="blue"></div>
</body>
</html>

Thanks for helping me.

Clint
  • 973
  • 7
  • 18
alioua walid
  • 247
  • 3
  • 19

3 Answers3

1

you can add red:hover selector to last style declaration ie you do to hovered red div what you are doing to blue div

.red{
                height: 150px;
                width: 150px;
                background-color: red;
                float:left;
                margin-right: 20px;
        }

        .blue{
                height: 150px;
                width: 150px;
                background-color: blue;
                float:left;
        }

        /* added one more selector here */
        .red:hover,
        .red:hover ~ .blue{
                transition: all .2s ease-in-out; 
                transform: scale(1.1); 
        }
        
<html>
<head>
</head>
<body>
    <div class="red"></div>
    <div class="blue"></div>
</body>
</html>
ashish singh
  • 6,526
  • 2
  • 15
  • 35
0

to apply a rule to more than one selector use a comma between selectors

.red:hover ~ .blue, .red:hover {
                transition: all .2s ease-in-out; 
                transform: scale(1.1); 
        }
Clint
  • 973
  • 7
  • 18
0

You can always give the same class to the divs that you want to affect.

<html>
<head>
</head>
<body>
    <div class="red affect"></div>
    <div class="blue affect"></div>
</body>
</html>


.affect {
     width: 150px;
     height: 150px;

     float:left;
}

.red {
     background-color: red;
 }

 .blue {
     background-color: blue;
  }

  .affect:hover {
     transition: all .2s ease-in-out; 
     transform: scale( 1.2 ); 
    }
Nathan Harold
  • 55
  • 1
  • 6