This is my first project using HTML and CSS so I'm pretty much a newbie for this language. I think my dropdown menu is conflicting with the bottom-border because it won't show up. This is both my HTML and CSS code for it.
* {
margin: 0;
padding: 0;
background-color: white;
overflow-x: hidden;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
header {
background-color: rgb(255, 255, 255);
height: 600px;
}
.navbar {
width: 85%;
margin: auto;
padding: 60px 0;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo {
width: 160px;
cursor: pointer;
margin-right: auto;
}
.navbar .dropdown {
display: none;
}
.navbar ul li:hover .dropdown {
display: block;
}
.navbar ul li {
list-style: none;
display: inline-block;
margin: 0 20px;
position: relative;
}
.navbar ul li a {
text-decoration: none;
color: black;
}
.navbar ul li::after {
content: '';
height: 3px;
width: 0;
background: teal;
position: absolute;
left: 0;
bottom: 0px;
transition: 0.5s;
}
.navbar ul li:hover::after {
width: 100%;
}
button {
list-style: none;
border: none;
background: teal;
padding: 10px 20px;
border-radius: 30px;
color: white;
font-size: 15px;
transition: 0.4s;
}
<header>
<div >
<div >
<img src="icon.png" >
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<ul >
<li><a href="#">Healthcare</a></li>
<li><a href="#">Cosmetic</a></li>
<li><a href="#">Misc.</a></li>
</ul>
<li><a href="#">About Us</a></li>
<li><a href="#">Register</a></li>
</ul>
<button type="button"><a href="#"></a>Login</button>
</div>
</div>
</header>
I already tried many options from various sources and It still won't work. I think the problem lays somewhere between the code for the border-bottom but I'm not sure.
CodePudding user response:
You're missing a li wrapper for the dropdown menu. Otherwise, there is no element where you can hover over. Try this:
<li>
dropdown
<ul >
<li><a href="#">Healthcare</a></li>
<li><a href="#">Cosmetic</a></li>
<li><a href="#">Misc.</a></li>
</ul>
</li>
From there you can carry on with the stylings.
CodePudding user response:
There's a lot to fix in your code. I fixed the essential things, among these:
The dropdown
ul
should be a parent of its main menuli
position: absolute
andposition: relative
for the dropdown and its parentli
, and accordingtop
andmargin
settings for the dropdown and itsli
childrenNo general
overflow: hidden
, since this will hide the submenu being an absolutely positioned child of another elementsome other CSS details - see below
* {
margin: 0;
padding: 0;
background-color: white;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
header {
background-color: rgb(255, 255, 255);
height: 600px;
}
.navbar {
width: 85%;
margin: auto;
padding: 60px 0;
display: flex;
align-items: center;
justify-content: space-between;
}
.logo {
width: 160px;
cursor: pointer;
margin-right: auto;
}
.navbar .dropdown {
display: none;
}
.navbar ul li:hover .dropdown {
display: block;
}
.navbar > ul > li {
position: relative;
}
.navbar > ul > li > .dropdown {
position: absolute;
top: 1.4em;
}
.navbar .dropdown li {
display: block;
margin: 0;
}
.navbar ul li {
list-style: none;
display: inline-block;
margin: 0 20px;
position: relative;
}
.navbar ul li a {
text-decoration: none;
color: black;
}
.navbar ul li::after {
content: '';
height: 3px;
width: 0;
background: teal;
position: absolute;
left: 0;
bottom: 0px;
transition: 0.5s;
}
.navbar ul li:hover::after {
width: 100%;
}
button {
list-style: none;
border: none;
background: teal;
padding: 10px 20px;
border-radius: 30px;
color: white;
font-size: 15px;
transition: 0.4s;
}
<header>
<div >
<div >
<img src="icon.png" >
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Products</a>
<ul >
<li><a href="#">Healthcare</a></li>
<li><a href="#">Cosmetic</a></li>
<li><a href="#">Misc.</a></li>
</ul>
</li>
<li><a href="#">About Us</a></li>
<li><a href="#">Register</a></li>
</ul>
<button type="button"><a href="#"></a>Login</button>
</div>
</div>
</header>