Is it possible to change the background color of a Bootstrap button using CSS overrides?

Desired Outcome

https://i.sstatic.net/EjEXG.gif

Actual Result

https://i.sstatic.net/BmXYG.gif

The goal is to toggle the red-background class, so that when hovering over the button-bullet, its background-color changes to #FCC1C5.

Avoiding the use of

.btn.button-bullet:hover</code due to jQuery logic disabling a button from showing with only one bullet element displayed.</p>

<p>Despite using <code>toggleClass("red-background")
, there are no visible changes happening.

Possibly the issue lies in CSS where .btn.button-bullet's background-color is set to transparent. If this is the problem, how can the background-color be overridden using jQuery?

Code Snippets

HTML

<div class="worksheet-problems">
  <div class="row">
    <div class="col-lg-7">
      <div class="form-group">
        <div class="input-group input-group-lg worksheet-problem">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-bullet"> <span class="glyphicon glyphicon-one-fine-dot" aria-hidden="true"></span> </button>
          </div>
          <input type="text" name="Worksheet-Problem" class="form-control" placeholder="Problem..." aria-label="Write worksheet problem here">
          <div class="input-group-btn">
            <button type="button" class="btn btn-default button-add" aria-label="Add"> <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS

.red-background{
  background-color: #FCC1C5;
}
/*remove Bootstrap default grey for buttons*/
.btn.button-add, .btn.button-bullet{
  background-color: transparent;
}

jQuery

$(document).ready(function() {
  $(".worksheet-problems").on("mouseenter mouseleave", ".button-bullet", function() {
    if ($(".worksheet-problems").children().length > 1) {
      $(this).children(".glyphicon").toggleClass("glyphicon-one-fine-dot");
      $(this).toggleClass("red-background");
      $(this).children(".glyphicon").toggleClass("glyphicon-minus-sign");
    }
  });
});

JSFiddle

Answer №1

Issue: The specificity of your red-background class is lower than .btn.button-bullet.

Resolution (increase specificity and position it after selectors with a transparent background):

.btn.button-add, .btn.button-bullet {
  background-color: transparent;
}
.btn.red-background {
  background-color: #FCC1C5;
}

Answer №2

It appears as though

$(this).toggleClass("red-background"); 

is not affecting the background element. To ensure you are targeting the correct element, use:

console.log($(this)); 

to verify that you are altering the intended element. In some cases, a helpful trick is to utilize:

.red-background{
    background-color: #FCC1C5 !important;
}

However, the issue may also stem from a bootstrap style taking precedence over your CSS class. Make your selector more specific.

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

Display iframe as the initial content upon loading

Seeking solutions for loading an iframe using jQuery or Ajax and outputting the content to the page once it has been loaded. The challenge lies in loading an external page that may be slow or fail to load altogether, leading to undesired blank spaces on th ...

Challenges with the jPages jQuery Plugin

Encountering issues with the implementation of jPages? Wanting to incorporate pagination on a table using jPages and looking to replicate the row by row fadeTo effect observed in the demo, but facing some challenges: Using border-collapsed causes the bac ...

Tips for beginning a counter that displays online status from the ground up

I have implemented a script on my website that displays the online player count for the GTA server without needing to refresh the page. You can check out the counter script here. var updateInterval = 700; setInterval(updatePlayerCount, updateInterval); // ...

What causes arrays in JavaScript to not be sorted in either ascending or descending order based on dates?

I'm attempting to organize my array of objects that contain a date property. I need to arrange the array based on ascending or descending dates. I've attempted the following approach: https://jsfiddle.net/rxaLutgn/1/ function sort_by(field, rev ...

Arranging rows of a specific height within a table at any location

I need to create a virtual table to browse through a very large dataset. Is there a way to position the rows (with fixed height) anywhere within the table? The issue I am facing is that the table properties automatically adjust the row height, disregarding ...

Reverting the box color changes in the opposite order of clicking them using HTML, CSS, and Jquery

