Bootstrap下拉菜单标题hover变蓝,如何修改该样式?
Bootstrap下拉菜单标题hover时仍变蓝的问题解决
我用Bootstrap框架开发时,鼠标悬停下拉菜单标题(即触发下拉的"Name"链接)时,标题会变成蓝色。目前已经成功修改下拉菜单项hover不再变蓝,但标题的hover变色效果始终无法调整,试了网上多种解决方案都无效。
当前效果:
现有代码
CSS代码
.header { position: fixed; top: 0; left: 0; right: 0; z-index: 999; } .top-navbar { background-color: #333; height: 40px; } .top-navbar-container { max-width: 1200px; margin: 0 auto; display: flex; justify-content: flex-end; align-items: center; height: 100%; } .top-menu { list-style: none; display: flex; margin: 0; padding: 0; } .top-menu li { margin-right: 0px; } .top-menu li:last-child { margin-right: 0; } .top-menu li a { text-decoration: none; color: #fff; font-size: 12px; font-weight: 600; } .top-menu li a:hover { color: #fff; } .top-menu .dropdown-item { color: #333; /* Grey text color for the dropdown items */ } .top-menu .dropdown-item:hover { color: #333; /* Grey text color for the dropdown items */ } .main-navbar { background-color: #fff; height: 80px; box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.1); } .main-navbar-container { display: flex; justify-content: space-between; align-items: center; height: 100%; max-width: 1200px; margin: 0 auto; padding: 0 20px; } .logo-container { width: 80px; } .logo { width: 100%; height: 100%; } .menu-container { display: flex; align-items: center; } .menu { list-style: none; display: flex; margin: 0; padding: 0; } .menu li { margin-right: 20px; } .menu li:last-child { margin-right: 0; } .menu li a { text-decoration: none; color: #333; font-weight: 600; } .icons { display: flex; align-items: center; margin-left: 20px; } .cart-icon { margin-right: 20px; width: 100px; height: 100px; } .logo-container { width: 78px; height: 100%; display: flex; align-items: center; } .logo { width: 100%; height: auto; } .dropdown-item:hover, .dropdown-item:focus { background-color: transparent; /* Set the background color to transparent */ color: inherit; /* Inherit the text color from the parent element */ } .top-menu li .dropdown-item a:hover { color: #fff; }
HTML代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" /> </head> <body> <header class="header"> <nav class="top-navbar" data-bs-theme="dark"> <div class="top-navbar-container"> <ul class="top-menu"> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-bs-toggle="dropdown" aria-expanded="false"> Name </a> <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <li><a class="dropdown-item" href="#">Action</a></li> <li><a class="dropdown-item" href="#">Another action</a></li> <li><a class="dropdown-item" href="logout.php">Logout</a></li> <li class="nav-item"> <a class="nav-link" href="register.php">Register</a> </li> <li class="nav-item"> <a class="nav-link" href="login.php">Login</a> </li> </ul> <div class="icons"> <a href="cart.html"><i class="fa-solid fa-cart-shopping fa-xl" style="color: #0000ff;"></i></a> </div> </li> </div> </nav> <nav class="main-navbar"> <div class="main-navbar-container"> <div class="logo-container"> <img src="Noah Grocery Logo.png" alt="Grocery Store Logo" class="logo"> </div> <div class="menu-container"> <ul class="menu"> <li><a href="index.html">Home</a></li> <li><a href="shop.html">Shop</a></li> <li><a href="support/support.html">Support</a></li> </ul> </div> </div> </nav> </header> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js"></script> </body> </html>
解决方案
问题根源是Bootstrap默认的.nav-link:hover样式优先级高于你自定义的样式,导致修改不生效。添加以下CSS代码即可解决:
/* 覆盖下拉标题的hover样式,保持白色不变 */ .top-menu .nav-item.dropdown .nav-link.dropdown-toggle:hover, .top-menu .nav-item.dropdown .nav-link.dropdown-toggle:focus { color: #fff !important; }
额外提示:你HTML里用了data-bs-theme="dark",这是Bootstrap 5的属性,而你加载的是Bootstrap 4,这个属性在Bootstrap 4里不生效,可以删掉避免混淆。
内容的提问来源于stack exchange,提问作者Random Coder 16
相关产品推荐
相关产品推荐

