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 >
<div >
<img src="/Images/WatchMo.png" alt="WatchMo logo">
</div>
<div >
<ul>
<li>Home</li>
<li>About Us</li>
<li>Favourites</li>
<li>My Account</li>
</ul>
</div>
</div>
</body>
</html>
CodePudding user response:
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.
CodePudding user response:
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 >
<div >
<img src="/Images/WatchMo.png" alt="WatchMo logo">
</div>
<div >
<ul>
<li>Home</li>
<li>About Us</li>
<li>Favourites</li>
<li>My Account</li>
</ul>
</div>
</div>
</body>
</html>