Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change bootstrap 4 drop-down colors?

I am trying to change both the background, and font color of the bootstrap 4 dropdown navigation.

I tried to use

.nav.nav-tabs > li.dropdown.active.open > a, 
.nav.nav-tabs > li.dropdown.active.open > ul.dropdown-menu a:hover,
.nav.nav-tabs > li.dropdown.open > a, 
.nav.nav-tabs > li.dropdown.open > ul.dropdown-menu a:hover
{
  color: #fff;
  background-color: #b91773;
  border-color: #fff;
}

But this wasn't working too well for me. Here is my HTML:

      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true"
          aria-expanded="false">
  Dropdown link
</a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
like image 353
Temple Avatar asked Jun 18 '17 07:06

Temple


1 Answers

I know this is already answered, but since I'm bookmarking it, I want to give the solution that worked for me when theming Bootsrap using Sass and NPM.

Note that I include functions and variables above my custom ones because I need to access them otherwise the compilation will fail. Read more about it on this issue.

Suppose you have your _custom.scss like this:

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";

// Add your custom variables here

@import "~bootstrap/scss/bootstrap";

Then you can go to Bootstrap's main variables file and copy over the ones you want to overwrite.

For example, if I want my dropdown background to be dark with white links I'd do it like:

$dropdown-bg:                       $dark;
$dropdown-link-color:               $light;

And my _custom.scss file after the changes would like like this:

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";

// Add your custom variables here
$dropdown-bg:                       $dark;
$dropdown-link-color:               $light;

@import "~bootstrap/scss/bootstrap";

And this is an image of what it looks like after compiling Sass:

Bootstrap's dropdown with dark background

This way I don't overwrite CSS rules avoiding unnecessary clutter.

like image 51
jolvera Avatar answered Nov 04 '22 00:11

jolvera