CSS while hover a div how to display other class(or) div?

房东的猫 提交于 2019-12-11 02:32:23

问题


this is my sample stylesheet

 <style>  
 .cover{ margin 0 auto;}    
  .cover .ab{ display:inline-block color:#000; height:25px;}

while hover the class .ab class .ac should drop from -25px from 0px

  .cover .ab:hover .ac{ top:0px }  
  .ac{ position:absolute; top:-25px; height:25px;} 
   </style>

my html looks like

      <body>
       <div class="cover">
       <div class="ab">&nbsp;</div>
       </div>
       <div class="ac">&nbsp;</div>
       </body>   

it doesn't seems to works is there anything wrong in statement please help me thanks n advance.


回答1:


.cover .ab:hover .ac { top:0px } 

This is not working since .ac is not a child of .ab (and also not a child of .cover by the way).




回答2:


Use Sibling selector over Cover. Demo

  .cover:hover ~ .ac { top:0px }  



回答3:


you can achieve it if both of your divs are siblings:

<div class="cover">
   <div class="ab">asdfa</div>     
   <div class="ac">test</div>
</div>

then css:

.ab:hover ~ .ac{ top:0px;height:25px; }  

would work for you working Demo.

The general sibling combinator (~) works for siblings.

you can move your .ac div in .cover div or apply css on .cover div:

.cover:hover ~ .ac { top:0px }  


来源:https://stackoverflow.com/questions/16140744/css-while-hover-a-div-how-to-display-other-classor-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!