I’m making a navigation bar with dropdown features + a home button. But, the Home button stays on the very top right corner of the page, and not with the dropdown options. In this navigation bar, there should be a home button (to the left), with the 3 dropdown menus on the same line next to it. But, the home button goes on the very top right of the page and I’m not sure how to fix it. This was also taken from a Codepen example and changed to fit the theme and subject of my website.
This is what the navigation bar looks like right now:
header {
height: 120px;
position: sticky;
top: 0;
background-color: #21203d;
float: left;
z-index: 10;
}
.logo {
max-width: 120px;
margin-top: 5px;
margin-bottom: 10px;
margin-left: 20px;
background-color: #21203d;
width: 16%;
}
.navbar {
background-color: #21203d;
font-family: Franklin Gothic;
display: inline-block;
font-size: large;
position: absolute;
right: 0;
margin-right: 30px;
}
.navbar a {
color: white;
text-decoration: none;
font-size: large;
float: right;
}
.dropdown {
float: right;
text-align: center;
display: block;
margin-top: 40px;
font-size: large;
}
.dropdown .dropbtn {
font-size: large;
text-transform: uppercase;
font-weight: bold;
border: none;
outline: none;
color: white;
padding: 15px;
background-color: #21203d;
margin: 0;
}
<header>
<div class="wrapper">
<img src="Images/Favicon.png" class="logo"><span class="color"></span>
<nav class="navbar">
<a href="Index.html">Home</a>
<div class="dropdown">
<button class="dropbtn">Aerospace Engineering</button>
<div class="dropdown-content">
<a href="">Aerodynamics</a>
<a href="#">Aircraft</a>
<a href="">Spacecraft</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Quantum Fields</button>
<div class="dropdown-content">
<a href="#">Mechanics</a>
<a href="#">Physics</a>
<a href="">Computing</a>
</div>
</div>
<div class="dropdown">
<button class="dropbtn">Astronomy</button>
<div class="dropdown-content">
<a href="#">The Stars</a>
<a href="#">Galaxy Types</a>
<a href="#">Black Holes</a>
</div>
</div>
</nav>
</div>
</header>
2
Answers
Alignment is disrupted by the combination of
position: absolute
andfloat: right
. To correct this:header
and.navbar
to display: flex;`.align-items: center
and usegap
for spacing.float
andabsolute
positioning for.navbar
and.dropdown
.Updated CSS
This aligns "Home" and dropdowns on the same line. Let me know if this helps!
First Set Header width to 100%; then add home button inside a div with class and add style as you have added in dropdown with float left.
i have tested it and adding the updated css,