0

hover on button but div does not change its property

change CSS property of div by hovering on another tag(button) which is created after it

.zz {
  text-align: center;
  line-height: 100px;
  font-size: 20px;
  font-weight: bold;
  width: 100px;
  height: 100px;
  color: white;
  background: red;
  border: 4px solid black;
  margin: 100px;
  -webkit-transition: transform 1s;
  -webkit-transition-timing: ease-in-out;
  -webkit-transition-delay: 0.1s;
  float: left;
}

#btn:hover~.zz {
  background: green;
  -webkit-transform: rotate(40deg) translate(100px, 20px);
}
<div class="zz"> this is div</div>
<button id="btn"> hover me </button>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
Surya Pratap
  • 59
  • 2
  • 6

2 Answers2

5

You need to place the div after the button in the DOM for sibling selector. Or, you could use javascript to add a class to the .zz div.

.zz {
  text-align: center;
  line-height: 100px;
  font-size: 20px;
  font-weight: bold;
  width: 100px;
  height: 100px;
  color: white;
  background: red;
  border: 4px solid black;
  margin: 100px;
  -webkit-transition: transform 1s;
  -webkit-transition-timing: ease-in-out;
  -webkit-transition-delay: 0.1s;
  float: left;
}

#btn:hover ~ .zz {
  background: green;
  -webkit-transform: rotate(40deg) translate(100px, 20px);
}
<button id="btn"> hover me </button>
<div class="zz"> this is div</div>
Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
0

#btn:hover ~ .zz, Selects every .zz class that are preceded by a #btn id when #btn is hover,but in your case #btn is before of .zz,so .zz is not selected.

So,Just Change :

<div class="zz"> this is div</div>    ---
<button id="btn"> hover me </button>     |
                            Here   <-----

To:

<button id="btn"> hover me </button>
<div class="zz"> this is div</div>

.zz{ text-align:center; 
  line-height:100px; 
  font-size:20px; 
  font-weight:bold; 
  width:100px; height:100px; 
  color:white; 
  background:red; 
  border:4px solid black;
   margin:100px;
   
-webkit-transition: transform 1s ;
-webkit-transition-timing:ease-in-out;
 -webkit-transition-delay:0.1s;
  float:left;
    }
    
    #btn:hover ~.zz{ background:green;
                -webkit-transform:rotate(40deg) translate(100px,20px);
                
                
            }
<button id="btn"> hover me </button>
<div class="zz"> this is div</div>
caramba
  • 21,963
  • 19
  • 86
  • 127
Ehsan
  • 12,655
  • 3
  • 25
  • 44