I've got a question. I was trying to code a navbar. It's working perfectly except for the fact that over the navbar there is a big space. So that the navbar is in the middle of the page. I've got no clue what the mistake could be. If u got an idea of what the problem could be and how to solve it, I Would be very pleased. Otherwise, I would be happy to get improvement tips for my code.
.navbar {
position: fixed;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
top: 20%;
width: 100%;
height: 70px;
background-color: black;
-webkit-box-shadow: 0 0 10px var(--shadow);
box-shadow: 0 0 10px var(--shadow);
color: white;
margin-top: 2px;
}
.navbar-nav {
width: 40%;
height: 100%;
padding: 0 7%;
margin: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
list-style: none;
}
.navbar-nav .nav-item {
text-align: center;
}
.navbar-nav .nav-item .item-link {
position: relative;
width: 100%;
padding: 15px 20px;
color: var(--white-smoke);
text-decoration: none;
border-radius: 5px;
border: 1px solid #0e0e0e00;
}
.navbar-nav .nav-item .item-link:hover {
background-color: var(--shadow);
border: 1px solid #4e4e4e4d;
}
.navbar-nav .c:hover #drop-down {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.navbar-nav .c:focus-within #drop-down {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.navbar-nav .c #drop-down {
position: absolute;
width: -webkit-fit-content;
width: -moz-fit-content;
width: fit-content;
display: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
top: 60px;
background-color: var(--white-smoke);
padding: 30px 0;
border-radius: 5px;
text-align: left;
-webkit-box-shadow: 0 0 10px var(--shadow);
box-shadow: 0 0 10px var(--shadow);
-webkit-transform: translateX(-10%);
transform: translateX(-10%);
}
.navbar-nav .c #drop-down .clinks {
display: block;
color: #000;
padding: 5px 0;
text-decoration: none;
padding: 10px 50px 10px 20px;
}
.navbar-nav .c #drop-down .clinks i {
padding: 0 2px;
}
.navbar-nav .c #drop-down .clinks:hover {
background-color: var(--shadow);
}
@media screen and (max-width: 1000px) {
.navbar #brand {
display: none;
}
.navbar .navbar-nav {
width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<title>DropDown Menu</title>
</head>
<body>
<nav >
<ul >
<!-- Home -->
<li ><a href="#" >Home</a>
</li>
<!-- Categories -->
<li >
<a href="#" >
<i aria-hidden="true"></i> Investieren</a>
<!-- Dropdown Menu -->
<div id="drop-down">
<a href="#" > <i aria-hidden="true"></i> Web Development
</a>
<a href="#" > <i aria-hidden="true"></i> Mobile Apps
</a>
<a href="#" > <i aria-hidden="true"></i> UI / UX
</a>
<a href="#" > <i aria-hidden="true"></i> Database
</a>
<a href="#" > <i aria-hidden="true"></i> Game Development
</a>
</div>
</li>
<!-- Prices -->
<li ><a href="#" >Prices</a>
</li>
<!-- About -->
<li ><a href="#" >About</a>
</li>
</ul>
</nav>
</body>
</html>
CodePudding user response:
Because you have top: 20%;
on your .navbar
with fixed positioning. You also have margin-top: 2px
on your navbar
. Remove that and you should be good.
.navbar {
position: fixed;
/*top: 20%;*/
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
width: 100%;
height: 70px;
background-color: black;
-webkit-box-shadow: 0 0 10px var(--shadow);
box-shadow: 0 0 10px var(--shadow);
color: white;
/*margin-top: 2px;*/
}
body {
margin: 0;
}
.navbar-nav {
width: 40%;
height: 100%;
padding: 0 7%;
margin: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
list-style: none;
}
.navbar-nav .nav-item {
text-align: center;
}
.navbar-nav .nav-item .item-link {
position: relative;
width: 100%;
padding: 15px 20px;
color: var(--white-smoke);
text-decoration: none;
border-radius: 5px;
border: 1px solid #0e0e0e00;
}
.navbar-nav .nav-item .item-link:hover {
background-color: var(--shadow);
border: 1px solid #4e4e4e4d;
}
.navbar-nav .c:hover #drop-down {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.navbar-nav .c:focus-within #drop-down {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.navbar-nav .c #drop-down {
position: absolute;
width: -webkit-fit-content;
width: -moz-fit-content;
width: fit-content;
display: none;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
top: 60px;
background-color: var(--white-smoke);
padding: 30px 0;
border-radius: 5px;
text-align: left;
-webkit-box-shadow: 0 0 10px var(--shadow);
box-shadow: 0 0 10px var(--shadow);
-webkit-transform: translateX(-10%);
transform: translateX(-10%);
}
.navbar-nav .c #drop-down .clinks {
display: block;
color: #000;
padding: 5px 0;
text-decoration: none;
padding: 10px 50px 10px 20px;
}
.navbar-nav .c #drop-down .clinks i {
padding: 0 2px;
}
.navbar-nav .c #drop-down .clinks:hover {
background-color: var(--shadow);
}
@media screen and (max-width: 1000px) {
.navbar #brand {
display: none;
}
.navbar .navbar-nav {
width: 100%;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="css/style.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<title>DropDown Menu</title>
</head>
<body>
<nav >
<ul >
<!-- Home -->
<li ><a href="#" >Home</a>
</li>
<!-- Categories -->
<li >
<a href="#" >
<i aria-hidden="true"></i> Investieren</a>
<!-- Dropdown Menu -->
<div id="drop-down">
<a href="#" > <i aria-hidden="true"></i> Web Development
</a>
<a href="#" > <i aria-hidden="true"></i> Mobile Apps
</a>
<a href="#" > <i aria-hidden="true"></i> UI / UX
</a>
<a href="#" > <i aria-hidden="true"></i> Database
</a>
<a href="#" > <i aria-hidden="true"></i> Game Development
</a>
</div>
</li>
<!-- Prices -->
<li ><a href="#" >Prices</a>
</li>
<!-- About -->
<li ><a href="#" >About</a>
</li>
</ul>
</nav>
</body>
</html>
CodePudding user response:
It's because you have the navbar
set to a fixed position with a top of 20%.
.navbar {
position: fixed; //this will make sure the navbar always stays in its position
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
top: 20%; //this is what is causing the "space" above
width: 100%;
height: 70px;
background-color: black;
-webkit-box-shadow: 0 0 10px var(--shadow);
box-shadow: 0 0 10px var(--shadow);
color:white;
margin-top:2px;
}
You're telling the navbar to go down 20% of the page with the top:20%. If you don't want the space, just remove the top: 20% and you should be fine.
CodePudding user response:
The issue is you have the navbar
set to top: 20%;
. You need to set it the fixed position to top 0%:
.navbar {
top: 0%;
left: 0%;
right: 0%;
}