html - HTML/CSS 下拉菜单未覆盖或显示在块中
问题描述
让我的下拉菜单在悬停时正确显示有些麻烦;尽管 z-index 设置为 1,但它目前全部显示在一行上,而且似乎也没有覆盖。尝试制作项目 ap、a 和 div,但仍然被难住了。有什么建议吗?
HTML
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<nav class="navbar">
<a href="/" id="name-link">Name</a>
<div class="dropdown">
<a>Projects</a>
<div class="dropdown-content">
<a href="/">Project 1</a>
<a href="/">Project 2</a>
<a href="/">Project 3</a>
</div>
</div>
<a href="/about.html">About</a>
<a href="/">Contact</a>
</nav>
</body>
</html>
CSS
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
letter-spacing: .015em;
color: #000000;
}
a:link, a:visited {
color: #000000;
text-decoration: none;
}
a:hover, a:active {
color: #a9c4d4;
text-decoration: underline;
}
.navbar {
overflow: hidden;
font-size: 2.5em;
font-weight: 700;
position: fixed;
top: 0;
width: 100%;
list-style-type: none;
margin: 0px 0px 0px 110px;
padding: 10px 0px 10px 0px;
background-color: #ffffff;
}
.navbar a {
float: left;
display: block;
margin: 10px;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
z-index: 1;
}
.dropdown-content a {
float: none;
margin: 10px;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-content {
display: block;
}
#name-link {
color: #a9c4d4;
text-decoration: none;
}
a.selected-link {
text-decoration: underline;
}
jsfiddle在这里:https ://jsfiddle.net/29gev50b/2/
解决方案
您没有在navbar
div 容器中留出足够的空间来扩展菜单,这就是它被切断的原因。z-index
因为盒子(div)不够大,所以不起作用。
添加min-height: 100px;
到您navbar
的修复。
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
letter-spacing: .015em;
color: #000000;
}
a:link,
a:visited {
color: #000000;
text-decoration: none;
}
a:hover,
a:active {
color: #a9c4d4;
text-decoration: underline;
}
.navbar {
overflow: hidden;
font-size: 2.5em;
font-weight: 700;
position: fixed;
top: 0;
width: 100%;
list-style-type: none;
margin: 0px 0px 0px 110px;
padding: 10px 0px 10px 0px;
min-height: 100px;
background-color: #ffffff;
}
.navbar a {
float: left;
display: block;
margin: 10px;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #ffffff;
z-index: 1;
}
.dropdown-content a {
float: none;
margin: 10px;
display: block;
text-align: left;
}
.dropdown:hover .dropdown-content {
display: block;
}
#name-link {
color: #a9c4d4;
text-decoration: none;
}
a.selected-link {
text-decoration: underline;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
<nav class="navbar">
<a href="/" id="name-link">Name</a>
<div class="dropdown">
<a>Projects</a>
<div class="dropdown-content">
<a href="/">Project 1</a>
<a href="/">Project 2</a>
<a href="/">Project 3</a>
</div>
</div>
<a href="/about.html">About</a>
<a href="/">Contact</a>
</nav>
</body>
</html>
推荐阅读
- c# - 如何在表头中插入换行符?
- android - 函数在 c 中被弃用 [-Wdeprecated-declarations]
- node.js - semantic-release 配置为仅从 master 发布
- flutter - 输入文本宽度超过文本域宽度时如何防止flutter文本域文本消失?
- javascript - 连接到 HTML 的 JS 事件不起作用
- java - 如何阅读具有阿拉伯文列的excel
- laravel - 如何从laravel中的路线获取第二个参数
- vim - 使用 neoclide / coc.nvim 在 VIM 中找不到 Clangd
- ios - 如何使用数组对类进行排序?- 迅速 5
- macos - 如何使用 Automator 调整 Visual Studio Code 窗口的大小?