英文:
How to change Bootstrap dropdown background color & color in CSS?
问题
以下是您提供的内容的翻译:
我一直在尝试改变下拉菜单的背景颜色和颜色,但没有成功(dropdown-menu和dropdown-item)。 现在下拉菜单的背景颜色非常浅灰色,每个项目的悬停具有中等浅灰色背景和黄色文字颜色,它们之间的对比度不高,我需要更改它们。
/* 导航栏 */
.navbar-nav {
font-weight: bold !important;
}
/* 导航栏背景颜色 */
.nav-item a,
.nav-item a:link,
.nav-item a:visited {
color: var(--bodyPage) !important;
}
/* 导航栏项目字体颜色 */
.nav-item a:hover,
.nav-item a:focus {
color: var(--linkHover) !important;
}
/* 活动导航栏项目字体颜色 */
.active a,
.nav-item .active a:link,
.active a:visited {
color: var(--navActive) !important;
}
/* 活动导航栏项目悬停字体颜色 */
.active a:hover,
.active a:focus {
color: var(--linkHover) !important;
}
/* 链接 */
a,
a:link,
a:visited {
color: var(--link);
}
a:hover,
a:focus {
color: var(--linkHover);
}
.site-footer a,
.site-footer a:link,
.site-footer a:visited {
color: var(--link);
}
/* 页脚链接颜色 */
.site-footer a:hover,
.site-footer a:focus {
color: var(--linkHover);
}
/* 页脚悬停链接颜色 */
.site-page a,
.site-page a:link,
.site-page a:visited {
color: var(--link);
}
/* 页面正文链接颜色 */
.site-page a:hover,
.site-page a:focus {
color: var(--linkHover);
}
/* 页面正文悬停链接颜色 */
.banner a,
.banner a:link,
.banner a:visited {
color: var(--link);
}
/* 横幅链接颜色 */
.banner a:hover,
.banner a:focus {
color: var(--linkHover);
}
/* 横幅悬停链接颜色 */
.no-banner a,
.no-banner a:link,
.no-banner a:visited {
color: var(--link);
}
/* 无横幅链接颜色 */
.no-banner a:hover,
.no-banner a:focus {
color: var(--linkHover);
}
/* 无横幅悬停链接颜色 */
.navbar-default .navbar-toggle {
background-color: red !important;
}
@media (max-width:992px) {
.navbar-nav {
background-color: black !important;
padding: 8px !important;
}
}
/* 下拉菜单 */
.dropdown-menu {
background-color: rgb(99, 97, 97) !important;
}
.dropdown-item a:hover {
background-color: rgb(0, 0, 0) !important;
}
希望这能帮助您更改下拉菜单的颜色和背景颜色。
英文:
I have been trying everything to change the background-color & color of the dropdown with no success (dropdown-menu & dropdown-item). Now the background color of the dropdown is very light grey and the hover on each item has a medium light grey background and a yellow color which are not very contrasted and I need to change them.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* navbar */
.navbar-nav {
font-weight: bold !important;
}
/* navbar bgn color */
.nav-item a,
.nav-item a:link,
.nav-item a:visited {
color: var(--bodyPage) !important;
}
/* navbar item font color */
.nav-item a:hover,
.nav-item a:focus {
color: var(--linkHover) !important;
}
/* navbar item hover font color */
.active a,
.nav-item .active a:link,
.active a:visited {
color: var(--navActive) !important;
}
/* active navbar item font color */
.active a:hover,
.active a:focus {
color: var(--linkHover) !important;
}
/* active navbar item hover font color */
/* links */
a,
a:link,
a:visited {
color: var(--link);
}
a:hover,
a:focus {
color: var(--linkHover);
}
.site-footer a,
.site-footer a:link,
.site-footer a:visited {
color: var(--link);
}
/* footer link color */
.site-footer a:hover,
.site-footer a:focus {
color: var(--linkHover);
}
/* footer hover link color */
.site-page a,
.site-page a:link,
.site-page a:visited {
color: var(--link);
}
/* page body link color */
.site-page a:hover,
.site-page a:focus {
color: var(--linkHover);
}
/* page body hover link color */
.banner a,
.banner a:link,
.banner a:visited {
color: var(--link);
}
/* banner link color */
.banner a:hover,
.banner a:focus {
color: var(--linkHover);
}
/* banner hover link color */
.no-banner a,
.no-banner a:link,
.no-banner a:visited {
color: var(--link);
}
/* no banner link color */
.no-banner a:hover,
.no-banner a:focus {
color: var(--linkHover);
}
/* no banner hover link color */
.navbar-default .navbar-toggle {
background-color: red !important;
}
@media (max-width:992px) {
.navbar-nav {
background-color: black !important;
padding: 8px !important;
}
}
/* dropdown menu */
.dropdown-menu {
background-color: rgb(99, 97, 97) !important;
}
.dropdown-item a:hover {
background-color: rgb(0, 0, 0) !important;
}
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<body data-spy="scroll" data-target=".site-navbar-target" data-offset="300">
<div class="site-wrap" id="home-section">
<header class="container-fluid sticky site-header" id="headerSite">
<div class="container">
<nav class="navbar navbar-expand-md navbar-light">
<div class="collapse navbar-collapse justify-content-between" id="navbarCollapse">
<div class="navbar-nav mr-auto">
<li class="nav-item"><a href="/dashboard" class="nav-link">Dashboard</a></li>
<div class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" id="dropdownNav" data-bs-toggle="dropdown">database</a>
<div class="dropdown-menu" aria-labelledby="dropdownNav">
<a href="..." class="dropdown-item">table1</a>
<a href="..." class="dropdown-item">table2</a>
<a href="..." class="dropdown-item">table3</a>
</div>
</div>
</div>
</div>
</nav>
</div>
</header>
<!-- end snippet -->
答案1
得分: 1
-
添加一个自定义的 CSS 文件(例如 style.css),并在 Bootstrap CSS 引用下方添加链接引用(例如
<link rel="stylesheet" href="path-to-your-css-file/style.css">
) -
为要修改的元素添加一个新的自定义类,并根据需求设置该类的颜色。
例如: <a href="#" class="nav-link dropdown-toggle custom-dropdown" id="dropdownNav" data-bs-toggle="dropdown">database</a>
- 对于下拉项也可以做同样的操作。
英文:
-
Add a custom css file (e.g. style.css) and add reference to it below your bootstrap css reference (e.g.
<link rel="stylesheet" href="path-to-your-css-file/style.css">
) -
Add a new class to custom class to the elements you want to modify and color that class as per your requirement.
e.g. <a href="#" class="nav-link dropdown-toggle custom-dropdown" id="dropdownNav" data-bs-toggle="dropdown">database</a>
- You can do the same for dropdown-item too.
答案2
得分: 0
看起来你有一些HTML问题,我甚至无法查看你示例中的下拉菜单。这种方式对我有效,我可以看到下拉菜单中的颜色变化。不清楚你确切想要什么颜色,但这可能符合你的需求。
/* 导航栏 */
.navbar-nav {
font-weight: bold !important;
}
/* 导航栏背景颜色 */
.nav-item a,
.nav-item a:link,
.nav-item a:visited {
color: var(--bodyPage) !important;
}
/* 导航栏项字体颜色 */
.nav-item a:hover,
.nav-item a:focus {
color: var(--linkHover) !important;
}
/* 导航栏项悬停字体颜色 */
.active a,
.nav-item .active a:link,
.active a:visited {
color: var(--navActive) !important;
}
/* 活动导航栏项字体颜色 */
.active a:hover,
.active a:focus {
color: var(--linkHover) !important;
}
/* 活动导航栏项悬停字体颜色 */
/* 链接 */
a,
a:link,
a:visited {
color: var(--link);
}
a:hover,
a:focus {
color: var(--linkHover);
}
.site-footer a,
.site-footer a:link,
.site-footer a:visited {
color: var(--link);
}
/* 页脚链接颜色 */
.site-footer a:hover,
.site-footer a:focus {
color: var(--linkHover);
}
/* 页脚悬停链接颜色 */
.site-page a,
.site-page a:link,
.site-page a:visited {
color: var(--link);
}
/* 页面链接颜色 */
.site-page a:hover,
.site-page a:focus {
color: var(--linkHover);
}
/* 页面悬停链接颜色 */
.banner a,
.banner a:link,
.banner a:visited {
color: var(--link);
}
/* 横幅链接颜色 */
.banner a:hover,
.banner a:focus {
color: var(--linkHover);
}
/* 横幅悬停链接颜色 */
.no-banner a,
.no-banner a:link,
.no-banner a:visited {
color: var(--link);
}
/* 无横幅链接颜色 */
.no-banner a:hover,
.no-banner a:focus {
color: var(--linkHover);
}
/* 无横幅悬停链接颜色 */
.navbar-default .navbar-toggle {
background-color: red !important;
}
@media (max-width: 992px) {
.navbar-nav {
background-color: black !important;
padding: 8px !important;
}
}
/* 下拉菜单 */
.dropdown-menu {
background-color: rgb(99, 97, 97) !important;
}
.dropdown-item a:hover {
background-color: rgb(0, 0, 0) !important;
}
英文:
It looks like you've got some HTML issues, and I wasn't able to even view the dropdown menu in your example. This way works for me, and I can see the color changed in the dropdown menu. It's not clear what colors you want exactly, but this might get you there.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
/* navbar */
.navbar-nav {
font-weight: bold !important;
}
/* navbar bgn color */
.nav-item a,
.nav-item a:link,
.nav-item a:visited {
color: var(--bodyPage) !important;
}
/* navbar item font color */
.nav-item a:hover,
.nav-item a:focus {
color: var(--linkHover) !important;
}
/* navbar item hover font color */
.active a,
.nav-item .active a:link,
.active a:visited {
color: var(--navActive) !important;
}
/* active navbar item font color */
.active a:hover,
.active a:focus {
color: var(--linkHover) !important;
}
/* active navbar item hover font color */
/* links */
a,
a:link,
a:visited {
color: var(--link);
}
a:hover,
a:focus {
color: var(--linkHover);
}
.site-footer a,
.site-footer a:link,
.site-footer a:visited {
color: var(--link);
}
/* footer link color */
.site-footer a:hover,
.site-footer a:focus {
color: var(--linkHover);
}
/* footer hover link color */
.site-page a,
.site-page a:link,
.site-page a:visited {
color: var(--link);
}
/* page body link color */
.site-page a:hover,
.site-page a:focus {
color: var(--linkHover);
}
/* page body hover link color */
.banner a,
.banner a:link,
.banner a:visited {
color: var(--link);
}
/* banner link color */
.banner a:hover,
.banner a:focus {
color: var(--linkHover);
}
/* banner hover link color */
.no-banner a,
.no-banner a:link,
.no-banner a:visited {
color: var(--link);
}
/* no banner link color */
.no-banner a:hover,
.no-banner a:focus {
color: var(--linkHover);
}
/* no banner hover link color */
.navbar-default .navbar-toggle {
background-color: red !important;
}
@media (max-width:992px) {
.navbar-nav {
background-color: black !important;
padding: 8px !important;
}
}
/* dropdown menu */
.dropdown-menu {
background-color: rgb(99, 97, 97) !important;
}
.dropdown-item a:hover {
background-color: rgb(0, 0, 0) !important;
}
<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.3/dist/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<header class="site-wrap" id="home-section">
<header class="container-fluid sticky site-header" id="headerSite">
<div class="container">
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item"><a href="/dashboard" class="nav-link">Dashboard</a></li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
database
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">table1</a>
<a class="dropdown-item" href="#">table2</a>
<a class="dropdown-item" href="#">table3</a>
</li>
</ul>
</div>
</nav>
</div>
</header>
<!-- end snippet -->
答案3
得分: 0
以下是您的解决方案:
现在已成功更改
我解释了我的代码注释,请检查
请使用以下代码添加到您的CSS文件或样式标签中,然后下拉菜单将更改样式:
.dropdown {
background-color: aqua !important;
/* 这是下拉菜单的背景颜色 */
}
.dropdown a {
color: whitesmoke !important;
/* 这是下拉菜单文字颜色 */
}
.dropdown-menu {
background-color: brown !important;
/* 这是下拉菜单背景颜色 */
}
a.dropdown-item {
color: blueviolet !important;
/* 这是下拉菜单文字颜色 */
}
a.dropdown-item:hover {
background-color: chocolate;
color: seagreen !important;
/* 这是下拉菜单悬停时的背景和文字颜色 */
}
同时,确保在HTML中添加以下引用:
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet">
这将使下拉菜单样式更改生效。
英文:
Here is your solution...
now it change successfully
i explain my to the comment of the code check please
use this code to your css file or in the style tag then the dropdown will be changed...
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.dropdown {
background-color: aqua !important;
/* this is for dropdown background */
}
.dropdown a {
color: whitesmoke !important;
/* this is for dropdown text color */
}
.dropdown-menu {
background-color: brown !important;
/* this is for dropdown menu background */
}
a.dropdown-item {
color: blueviolet !important;
/* this is for dropdown menu text color */
}
a.dropdown-item:hover {
background-color: chocolate;
color: seagreen !important;
/* this is for dropdown menu hover background and text color */
}
<!-- language: lang-html -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-expanded="false">
Dropdown
</a>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="#">Action</a></li>
<li><a class="dropdown-item" href="#">Another action</a></li>
<li>
<hr class="dropdown-divider">
</li>
<li><a class="dropdown-item" href="#">Something else here</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
</div>
</nav>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论