I’m creating my first navigation bar but it’s not working expected. How can I remain the main UL element and not increase their sizing if I want to hover the li element?
I tried to add in ul li a margin:0
but still, it didn’t work.
I wanted to have different styling when I hover those li, but the blue background changed as well, what I wanted is to remain the .links as is so that only the change will take effect on the li when I hover it.
nav {
width: 100%;
height: 60px;
max-width: 1200px;
margin: 0 auto;
display: flex;
align-items: center;
justify-content: space-between;
}
li {
list-style: none;
}
nav .logo a {
font-size: 1.5rem;
font-weight: bold;
text-transform: uppercase;
}
nav .links {
display: flex;
gap: 2rem;
background: #00ABE4 ;
padding: 10px 20px;
}
nav .toggle-btn {
color: #00ABE4 ;
font-size: 1.5rem;
cursor: pointer;
display: none;
}
.nav-btn {
background: #00ABE4 ;
color: #fff;
padding: 0.5rem 1rem;
border: none;
outline: none;
font-size: 0.8rem;
font-weight: bold;
cursor: pointer;
transition: scale 0.2 ease-in-out;
}
.nav-btn:hover {
scale: 1.05;
color: #fff;
}
ul > li:hover {
background: #000;
padding: 5px ;
transition: all 0.2s ease;
}
nav .nav-btn:active {
scale: 0.95;
}
<header>
<nav>
<div class="logo">
<a href="#">Jury Mini</a>
</div>
<ul class="links">
<li><a href="hero">Home</a></li>
<li><a href="">About</a></li>
<li><a href="hero">Services</a></li>
<li><a href="hero">Contact</a></li>
</ul>
<a href="#" class="nav-btn">Get Started</a>
<div class="toggle-btn"><i class="fa-solid fa-bars"></i></div>
</nav>
</header>
4
Answers
I added this codes to your css :
and deleted
ul > li:hover
class, addedwidth:100vw
fornav
.Finally addedjustify-content:center;
align-items:center;
tonav .links
classYou’ll need to remove the
padding: 5px;
that you put in the
ul>li :hover
style first theul
element will stay still.The only problem was the
padding: 5px ;
on the hover in your CSS. You can see my version without the padding below.In your hover code, you add extra padding to the text, which is why this happens. Removing it will help you:
To debug such an issue, you do the next:
The padding property provides a padding area for your element, which is the letting the whole banner expand and is the reason causing this issue:
Here you can find further information about how to use padding and few example from MDN:
https://developer.mozilla.org/en-US/docs/Web/CSS/padding