Using only CSS, show div on hover over

后端 未结 13 1110
时光取名叫无心
时光取名叫无心 2020-11-22 01:58

I would like to show a div when someone hovers over an element, but I would like to do this in CSS and not JavaScript. Do you know how this can be ach

相关标签:
13条回答
  • 2020-11-22 02:22

    Don't forget. if you are trying to hover around an image, you have to put it around a container. css:

    .brand:hover + .brand-sales {
        display: block;
    }
    
    .brand-sales {
        display: none;
    }
    

    If you hover on this:

    <span className="brand">
       <img src="https://murmure.me/wp-content/uploads/2017/10/nike-square-1900x1900.jpg" 
         alt"some image class="product-card-place-logo"/>
    </span>
    

    This will show:

    <div class="product-card-sales-container brand-sales">
        <div class="product-card-">Message from the business goes here. They can talk alot or not</div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 02:23

    The + allow 'select' only first not nested element , the > select nested elements only - the better is to use ~ which allow to select arbitrary element which is child of parent hovered element. Using opacity/width and transition you can provide smooth appear

    div { transition: all 1s }
    .ccc, .ggg { opacity: 0; color: red}
    .ccc { height: 0 }
    
    .aaa:hover ~ .bbb .ccc { opacity: 1; height: 34px }
    .aaa:hover ~ .eee .fff .ggg { opacity: 1 }
    <div class="aaa">Hover me... to see<br><br> </div>
    
    <div class='bbb'>BBBBB
      <div class='ccc'>CCCCC
        <div class='ffffd'>DDDDD</div>
      </div>
    </div>
    
    <div class='eee'>EEEEE
      <div class='fff'>FFFFF
        <div class='ggg'>GGGGG</div>
        <div class='hhh'>HHHHH</div>
      </div>
    </div>

    0 讨论(0)
  • 2020-11-22 02:23

    From my testing using this CSS:

    .expandable{
    display: none;
    }
    .expand:hover+.expandable{
    display:inline !important;
    }
    .expandable:hover{
    display:inline !important;
    }
    

    And this HTML:

    <div class="expand">expand</div>
    <div class="expand">expand</div>
    <div class="expandable">expandable</div>
    

    , it resulted that it does expand using the second , but does not expand using the first one. So if there is a div between the hover target and the hidden div, then it will not work.

    0 讨论(0)
  • 2020-11-22 02:25

    HTML

    <div>
        <h4>Show content</h4>
    </div>
    <div>
      <p>Hello World</p>
    </div>
    

    CSS

     div+div {
        display: none;
     }
    
     div:hover +div {
       display: block;
     }
    

    CodePen :hover on div show text in another div

    0 讨论(0)
  • 2020-11-22 02:26

    You can do something like this:

    div {
        display: none;
    }
        
    a:hover + div {
        display: block;
    }
    <a>Hover over me!</a>
    <div>Stuff shown on hover</div>

    This uses the adjacent sibling selector, and is the basis of the suckerfish dropdown menu.

    HTML5 allows anchor elements to wrap almost anything, so in that case the div element can be made a child of the anchor. Otherwise the principle is the same - use the :hover pseudo-class to change the display property of another element.

    0 讨论(0)
  • 2020-11-22 02:26

    For me, if I want to interact with the hidden div without seeing it disappear each time I leave the triggering element (a in that case) I must add:

    div:hover {
        display: block;
    }
    
    0 讨论(0)
提交回复
热议问题