且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

仅使用 CSS,在悬停在另一个元素上时显示 div

更新时间:2023-01-08 10:08:55

你可以像这个:

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

<a>Hover over me!</a>
<div>Stuff shown on hover</div>

这使用了相邻兄弟选择器,并且是suckerfish 下拉菜单.

HTML5 允许锚元素包装几乎任何东西,因此在这种情况下,div 元素可以成为锚的子元素.否则原理是相同的 - 使用 :hover 伪类来更改另一个元素的 display 属性.

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.