html - 位置:绝对导致元素在网页中消失
问题描述
position: absolute
正在导致元素消失
我正在为我的网页编写一个导航栏,我想将ul
嵌套在元素内的nav
元素居中,所以我使用了 Smash Magazine 编写的技术来居中(垂直,而不是水平)。结果是背景nav
完全消失了,但是当我将鼠标悬停在li
元素上时,它们各自的背景就会出现。我该如何解决?
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Raleway" rel="stylesheet">
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<ul>
<li><a href="#">Support Us</a></li>
<li><a href="#">Download</a></li>
<li><a href="#">GitHub</a></li>
</ul>
</nav>
<header>
<h1>Scriptura</h1>
<p><em>Customizable note taking software</em></p>
</header>
<section>
<h2>What Is <em>Scriptura</em></h2>
<p><em>Scriptura</em> is a note-taking software that is meant to be super cuztomizable to fit the users needs. Most of the time, you are either spending money on either a paid software that takes you way over the top for stuff you don't need, and other times you are stuck with a limited-memory budget version of note-taking software. With <em>Scriptura</em>, you can add and remove features as you need them, and have <strong>unlimited</strong> cloud storage data with syncing across 5 devices!</p>
</section>
<script src="index.js"></script>
</body>
</html>
CSS:
body {
margin: 0;
}
h1, h2, h3, h4, h5, h6 {
font-family: Open Sans, sans-serif;
}
p {
font-family: Raleway, sans-serif;
}
li {
font-family: Raleway, sans-serif;
}
a {
font-family: Raleway, sans-serif;
text-decoration: none;
color: black;
}
nav {
background: whitesmoke;
position: relative;
height: 100%;
}
nav ul {
position: absolute;
margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
list-style-type: none;
text-align: right;
margin-right: 30px;
}
nav li {
display: inline;
padding: 10px;
transition: background-color 0.4s ease-in-out 0s;
}
nav li:hover {
background-color: lightgrey;
}
解决方案
请检查小提琴,问题是您为导航设置了 100% 的高度,浏览器无法理解 100% 的元素。例如,您可以为菜单设置 50 像素,并按行高或仅高度对齐“li”。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans|Raleway" rel="stylesheet">
<link href="index.css" rel="stylesheet" type="text/css" />
</head>
<body>
<nav>
<ul>
<li><a href="#">Support Us</a></li>
<li><a href="#">Download</a></li>
<li><a href="#">GitHub</a></li>
</ul>
</nav>
<header>
<h1>Scriptura</h1>
<p><em>Customizable note taking software</em></p>
</header>
<section>
<h2>What Is <em>Scriptura</em></h2>
<p><em>Scriptura</em> is a note-taking software that is meant to be super cuztomizable to fit the users needs. Most of the time, you are either spending money on either a paid software that takes you way over the top for stuff you don't need, and other times you are stuck with a limited-memory budget version of note-taking software. With <em>Scriptura</em>, you can add and remove features as you need them, and have <strong>unlimited</strong> cloud storage data with syncing across 5 devices!</p>
</section>
<script src="index.js"></script>
</body>
</html>
body {
margin: 0;
}
h1, h2, h3, h4, h5, h6 {
font-family: Open Sans, sans-serif;
}
p {
font-family: Raleway, sans-serif;
}
li {
font-family: Raleway, sans-serif;
}
a {
font-family: Raleway, sans-serif;
text-decoration: none;
color: black;
}
nav {
background: whitesmoke;
position: relative;
height: 50px;
}
nav ul {
position: absolute;
margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
list-style-type: none;
text-align: right;
margin-right: 30px;
}
nav li {
display: inline;
padding: 10px;
transition: background-color 0.4s ease-in-out 0s;
line-height: 50px;
}
nav li:hover {
background-color: lightgrey;
}
推荐阅读
- javascript - 如何检查对象键并替换其值?
- amazon-web-services - 由于 amplify.ps1 安全问题,命令“amplify configure”无法运行
- javascript - 删除表中的空行 - Javascript
- sequence - Freemarker:从另一个序列中删除一个序列中的项目
- javascript - 刷新页面后,我无法通过 Auth0 API 检索用户信息
- pytorch - 如何将带有 CUDA 的 PyTorch 添加到 Dask Helm 图表
- python - 将列表值拆分为列:python
- javascript - 如何比较两个数组 test dsa das dsge a
- mysql - 如何将表列从十进制更改为 varchar
- r - 带有主题的闪亮应用程序无法更改按钮背景颜色