首页 > 解决方案 > 位置:绝对导致元素在网页中消失

问题描述

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;
}

标签: htmlcss

解决方案


请检查小提琴,问题是您为导航设置了 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;
}

http://jsfiddle.net/7gokv9py/5/


推荐阅读