Reputation: 55
position: absolute
is Causing Element To DisappearI am writing a navbar for my webpage and I wanted to center the ul
element nested inside a nav
element, so I used a technique written by Smash Magazine to center it (vertically, not horizontally). The result is that the background for the nav
completely disappears, but when I hover over the li
elements, their respective backgrounds appear. How do I fix this?
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;
}
Upvotes: 1
Views: 1856
Reputation: 71
Please check the fiddle, Issue is that you've set 100% height for nav, browser can't understand 100% of what element. You can set 50px for example for menu, and align "li" by line-height or just height.
<!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/
Upvotes: 2