As someone new to front end development, I am experimenting with creating multiple boxes using HTML, CSS, and jQuery. My goal is to have the boxes change color to blue when clicked, then revert back to their original color in the reverse order they were cl ...

Display WooCommerce notifications when using the AJAX add to cart feature

Looking to enhance my WooCommerce shop by incorporating AJAX functionality for the add to cart feature, I stumbled upon this incredibly useful tutorial which worked seamlessly. However, after implementing the solution, I noticed that the WooCommerce notic ...

Create a stylish slide-in menu using CSS, triggered by an onclick event. No JavaScript required!

I implemented a hamburger menu that slides in when clicked. However, I'm facing an issue where the slide-in menu disappears after a second. How can I make sure the menu stays visible? #navigationWrap { position: fixed; top: 0; ...

What steps do I need to take to retrieve data in a JSON array based on its

Hello, I could really use your assistance with a problem I'm having. Here is the scenario: I have a JSON array that looks like this: "category" : [ { id: 1, product: [{id : product_1, type : ball}] }, { id : 2, product :[{id : prod ...

The HTML and body elements do not possess a width of 100% within the document

When viewing my portfolio site on smaller screens such as 425px and below, there is a white gap on the right side. This issue arises because the HTML and body elements do not inherit full width by default. Do you know why this happens and how I can resol ...

The height property in the CSS is causing the parent element to break and cannot be confined

Here is the HTML code that I am currently working with: <div id="div1"> <p id = "a"> <! -- content --> </p> <p id='b'> <! -- content --> </p> </div> Additionally, th ...

Script for uploading multiple images without using flash, with customization options available for each individual upload

Looking for a non-flash images uploader script that has the following features: Ability to upload multiple files Supports drag and drop functionality Displays progress bar for each upload Shows small preview of each upload Allows for resumable downloads ...

Show two <p>'s next to each other

I am trying to arrange 2 paragraphs side by side. <p class = "firstClass">This is the first paragraph.</p> <p class = "secondClass">This is the second paragraph.</p> Result: This is the first paragraph. This is the second paragra ...

Utilizing the Page Method to Handle Warnings in $.ajax({}) Calls

I created a custom method in the .cs file of my Default.aspx page: [WebMethod] public static string ConvertToJSON(object data) { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); string json = jsSerializer.Serialize(data); retu ...

Why are my elements not appearing where I want them to in absolute positioning?

Could use some help with a seemingly simple question, I just need some clarity on this issue. Background: I am working on developing a complex website and one of my ideas involves generating a series of boxes on the screen. For various reasons that are t ...

Identify the quantity of dynamically added <li> elements within the <ul> using jQuery

I'm facing an issue where I need to dynamically add a list of LI items to a UL using jQuery. However, when I try to alert the number of LI elements in this list, it only shows 0. I suspect that it's because the code is trying to count the origina ...

How can we nest a div within the outermost parent element?

Consider a scenario where there are 3 different divs named grandParent, Parent, and Child. Although the parent's tag is placed inside the grandParent, due to the use of position: absolute; property, the parent ends up being displayed outside the grand ...

Maintain the aspect ratio of a div with CSS styling

Attempting to create a div while maintaining a 16:9 aspect ratio using CSS led me to conduct a Google search. Fortunately, I stumbled upon a helpful resource on Stack Overflow that detailed how to achieve this: How to maintain the aspect ratio. Excited to ...

Semantic UI allows for both left and right action input functionalities to be utilized

Is there a way to add actions on both ends of an input field? Here's what I've tried so far: <form class="ui form"> <div class="ui left action input"> <select class="ui compact selection dropdown" name="seats"> ...

Quantify the Proportion of Affirmative/Negative Responses and

I am attempting to calculate the percentage of "Yes" or "No" responses in an HTML table based on user input for each month's questions. Then, I want to display the average percentage in the "average" column for each month. For example, if the user sel ...