<!DOCTYPE html>
<html>
<head>
<title>鼠标悬停显示/隐藏菜单</title>
<style>
.container {
align-items: flex-start;
}
.subnav {
position: relative; /* 添加相对定位 */
width: 200px;
}
.subnav ul {
display: none;
position: absolute; /* 添加绝对定位 */
top: -20px;
left: 50px; /* 设置左侧距离为父元素宽度,即显示在右侧 */
width: 150px;
box-shadow: 0 1px 2px #222;
list-style: none;
padding: 0;
}
.subnav:hover ul {
display: block;
}
.subnav li {
height: 40px;
text-align: center;
line-height: 40px;
background: white;
margin-right: 10px; /* 可选,用于设置列表项之间的间距 */
}
.subnav li:hover {
color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="subnav">
<a href="#">菜单按钮1</a>
<ul>
<li>菜单项1</li>
<li>菜单项2</li>
<li>菜单项3</li>
</ul>
</div>
<br><br><br>
<div class="subnav">
<a href="#">菜单按钮2</a>
<ul>
<li>菜单项4</li>
<li>菜单项5</li>
<li>菜单项6</li>
</ul>
</div>
</div>
</body>
</html>