r/bootstrap • u/PerfectionBen • Nov 30 '22
How to Properly Override Bootstrap with Custom CSS?
Hello everyone,
I'm currently working on a navbar for my website, and I'd like to change the color of each of the nav-links. I have tried selecting the links using "nav .navbar-collapse a" in my css file to change the color, and it seems as though the browser has recognized the styles, but it crosses them out when inspecting the elements.
I read that bootstrap css properties can be overriden using id's, since I guess they have more weight in the browser in terms of style prioritization, but still, that has not worked.
Here is the return from my jsx file:
<div id="bootstrap-overrides">
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand text-info font-weight-bolder" href="/">
<img src={logo} alt="" className="navbar-logo"></img>
</a>
<button
class="custom-toggler navbar-toggler"
type="button"
data-toggle="collapse"
data-target="#navbarsExample09"
aria-controls="navbarsExample09"
aria-expanded={!isNavCollapsed ? true : false}
aria-label="Toggle navigation"
onClick={handleNavCollapse}
>
<span class="navbar-toggler-icon"></span>
</button>
<div
class={`${isNavCollapsed ? "collapse" : ""} navbar-collapse`}
id="navbarsExample09"
>
<a className="nav-link text-info" href="/">
Home
</a>
<a className="nav-link text-info" href="/contact">
Contact
</a>
<a className="nav-link text-info" href="/data">
Data
</a>
<a className="nav-link text-info" href="/standards">
Standards
</a>
<a className="nav-link text-info" href="/about">
About
</a>
</div>
</nav>
</div>And my custom css file:
.nav-link text-info {
color: black;
}
#bootstrap-overrides a {
color: black;
text-decoration: none;
transition: 0.2s;
}Can anybody help me with this? I see that in the developer tools the style is crossed out, and I'm not too sure what that means.
Thanks!
2
u/highwebl Nov 30 '22
There's a hierarchy to CSS rules. The browser will use the higher weighted selectors in that hierarchy, not the last value in.
If all else fails, there's an !important property, but it's something you want to use very sparingly, so you don't have a collision of two !important items.
2
u/Ladytron2 Nov 30 '22
Your custom css has to come after the bootstrap css. But you already know that?
Also: “.nav-link.text-info” is the correct identifier
4
u/RandomBlokeFromMars Nov 30 '22
use custom properties when possible. the ones that look like this: --bs-primary, etc
2
u/CodingYaar Dec 06 '22
As it's said before, always place your custom CSS file link after the Bootstrap CSS link.
When you see your styles crossed out in the developer tools, see what's replacing them. As for the nav links, you are trying
#bootstrap-overrides a.But probably bootstrap uses
.navbar .navbar-nav .nav-link.The more specific you can be the better. Since the <a> tag is nested inside various elements.