I have a nav which has a number of menu items. The text in each menu item varies in length.
Here's how it's looking:
nav ul {
display: flex;
justify-content: space-between;
align-items: center;
text-align: center;
margin: 0;
padding: 0;
}
nav ul li {
background: darkblue;
list-style: none;
padding: 24px;
}
a {
color: #fff;
text-decoration: none;
}
<nav>
<ul>
<li><a href="#">Menu Item</a></li>
<li><a href="#">A Longer Menu Item</a></li>
<li><a href="#">A Very Very Long Menu Item</a></li>
<li><a href="#">Menu Item</a></li>
</ul>
</nav>
The problem here is that it leaves large gaps between each item when viewport is wide. To fix this, I've added width: 100%;
to the <li>
, like so:
nav ul {
display: flex;
justify-content: space-between;
align-items: center;
text-align: center;
margin: 0;
padding: 0;
}
nav ul li {
width: 100%;
border-right: 3px solid #fff;
background: darkblue;
list-style: none;
padding: 24px;
}
a {
color: #fff;
text-decoration: none;
}
<nav>
<ul>
<li><a href="#">Menu Item</a></li>
<li><a href="#">A Longer Menu Item</a></li>
<li><a href="#">A Very Very Long Menu Item</a></li>
<li><a href="#">Menu Item</a></li>
</ul>
</nav>
This kind of solves the problem, but now the heights are not equal. Is there a way within flexbox to fix this?