Navbar hovering font color in listed items

I have a navbar on a website, of which I want the background color to be yellow and text black. And when having a mouse over an item, the background would be black and the text yellow.

My problem is that the text is returning to black when moving the mouse outside of the text, but still within the boxed “li”-item, resulting in black background and black text. This causes problems for example where hovered dropdown title (“MORE”) turns to black, and the background also remains in black when moving the mouse onto the listed items (see picture).

I know it’s because of “a = black” and “a:hover = yellow”, so when the mouse is off the text (“a”), it returns to black, but I can’t get my head around on how to have the “a” remain yellow inside the hovered “li” item, even though the mouse is not on top of the text.

Note: I’m also using Spynav and StickNav on the website, which is the reason for the structure of my code. I just left them out to be more clear to read

Code:

<nav class="navbar">
    <ul class="nav">
        <li><a href="#">HOME</a></li>
        <li><a href="#">CONTACT</a></li>
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown">MORE</a>
            <ul class="dropdown-content">
                <li><a href="discography">DISCOGRAPHY</a></li>
                <li><a href="gallery">GALLERY</a></li>
                <li><a href="videos">VIDEOS</a></li>
            </ul>
        </li>
    </ul>
</nav>
.nav {
    background-color: yellow;
}

.nav a{
    color: black;
}

.nav a:hover{
    color:yellow;
}

.nav li:hover{
    background-color: black;
}

/* DROPDOWN MENU */
.dropdown-content {
    background-color: yellow;
    display: none;
    position: absolute;
    min-width: auto;
}

.dropdown:hover .dropdown-content {
    display: block;
}

I have tried having the text color of hovered li-item to be yellow, and background black, as I’d love to think it would work like this.

.nav li:hover{
    color:yellow !important;
    background-color: black;
}

New contributor

pineapple is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Change the selector .nav a:hover into .nav li:hover > a

Then each hovered menu item becomes yellow-on-black, and the items in the sub-menu stay black-on-yellow, until they themselves also get hovered. (That’s how you want it, right?)

.nav {
    background-color: yellow;
}

.nav a{
    color: black;
}

.nav li:hover > a{
    color:yellow;
}

.nav li:hover{
    background-color: black;
}

/* DROPDOWN MENU */
.dropdown-content {
    background-color: yellow;
    display: none;
    position: absolute;
    min-width: auto;
}

.dropdown:hover .dropdown-content {
    display: block;
}
<nav class="navbar">
    <ul class="nav">
        <li><a href="#">HOME</a></li>
        <li><a href="#">CONTACT</a></li>
        <li class="dropdown">
            <a class="dropdown-toggle" data-toggle="dropdown">MORE</a>
            <ul class="dropdown-content">
                <li><a href="discography">DISCOGRAPHY</a></li>
                <li><a href="gallery">GALLERY</a></li>
                <li><a href="videos">VIDEOS</a></li>
            </ul>
        </li>
    </ul>
</nav>

 body {
  background: #6e28d9;
  padding: 0 24px;
  margin: 0;
  height: 100vh;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
.nav {
    background-color: yellow;
    padding: 0;
    margin: 0;
    list-style: none;
}

.nav li {
    display: inline-block;
}

.nav a {
    display: block;
    padding: 10px 20px;
    color: black;
    text-decoration: none;
}


.nav a:hover,
.nav li:hover > a {
    color: yellow;
    background-color: black;
}

   
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: yellow; 
    min-width: 160px; 
    padding: 0;
    margin: 0;
    list-style: none;
    z-index: 1;
}

.dropdown-content a {
    color: black; /* Text color of the dropdown items */
    padding: 10px 20px;
    display: block;
    text-decoration: none;
}


.dropdown-content a:hover {
    color: yellow; /* Text color when hovered */
    background-color: black; 
}

   
.dropdown:hover .dropdown-content {
    display: block;
}


.dropdown:hover .dropdown-content {
    background-color: yellow;
}

html:

   <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0">
        <link rel="stylesheet" href="src/style.css">
      </head>
      <body>
        
        <nav class="navbar">
        <ul class="nav">
            <li><a href="#">HOME</a></li>
            <li><a href="#">CONTACT</a></li>
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">MORE</a>
                <ul class="dropdown-content">
                    <li><a href="discography">DISCOGRAPHY</a></li>
                    <li><a href="gallery">GALLERY</a></li>
                    <li><a href="videos">VIDEOS</a></li>
                </ul>
            </li>
        </ul>
    </nav>
    
      </body>
    </html>

This is the fixed CSS. This works for me

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật