How can you modify the background color of a selected or hovered Bootstrap 4.6 select option?

When using Bootstrap 4.6, all my form select dropdowns seem to be displaying with a blue background like in this image:

https://i.sstatic.net/Vqsxw.png

In my .scss file, I have set it up as follows:

// Custom Variables

@import '../../vendor/bootstrap-4.6.0/scss/functions';
@import '../../vendor/bootstrap-4.6.0/scss/variables';

// Bootstrap Overrides
$primary: #6b37c4;
$link-color: $primary;
$link-hover-color: darken($primary, 10%);
$WHAT-GOES-HERE-BG?: $primary; // <-- what goes here?

The issue at hand:

I am wondering if there is a specific Bootstrap 4.6 variable that can be modified to change the blue background color to something else universally.

If yes, which variable name(s) should be changed? I attempted changing variables like custom-select-bg, custom-select-background, form-select-bg, but none yielded the desired results.

If not, is there an alternative method to alter this blue background for Bootstrap 4.6? I lack knowledge about browser compatibility, however, I prefer a long-lasting solution. Various suggestions on html - Change select list option background color on hover were outdated or ineffective.

An example to illustrate:

I expected the select option's background color to switch to yellow upon hovering, but it remained unchanged. Furthermore, I am unsure how to prevent the menu from closing so I could examine the CSS! It's becoming a bit frustrating.

#mySelectDiv {
  max-width: 300px;
  padding: 1rem;
}

option:hover {
  background-color: yellow;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="43212c2c37303731223303776d756d71">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div class="my-3" id="mySelectDiv">
  <select class="custom-select" id="myCustomSelect">
    <option selected>Open this select menu</option>
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
  </select>
</div>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7c160d09190e053c4f5249524d">[email protected]</a>/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0809f80809582de9a83b0c1dec1c6dec1">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff9d90908b8c8b8d9e8fbfcbd1c9d1cd">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-+sLIOodYLS7CIrQpBjl+C7nPvqq+FbNUBDunl/OZv93DB7Ln/533i8e/mZXLi/P+" crossorigin="anonymous"></script>

Answer №1

My suggestion is to avoid custom styling for better results.


If you choose not to follow the first recommendation, my second suggestion would be to utilize a reliable select plugin that already incorporates accessibility features and offers styling options for DOM elements. These plugins do not use <select> under the hood but rather generic DOM elements that can be styled easily.


Creating your own solution is not particularly difficult, but it's not the most ideal approach due to the complexity of dropdowns or selects as UI components. For instance, when dealing with the .dropdown-menu, it often needs to handle overflow within containers with overflow: hidden to prevent unexpected scrollbars from appearing. This can pose some challenges.

If you're interested, here's a customized solution:

$('.dropdown-menu a').click(e => {
  $('.dropdown-item', e.target.closest('.dropdown')).removeClass('active');
  mySelect.value = e.target.innerText
  $(e.target).addClass('active')
  mySelect.focus()
})
#mySelect {
  background-color: #fff !important;
  margin-right: -1.5rem;
  padding-right: 1.5rem;
}
#mySelect:focus {
  border-color: #6b37c4;
  box-shadow: 0 0 0 0.2rem rgb(107 35 196 / 25%);
}

