Maintain the color of a Bootstrap 4 dropdown link even when it is open by incorporating hover

Currently, I am utilizing bootstrap 4 to create a menu that includes dropdown items. The issue I am facing is that when hovering over a dropdown link, the background color changes and the dropdown opens. However, as soon as I move the mouse to select one of the dropdown items, the color reverts back to its original state. I am seeking a solution to keep the hover color persistent while the dropdown is open.

body {
  background-color: #091230;
}

.dropdown:hover .dropdown-menu {
  display: block;
}

.navbar-nav>li {
  margin-left: 10px;
  margin-right: 10px;
}

nav .navbar-nav li>a {
  color: white !important;
  -moz-border-radius: 0px;
  -webkit-border-radius: 5px 5px 0px 0px;
  border-radius: 5px 5px 0px 0px;
}

nav .navbar-nav li>a:hover {
  background-color: grey;
}

.navbar .dropdown-menu {
  background-color: grey;
  margin: 0;
  -moz-border-radius: 0px;
  -webkit-border-radius: 0px 5px 5px 5px;
  border-radius: 0px 5px 5px 5px;
  border-top-color: #343A40 !important;
}

.dropdown div {
  border-color: grey !important;
}

.navbar .dropdown-menu a {
  color: white !important;
}

.dropdown-item:hover {
  background-color: #343A40 !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand navbar-dark bg-dark">


  <ul class="navbar-nav">
    <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">
          Dropdown
        </a>
      <div class="dropdown-menu" aria-labelledby="navbarDropdown">
        <a class="dropdown-item" href="#">Item 1</a>
        <a class="dropdown-item" href="#">Item 2</a>
        <a class="dropdown-item" href="#">Item 3</a>
        <a class="dropdown-item" href="#">Item 4</a>
      </div>
    </li>
  </ul>

</nav>

Answer №1

To achieve the desired outcome, it is suggested to transfer the :hover state from the a element to the li element:

nav .navbar-nav li:hover > a {
  background-color: grey;
}

Furthermore, it is important to refrain from using !important whenever possible. It is considered best practice to override existing styling rules by enhancing the specificity of your selectors. Below is a complete working example:

body {
  background-color: #091230;
}

.dropdown:hover .dropdown-menu {
  display: block;
}

.navbar-nav>li {
  margin-left: 10px;
  margin-right: 10px;
}

#container nav .navbar-nav li>a {
  color: white;
  border-radius: 5px 5px 0px 0px;
}

nav .navbar-nav li:hover>a {
  background-color: grey;
}

#container .navbar .dropdown-menu {
  background-color: grey;
  margin: 0;
  border-radius: 0px 5px 5px 5px;
  border-top-color: #343A40;
}

#container .dropdown div {
  border-color: grey;
}

#container .navbar .dropdown-menu a {
  color: white;
}

#container .dropdown-item:hover {
  background-color: #343A40;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div id="container">
  <nav class="navbar navbar-expand navbar-dark bg-dark">
    <ul class="navbar-nav">
      <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">Dropdown</a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="dropdown-item" href="#">Item 1</a>
          <a class="dropdown-item" href="#">Item 2</a>
          <a class="dropdown-item" href="#">Item 3</a>
          <a class="dropdown-item" href="#">Item 4</a>
        </div>
      </li>
    </ul>
  </nav>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using AJAX to Capture PHP Error Responses

I have implemented a form where users can upload profile pictures and see live validation feedback without refreshing the page. I am currently using AJAX for this functionality, but it seems that the requests are not reaching the PHP file. Also, after subm ...

Is there a way to make the CSS3 border-image compatible with all the latest browsers, including IE 9+, Chrome, and Firefox?

Is there a way to implement border-image for a dynamically scaling CSS container across all modern browsers using a .js framework? If not, what would be a suitable alternative? To visualize the desired effect, check out: ...

The strict use of `use strict` can result in an undefined

I've been working on organizing my JavaScript file and decided to use a module pattern as suggested. However, I encountered an issue when using "use strict" in this pattern where a function is declared as undefined. Surprisingly, without the "use stri ...

Display information when hovering over a tag

I'm working on adding a feature where hovering over a link will display a tooltip. For reference, here is an example: https://i.stack.imgur.com/K84Wf.png Are there any alternative JavaScript libraries that offer this functionality? (ideally similar ...

