'Submenu is not aligned css html
I'm having problems with my submenu. What happens is the following:
My menu has 4 links in which one of these links presents a submenu with 3 links. What is happening is that my submenu, instead of showing the links stacked one below the other, they are next to each other. I want the submenu links to be stacked.
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@200&display=swap');
*{
box-sizing: border-box;
margin: 0;
padding: 0;
background-color: #FFFFFF;
}
.header {
background-color: #F1F1F1;
text-align: center;
padding: 20px;
}
li,a,button{
font-family: 'Nunito', sans-serif;
font-weight: 500;
font-size: 16px;
color:black;
text-decoration: none;
}
header{
display: flex;
justify-content: space-between;
align-items: center;
padding: 30px 10%;
}
.logo{
cursor: pointer;
}
.nav_links{
list-style: none;
}
.nav_links li{
display: inline-block;
padding: 0px 20px;
}
.nav_links li a{
transition: all 0.3s ease 0s;
}
.nav_links li a:hover{
color: #0088a9;
}
.nav_links li ul{
width: 200px;
text-align: center;
position: absolute;
background: #373C58;
padding: 0;
list-style-type: none;
box-shadow: 0px 0px 23px 0px rgba(0, 0, 0, 0.25);
margin-top: 10px;
border-radius: 7px;
display: none;
}
.nav_links li ul li{
padding: .5rem;
}
.nav_links li ul li a{
width: 100%;
height: 100%;
display: inline-block;
padding: 0;
margin: 0;
}
.nav_links li ul li:hover{
background: #272B3F;
border-radius: 7px;
}
.nav_links a:focus + ul,
.nav_links a:hover + ul,
.nav_links ul:hover{
display: block;
}
button{
padding: 9px 25px;
background-color: rgba(0, 136, 169, 1.0);
border: none;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease 0s;
}
button:hover{
background-color: rgba(0, 136, 169, 0.8);
}
<header>
<img class ="logo" src="icon.png" width="100" height="50" align="left">
<nav>
<ul class="nav_links">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li>
<a href="#">3</a>
<ul>
<li><a href="#">A</a></li>
<li><a href="#">B</a></li>
<li><a href="#">C</a></li>
</ul>
</li>
<li><a href="#">4</a></li>
</ul>
</nav>
<a class="login" href="#"><button>Login</button></a>
</header>
Also viewable in this fiddle
Solution 1:[1]
Since you have set .nav_links li { display: inline-block } all the li element inside .nav will inherit this attribute. In order to solve that you can do two things:
Instead apply the
display: inline-blockto all.nav_links lijust add a chevron between them to indicate that you just want thedisplay: inline-blockonly in the first element group. Like this:.nav_links > li { display: inline-block }.As @yainspan says just include the attribute
display: blockin the.nav_links li ul licss selector.
Personally I do prefer the first one in order to save time and effort searching every element inside the DOM tree. But both choices are good.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | marc_s |
