TripVik
TripVik

Reputation: 47

How to change the color of selected dropdown in bootstrap navbar?

Whenever a dropdown item is clicked its color changes to white that makes the white text unreadable.

I've already tried this but the solution doesn't work for me. Here is my snippet

        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">Demo</a>
            </div>
            <div class="collapse navbar-collapse" id="myNavbar">
                <ul class="nav navbar-nav">

                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#">Applications<span class="caret"></span></a>
                        <ul class="dropdown-menu dropdown-menu-left">
                            <li><a href="#">Communications</a></li>
                            <li><a href="#">Environmental</a></li>
                            <li><a href="#">General</a></li>
                            <li><a href="#">Programmable</a></li>
                        </ul>
                    </li>
                    <li><a href="#">Product Selector</a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <form class="navbar-form" role="search">
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="Search" style="background-color:#000;margin-top:15px; border-radius: 0">
                                <div class="input-group-btn">
                                    <button class="btn btn-default" type="submit" style="background-color:#000;margin-top:15px;border-radius: 0"><i class="glyphicon glyphicon-search" style="color:#fff;"></i></button>
                                </div>
                            </div>
                        </form>
                    </li>
                    <li><a href="#"><i class="glyphicon glyphicon-shopping-cart logo-small"></i></a></li>
                    <li class="dropdown">
                        <a class="dropdown-toggle" data-toggle="dropdown" href="#"><i class="glyphicon glyphicon-user logo-small"></i></a>
                         <ul class="dropdown-menu dropdown-menu-right">
                            <li><a asp-area="Identity" asp-page="/Account/Login">Login</a></li>
                            <li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

I want the background of the active selection to be the same color as the dropdown item background. Thanks in advance!

Upvotes: 0

Views: 731

Answers (2)

hungerstar
hungerstar

Reputation: 21725

Bootstrap applies focus classes. You will need to override those as well.

.dropdown-menu>li>a:focus,
.dropdown-menu>li>a:hover {
  /* Your styles here. */
}

But you've done it with the following CSS selector, .nav.navbar-nav li a:hover. Add to the selector you already have (if you want the same styles for hover and focus) for :focus.

.nav.navbar-nav li a:hover,
.nav.navbar-nav li a:focus {
  /* Your existing styles. */
}

FWIW, the link to the other StackOverflow post you have is related to the Navbar, not Dropdowns.

Upvotes: 1

Adrift
Adrift

Reputation: 59859

This is the relevant selector. Change it to whichever color you need. Updated example

.dropdown-menu > li > a:focus {
    background-color: initial; /* Equivalent to background-color: transparent */
}

Upvotes: 2

Related Questions