Can anyone assist me with this HTML/jQuery code?

Despite my efforts, I've been unable to achieve success. The challenge I'm facing is with duplicating controls for adding images. I have a button and a div where the user can choose an image by clicking the button. The selected image appears in ...

How can I adjust the size and width of the autofocus cursor inside an input box using Angular?

How can I modify the height and width of the cursor in an input field with auto focus? app.component.html <input class="subdisplay" [ngModel]="input | number" type="text" (keyup)="getValue(box.value)" name ...

An overflow occurs due to the grid column gap

After testing CSS display: grid, I noticed that the grid-column-gap: 10px; property is causing the parent container to break. This makes the green area in my code smaller than the grid area itself. It seems like box-sizing: border-box; doesn't have a ...

Adjust the position of the content to the bottom for a form-group in Bootstrap v4

Currently, I am facing an issue where my label is overflowing and wrapping around the input field, causing it not to align at the bottom of the div. Is there a straightforward CSS solution for this problem? I have experimented with position relative/absol ...

I am having trouble with my scroll reveal effects on JavaScript, how can I troubleshoot and fix the issue?

I'm currently making updates to my portfolio website, but for some reason, the scroll reveal animation has suddenly stopped working. I made a few edits and now it seems to be broken. /*===== SCROLL REVEAL ANIMATION =====*/ const sr = ScrollReveal({ ...

Discovering modifications to CSS using selenium

Currently, I am working on an ajax form where CSS elements are dynamically changed. I am curious to know if it is feasible to verify these changes using Selenium. For example, checking if the background color changes from #ffffff to #000000 after clicking ...

What benefits does importing bootstrap.scss into styles.scss provide compared to adding it directly to the styles block in .angular.cli.json?

What are the advantages of importing bootstrap.scss into the styles.scss file compared to adding it to the .angular-cli.json styles block? Which approach is better and why? styles.scss @import '~bootstrap/scss/bootstrap.scss'; vs .angular-cl ...

Tips for changing between two texts within a button when clicked

Seeking a solution for toggling between two different texts inside a button when making an ajax call, with only one text displayed at a time. I'm facing difficulty in targeting the spans within the button specifically. Using 'this' as conte ...

Transmitting a pair of parameters in a solitary AJAX call

Currently, I have a webpage that retrieves user input from $_POST. Here is an example of how it looks: $login=$_POST['name']; Later on, upon clicking a button, it triggers an AJAX request as shown below: var id = $(this).val(); var d ...

Surprising behavior of translateZ in Framer Motion

Trying to position elements in a circular layout has led to unexpected behavior when using framer motion's translateZ. By applying styles with a template literal without shorthand for transforms, the desired effect is achieved: transform: ` rotateY( ...

Issues with CSS3 Animation failing to trigger upon hovering over links

Background: Looking to design a visually appealing hover effect for links in the future Current Demo Link: You can view the demo here specifically on Firefox Browser. body { color:#ffffff; font-family:"Helvetica"; font-size:12pt; backgr ...

Is there a way to prevent a 'keyup' event from being triggered by a 'keydown' event?

I have a tool that resolves zip codes and I am currently utilizing a keyup event handler to trigger a server query once the input length reaches 5 characters. However, I want to prevent unnecessary calls to the script, so I am exploring the possibility o ...

Displaying the Variable Product Options in WooCommerce

I have been working on creating a query to showcase products along with relevant information, but I am facing some challenges. Specifically, I am struggling with displaying the variable product options for all products and implementing an add to cart butt ...

What is the process of including a CSS class in a NAV anchor tag on WordPress?

I recently started using Bootstrap 4 and have structured my menu in the following way: <ul class="navbar-nav mx-auto justify-content-center"> <li class="nav-item"> <a class="nav-link" href="index.php">HOME</a> </ ...

Do AJAX forms require the use of a submit input?

Is it essential to include a submit input in my AJAX forms, given that: 1-)The forms are designed to function with AJAX, meaning users without JavaScript won't be able to use my app ...

When resizing, Bootstrap sidebar causes content overlapping

I am facing a problem that I am unable to resolve. The issue is with the top bar and side bar on my webpage. When the page is resized, the sidebar moves to an absolute position just below the top bar, causing it to overlap my content. Below is my CSS code ...