to learn HTML more in depth I’m building the website of a famous fashion brand, trying to emulate and build all the functions that I see with the HTML knowledge that I have. I still haven’t learned CSS, but I would like to know how to lay the html basics for a nav bar that, when hovered, drops down a menu with anchors to other sites and forms.
This is what I’ve tried so far.
The HTML:
<header>
<nav>
<img src="img/logo.png" alt="Brand Name" title="Brand Name" width="250" loading="lazy">
<ul>
<li>
<div class="navElement-nuoviArrivi">
<div class="nuoviArrivi">NUOVI ARRIVI</div>
<div class="dropdown-nuoviArrivi">
<ul>
<li><a href="https://link.com"></a>Coats</li>
<li><a href="https://link.com"></a>Shoes</li>
<li><a href="https://link.com"></a>Belts</li>
<li><a href="https://link.com"></a>Sweaters</li>
<li><a href="https://link.com"></a>Jewelry</li>
</ul>
</div>
</div>
</li>
<li><!--Other Nav Element with the same code structure--></li>
<li><!--Other Nav Element with the same code structure--></li>
<li><!--Other Nav Element with the same code structure--></li>
<li><!--Other Nav Element with the same code structure--></li>
</ul>
</nav>
</header>
The CSS I found on W3:
/* The container <div> - needed to position the dropdown content */
.navElement-nuoviArrivi {
position: relative;
display: inline-block;
}
.dropdown-nuoviArrivi {
display: none;
position: absolute;
background-color: #ffffff;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
/* Change color of dropdown links on hover */
.dropdown-nuoviArrivi a:hover {background-color: #ddd;}
/* Show the dropdown menu on hover */
.navElement-nuoviArrivi:hover .dropdown-nuoviArrivi {display: block;}
2
Answers
The basics you want to master here is using a
display: none
on the normal state of your dropdown. And when hovering this it changes todisplay: block
. Here is an example:Also, the thing you’re doing wrong here is: you’re closing this div right after you’re opening it.
<div class="nuoviArrivi">NUOVI ARRIVI</div>
The thing you want to do here is close the div after the whole list element. Because now your list isn’t associated with this div, while the CSS says it is and the hover is depending on this. The right way to do it is in the example above. You can run the code snippet to see it in action.If you have any other questions let me know! 🙂
It looks like you’re on the right track to create a dropdown menu in your navigation bar using HTML and CSS. However, there are a few adjustments needed to make it functional.
In the code I provided
I’ve enhanced the navigation menu by making "NUOVI ARRIVI" clickable with an
<a>
element, wrapping each menu item in<li>
elements with the class navElement, and amended the CSS selectors and
rules to style the dropdown links.
But obviously this is very basic it’s up to you to customise it the way you want.