Show child div on mouse hover of parent - needs javascript?

前端 未结 7 1994
悲哀的现实
悲哀的现实 2021-02-01 13:27

I need to show a div when you house over its parent (default is for the child div to be hidden). Is the only way to do this with javascript? Thanks

相关标签:
7条回答
  • 2021-02-01 13:57

    In addition to the accepted answer, one can use opacity so that layout is not jumping around on hover (this also allows to animate the appearance):

    css:

    .show-on-hover-parent:hover .show-on-hover-item {
        opacity: 1;
       //transition: opacity .5s; //<- optional opacity animation
    }
    
    .show-on-hover-parent .show-on-hover-item {
        opacity: 0;
       //transition: opacity .5s; //<- optional opacity animation
    }
    

    html:

    <div class="show-on-hover-parent">
        <div>Always visible 1</div>
        <div class="show-on-hover-item">visible on hover</div>
        <div>Always visible 2</div>
    </div>
    
    0 讨论(0)
提交回复
热议问题