简体   繁体   English

CSS 下拉菜单无法正确隐藏/显示

[英]CSS Dropdown menu won't hide/display properly

I tried searching and finding out how to make a dropdown menu and they all say to use display: none then when hover display block which makes sense but I can't seem to make it work in my code, what I want is to be able to hover my span element and be able to have a dropdown menu come form it.我尝试搜索并找出如何制作下拉菜单,他们都说要使用display: none然后当 hover 显示块是有道理的,但我似乎无法让它在我的代码中工作,我想要的是能够到 hover 我的span元素,并能够从它那里得到一个下拉菜单。

 .dropdown { position: relative; display: inline-block; }.dropdown-content { display: none; position: absolute; z-index: 1; width: 10rem; overflow: auto; }.dropdown:hover, .dropdown-content { display: block; }
 <div class="user-nav__user"> <img src="img/user.jpg" alt="User photo" class="user-nav__user-photo" /> <div class="dropdown"> <span class="user-nav__user-name">Jonas</span> <div class="dropdown-content"> <a href="#">Test 1</a> <a href="#">Test 2</a> <a href="#">Test 3</a> </div> </div> </div>

Remove the comma between.dropdown:hover and.dropdown-content in the css like this:删除 css 中的逗号 between.dropdown:hover 和 .dropdown-content,如下所示:

 .dropdown { position: relative; display: inline-block; }.dropdown-content { display: none; position: absolute; z-index: 1; width: 10rem; overflow: auto; }.dropdown:hover.dropdown-content { display: block; }
 <div class="user-nav__user"> <img src="img/user.jpg" alt="User photo" class="user-nav__user-photo" /> <div class="dropdown"> <span class="user-nav__user-name">Jonas</span> <div class="dropdown-content"> <a href="#">Test 1</a> <a href="#">Test 2</a> <a href="#">Test 3</a> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM