skip to Main Content

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


  1. I added this codes to your css :

    ul li {
    list-style-type:none
    }
    
    ul li a {
    text-decoration:none;
    padding:10px;
    }
    
    ul li a:hover {
    text-decoration:none;
    padding:10px;
    background: #000;
    transition: all 0.5s ease-out;
    color:#fff;
    }
    
    
    

    and deleted ul > li:hover class, added width:100vw for nav .Finally added justify-content:center; align-items:center; to nav .links class

    nav {
        width: 100vw;
        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;
        justify-content:center;
        align-items:center;
        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 {
    list-style-type:none
    }
    
    ul li a {
    text-decoration:none;
    padding:10px;
    }
    
    ul li a:hover {
    text-decoration:none;
    padding:10px;
    background: #000;
    transition: all 0.5s ease-out;
    color:#fff;
    }
    
    
    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>
    Login or Signup to reply.
  2. You’ll need to remove the padding: 5px;
    that you put in the ul>li :hover style first the ul element will stay still.

    Login or Signup to reply.
  3. The only problem was the padding: 5px ; on the hover in your CSS. You can see my version without the padding below.

    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;
        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>
    Login or Signup to reply.
  4. In your hover code, you add extra padding to the text, which is why this happens. Removing it will help you:

    ul > li:hover {
        background: #000;
        padding: 5px ;
        transition: all 0.2s ease;
    }
    

    To debug such an issue, you do the next:

    • Inspect the element (right click then select inspect)
      enter image description here
    • it will open a code editor in the browser. Select li (which is the one that you have the hover effect on it
      enter image description here
    • go to style, click on the :hov and choose hover to activate that effect
      enter image description here
    • You can see the CSS style in the browser and activate/deactivate what you prefer.
      enter image description here

    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:

    enter image description here

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search