.dropdown-toggle {
  display: flex;
  align-items: center;
}
.dropdown .dropdown-menu {
  box-shadow: 0 1px 5px 0 rgb(0 0 0 / 7%), 0 2px 2px 0 rgb(0 0 0 /5%), 0 3px 1px -2px rgb(0 0 0 / 4%);
  border: 1px solid rgb(0 0 0 / 7%);
}
.dropdown-menu .dropdown-item:hover,
.dropdown-menu .dropdown-item:active,
.dropdown-menu .dropdown-item:focus {
  background-color: rgb(107 35 196 / 10%);
  color: black;
}
.dropdown-menu .dropdown-item.active {
  background-color: #6b37c4;
  color: white;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fe9c91918a8d8a8c9f8ebecad0c8d0cc">[email protected]</a>/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

<div style="max-width: 200px">
  <div class="m-3 position-relative dropdown">
    <div class="dropdown-toggle" data-toggle="dropdown">
      <input readonly value="" class="form-control" placeholder="Open this select menu" id="mySelect" />
    </div>
    <div class="dropdown-menu w-100">
      <a class="dropdown-item" href="#">1</a>
      <a class="dropdown-item" href="#">2</a>
      <a class="dropdown-item" href="#">3</a>
    </div>
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd978c88988f84bdced3c8d3cc">[email protected]</a>/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b9c9d6c9c9dccb97d3caf98897888f9788">[email protected]</a>/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5979a9a818681879485b5c1dbc3dbc7">[email protected]</a>/dist/js/bootstrap.min.js"></script>

This is intended as a demonstration. Feel free to test it across various browsers and operating systems or enhance it further, but note that I may not provide updates for specific edge cases.

If you decide to create your own solution, here are some things to consider:

  • Implement keyboard navigation for opening/closing.
  • Include appropriate role and aria attributes for accessibility.
  • Ensure proper rendering on all mobile devices (iOS, Android) by testing on real devices or services like BrowserStack, as emulators may not reflect actual behavior accurately.

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

Having trouble getting the HTML input textbox onChange event to fire properly?

This is the code I have been working on: <script language="JavaScript"> function toggle() { if (this.value=='1') { document.getElementById('dbOn').style.visibility='visible'; } el ...

Issues with CSS and Node Express

I'm currently experimenting with some code and running into a problem. Using Express, I noticed that only the CSS selector for the body element is working. However, if I remove the body tag, then the next container works, but the ones after that do n ...

Nav Bar Toggle Button - Refuses to display the dropdown menus

My Django homepage features a Bootstrap navbar that, when resized, displays a toggle button. However, I am experiencing difficulty with dropdown functionality for the navbar items once the toggle button is activated. Can anyone provide guidance on how to a ...

How can you animate the background of a website using AngularJS - CSS or JavaScript?

My aim is to create a dynamic animation for the background image when the view changes. The current background image is set through a function defined within MainController: // app/js/controllers.js $scope.getBg = function() { return $route.current.sco ...

How to eliminate arrow icons from Bootstrap Carousel

Welcome to my blog at www.lowcoupling.com. I am trying to hide the left and right arrows on the bootstrap carousel, but my code doesn't seem to be working. Here is what I have tried: .glyphicon-chevron-right{ display:none; } (I also tried the ...

Retrieve the file path of an uploaded image in a Razor view using jQuery Ajax

I have been facing challenges while working on an ASP.NET MVC3 application where I am developing a custom image gallery. The browser being targeted is IE9, which rules out the option of using HTML5 File API. All my code resides within a strongly typed Razo ...

The reasons behind the unsightly gaps in my table

Here is the HTML code snippet: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML> <HEAD> <script type='text/javascript' src='js/jquery-1.10.2.min.js'></ ...

Guide to setting up the primary navigation in the WordPress administration panel

I recently started using WordPress and I'm struggling with changing the main menu href category. I've tried looking for the header in the FTP files, but I still can't figure out how to do it. I've read through all of the WordPress help ...

Sending documents to a printer directly from a Rails application

Is there a library or gem in Rails that can be used to print the contents of a web page directly onto paper? I am curious if it's possible to specify only a specific part of the page, such as a div, for printing. Any guidance, advice, or tutorial link ...

Struggling to retrieve a class within an <a> element in jsTree when hovering?

I'm currently utilizing jsTree and below is how I'm initializing it... function setupJSTree(data){ $("#treeSelector").jstree({ "plugins" : ["themes","json_data","UI","types"], "themes" : { "theme":"def ...

Title size in css

<div class="products_list products_slider"> <ul> <li> <a id="ContentPlaceHolder1_rptCategoryProducts_hlProductImg_0" class="product_image" href="/Urun/Oyuncak/woodoy-100-parca-kovali-ahsap-oyuncak-bloklar/7078"> <div class="produ ...

Combining selected boxes through merging

Looking to create a simple webpage with the following requirements: There should be 10 rows and 3 boxes in each row. If I select 2 or more boxes or drag a box, they should merge together. For example, if my initial screen looks like this: and then I se ...

When clicked, check if the CSS value is equal to

I have no idea why it's not working. After toggleClass is executed, if the CSS value equals "-84%" then hide(); $(".rwd-sec").click(function() { $(".rwd-main").toggleClass("is-active"); if ($(".rwd-main").css("right") == "-84%") { ...

Xpath is effective in Chrome Dev Tools, but does not seem to be functioning properly in RS

I am currently engaged in a project that involves extracting an HTML table containing specific text ("Current Prison History:") from various URLs that vary based on individual ID. I have attempted to utilize CSS selectors for this task, but encountered a c ...

The Angular JS routes are not properly loading the required file from the provided TemplateURL when receiving a response from Node

I am trying to retrieve data from a MySQL database using node (routes.js) and have the results injected into club.html, which is then dynamically updated in index.html using ng-view. However, it seems that the JSON response from node is displaying directly ...

How can I automatically disable certain checkboxes when a specific radio button is selected?

Looking to disable certain checkboxes when a specific radio button is selected. For instance, selecting the first radio button with the id="pz1" should disable checkboxes with matching id values from the thisToppings array. Each radio button cor ...

struggling to perfect the CSS styling

I am in need of a design similar to the image linked below: https://i.sstatic.net/Er1wT.png I attempted to create this layout using a table, but I have not been successful in aligning the side images properly. testTable.html <style> .container{ ...

WordPress woes: struggles with displaying Font Awesome icons

Issue with Font Awesome icons: displaying square boxes instead of icons Attempting to prototype a marketing page using Bootstrap and the latest Font Awesome file. However, encountering a problem where instead of icons, large square boxes are displayed on ...

What causes text inside a fieldset to sometimes appear bigger when viewed on mobile devices?

Have you ever noticed a strange quirk in Chrome that you couldn't quite explain? I recently stumbled upon a peculiar "feature" while using fieldset with responsive design. It seemed that the text within the fieldset appeared disproportionately larger ...

OpenStreetMap is failing to display the full map within the designated div

Here is the code snippet for displaying a map when clicking on a Span text, but not showing it in full: var latitude = document.querySelector('#lati').value; var longitude = document.querySelector('#longi').value; var open_address = doc ...