Using only CSS, show div on hover over

后端 未结 13 1117
时光取名叫无心
时光取名叫无心 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:26

    You can do something like this:

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

    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.

提交回复
热议问题