'nav bar list collapse when putting position absolute
I am trying to make a navigation bar with several elements and I am trying to make this elements list takes the height of the header/menu.
I am trying to do that by working on the positions for the divs and elements but for some reason I am not sure where my issue is that the list collapses when I make the li elements position: absolute
My code:
*{
margin:0;
padding: 0;
}
.navMenu ul{
height:100%;
position: relative;
}
.navMenu li{
list-style-type: none;
display: inline;
margin-left: 40px;
margin-right: 40px;
font-size: 25px;
color: white;
height: 100%;
position: absolute;
}
.navMenu{
right:3%;
height: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.navContainer{
position:relative;
background-color: black;
height: 100px;
}
.logoDiv{
position: relative;
height:100%;
position: absolute;
}
.logo{
position: absolute;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="HomeCSS.css">
<title>Home</title>
</head>
<body>
<div class="navContainer">
<div class="logoDiv">
<img class="logo" src="/Images/WatchMo.png" alt="WatchMo logo">
</div>
<div class="navMenu">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Favourites</li>
<li>My Account</li>
</ul>
</div>
</div>
</body>
</html>
Solution 1:[1]
Position absolute sort of prevents elements from taking any space on a page. So when the only elements in navMenu have no height nor width, it will collapse.
Solution 2:[2]
When you are using position:absolute all your elements will get on left:0 and top:0 to the container with position:relative. So this why it collapse.
Here I just modify a bit your .navMenu li to make elements getting the height of your navContainer as you wish:
.navMenu li{
list-style-type: none;
/* display: inline;*/
margin-left: 40px;
margin-right: 40px;
font-size: 20px; /* Originaly 25px */
color: white;
/* height: 100%;
position: absolute;*/
}
DEMO
*{
margin:0;
padding: 0;
}
.navMenu ul{
height:100%;
position: relative;
}
.navMenu li{
list-style-type: none;
/* display: inline;*/
margin-left: 40px;
margin-right: 40px;
font-size: 20px; /* Originaly 25px */
color: white;
/* height: 100%;
position: absolute;*/
}
.navMenu{
right:3%;
height: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.navContainer{
position:relative;
background-color: black;
height: 100px;
}
.logoDiv{
position: relative;
height:100%;
position: absolute;
}
.logo{
position: absolute;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="HomeCSS.css">
<title>Home</title>
</head>
<body>
<div class="navContainer">
<div class="logoDiv">
<img class="logo" src="/Images/WatchMo.png" alt="WatchMo logo">
</div>
<div class="navMenu">
<ul>
<li>Home</li>
<li>About Us</li>
<li>Favourites</li>
<li>My Account</li>
</ul>
</div>
</div>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user16948466 |
| Solution 2 | MaxiGui |
