位置:绝对导致网页上的元素消失

时间:2018-07-09 14:15:13

标签: html5 css3

gist导致元素消失

我正在为网页编写一个导航栏,并且我想将position: absolute元素中嵌套的ul元素居中,因此我使用了Smash Magazine编写的技术来居中(垂直而不是水平)。结果是nav的背景完全消失了,但是当我将鼠标悬停在nav元素上时,它们各自的背景就会出现。我该如何解决?

HTML:

li

CSS:

<!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>

1 个答案:

答案 0 :(得分:1)

请检查小提琴, 问题是您已将导航的高度设置为100%,浏览器无法理解该元素的100%。您可以将菜单设置为50px, 并按行高或仅高度对齐“ 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/