'Bootstrap 4 collapsed navbar background color
I want to change background color of collapsed navbar in bootstrap 4 alpha v6. Of course I want to change the background color of whole navbar.
So the question is any class responsible for navbar collapsing? The only div I found so far is #navbarSupportedContent
but it's only the content of ul in navbar (so it does not change the color of whole navbar). I can check in jquery if the navbar is collapsed then change the background color, but I would like to know if I can do it simply in css.
In addition to that I would like to say that the navbar default color is transparent, that's why I want to change it's color when it's collapsed.
I have also next question what's the class responsible for navbar toggler icon in BS4? I'm asking because I would like to change the color of the icon. I've already found and tried the following:
.navbar-toggler-icon {
color:white !important;
}
Solution 1:[1]
Bootstrap v5 update
Here's a Bootstrap 5 CSS walkthrough covering modifications of .navbar
itself, inside links and hamburger button (all avoiding !important
which should be used only as last override resort):
/* change navbar background */
nav.navbar {
background: transparent;
}
/* change navbar-brand color */
.navbar a.navbar-brand {
color: white;
}
/* change navbar-brand color on hover */
.navbar a.navbar-brand:hover {
color: green;
}
/* change navbar li colors, also active one but not disabled one */
.navbar ul.navbar-nav li.nav-item a.nav-link {
color: white;
}
/* change navbar-toggler inside lines color (stroke) */
.navbar-light span.navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='yellow' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
color: white;
}
/* change hamburger button border color */
.navbar-light button.navbar-toggler {
border-color: yellow;
}
/* change navbar background on collapse */
@media (max-width: 768px) {
nav.navbar {
background: lightgray;
}
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<nav class="navbar navbar-expand-lg navbar-light">
<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">
<a class="nav-link disabled">Disabled</a>
</li>
</ul>
<form class="d-flex">
<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>
v4 original answer.
/* change navbar background */
nav.navbar {
background: transparent;
}
/* change navbar-brand color */
.navbar a.navbar-brand {
color: white;
}
/* change navbar-brand color on hover */
.navbar a.navbar-brand:hover {
color: green;
}
/* change navbar li colors, also active one but not disabled one */
.navbar ul.navbar-nav li.nav-item a.nav-link {
color: white;
}
/* change navbar-toggler inside lines color (stroke) */
.navbar-light span.navbar-toggler-icon {
background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='yellow' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
color: white;
}
/* change hamburger button border color */
button.navbar-toggler.navbar-toggler-right {
border-color: yellow;
}
/* change navbar background on collapse */
@media (max-width: 768px) {
nav.navbar {
background: lightgray;
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<nav class="navbar navbar-toggleable-md navbar-light">
<button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<a class="navbar-brand" href="#">Navbar</a>
<div class="collapse navbar-collapse" id="navbarTogglerDemo02">
<ul class="navbar-nav mr-auto mt-2 mt-md-0">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
Solution 2:[2]
"...but the case is that my background color at the beginning is transparent and then if someone will collapse it then the background color should change."
I did like this:
<style>
.navbar {
background-color: transparent;
}
.navbar.collapse {
display:flex;
}
.navbar.collapsing,
.navbar.show {
background:white !important;
}
</style>
<nav class="navbar navbar-light navbarSupportedContent">
<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 navbarSupportedContent">
</div>
</nav>
Solution 3:[3]
This is what worked form me:
.navbar-collapse {
background-color: white;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | gest |
Solution 3 | Bonfix Ngetich |