Customize the color of Bootstrap 4 buttons across the entire website

I'm currently working on customizing the color of Bootstrap 4 buttons. Specifically, I want to change the color of btn-primary when it is being clicked. It seems like there should be a button event that triggers this color change, but I've tried using hover, focus, and active without success.

In my experiments, I managed to make the button with the btn-primary class turn completely red, except when clicking on it, which then made the button blue. I am generating CSS styles using Sass.

This is what I have in my app.scss file:

.btn-primary {
    &:hover {
        color: red;
        background-color: red;
    }
    &:focus {
        color: red;
        background-color: red;
    }
    &:active {
        color: red;
        background-color: red;
    }
}

Is there a way to set the color of the button while it's being clicked?

Answer №1

Make sure to include the !important rule when styling elements.

.btn-primary:hover {
  color: red;
  background-color: red !important;
}

.btn-primary:focus {
  color: red;
  background-color: orange !important;
}

.btn-primary:active {
  color: white;
  background-color: green !important;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<button type="button" class="btn btn-primary">Primary</button>

Alternatively, here's how you can achieve the same result without using !important:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>

<style>
.btn-primary:hover {
  color: white;
  background-color: red;
}

.btn-primary:focus {
  color: red;
  background-color: orange;
}

.btn-primary:not(:disabled):not(.disabled):active {
  color: white;
  background-color: green;
}
</style>
<button type="button" class="btn btn-primary">Primary</button>

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

displaying an item within a text field

I have an input box created using Angular reactive forms <input type="text" formControlName="OrgName" placeholder="Enter name" maxlength="60"> <p class="fieldRequired" *ngIf="showNameMSg" ...

Tips for switching out images depending on the time of day

Currently, I have a script that dynamically changes the background color of my webpage based on the time of day. However, I am facing issues trying to implement a similar functionality for replacing an image source. The current code is also time zone-based ...

When refreshing the webpage, it becomes glitchy but then corrects itself

Currently working on a website, I've encountered a frustrating problem that has me scratching my head. This issue occurs across multiple pages; upon refreshing the page, the navbar, search bar, and other elements become completely disorganized. Stra ...

The sequence of HTML attributes

Could there be a subjective angle to this question (or maybe not)... I work on crafting web designs and applications using Visual Studio and typically Bootstrap. When I drag and drop a CSS file into an HTML document, the code generated by Visual Studio loo ...

"Experience a unique website layout with varying designs on desktop and mobile devices while using the React technology to enable desktop

Just starting out with React and I've noticed something strange. When I view my website on a PC, everything looks normal. However, when I switch to the desktop site on my phone, things appear differently. It seems like moving an element by 20px on mob ...

Erase the white backdrop from the dragImage

Is there a way to remove the white outline that appears when dragging a draggable element from a dark background? I want to make it completely transparent. Here is a visual representation: https://i.sstatic.net/Eywf9.png Here is the code snippet: docume ...

Conceal the footer when the website is scrolling, reveal it when the website is not scrolling

Before I pose my question, I need to establish two constraints: Must be compatible with IE 7+ Only able to modify the CSS, cannot adjust the HTML/JS So, I have two divs: <div id="content"></div> <div id="footer"></div> "#conten ...

Tips for incorporating a transition effect on group hover using Tailwind CSS

I've been working on creating a cool animated transition effect for my button where a chevron icon appears when the user hovers over it. I'm currently developing the website using NextJS and TailwindCSS, and I've implemented a group hover fe ...

Issues with updating web pages on Google Chrome

Having an issue with Google Chrome not updating the new CSS on my website without clearing the cache every time. It's a concern because we can't expect visitors to constantly clear their cache when visiting. Does anyone have a solution for ensuri ...

Ensure consistent element heights within adjacent columns using CSS

My template looks like this: https://i.sstatic.net/hoLzR.jpg I am trying to keep the same height between each item in both columns, where the height is determined by the tallest item in each column when they are placed side by side. But on smaller screen ...

WordPress page-id causing style to fail showing up properly

Recently, I was making updates to my website located at My Site. On the homepage, I utilized the following code snippet to display content from a specific page: <?php $id=48; $post = get_post($id); $content = apply_filters('the_content', $ ...

Adjusting the appearance of a label within an md-input-container using a CSS class

Looking for a way to apply styling to the md-input label inside md-input-container using ng-class. While able to style with inline css, encountering issues when trying to use a css class on the label. Is this expected behavior or am I overlooking somethin ...

What could be causing the hover effect to malfunction in this code snippet?

I have been working on creating a hover effect for a list of items where an underline emerges from the center. While I have done something similar in the past, there seems to be an issue preventing it from working properly this time. I have included the HT ...

Is it possible to employ flexbox CSS to stack four tables on top of each other, allowing for resizing up to the maximum height of the window?

After experimenting with various setups, I have yet to find a complete solution. My objective is to have 4 tables stacked on top of each other, each starting just below 1/4 of the browser height. I want the ability to resize each table vertically, making t ...

Tips for transforming a menu into a dropdown menu

Is there a way to convert the current menu into a Dropdown menu? Currently, the menu is not collapsing and it keeps opening. Any suggestions on how to achieve this? Here is an example for reference: https://i.stack.imgur.com/2X8jT.png ar ...

Creating a compact Swiper slider: reducing image size without sacrificing full window coverage!

Utilizing Swiper to craft a slider that occupies the entire window, with a slight margin for a bar-- no issues there. (https://i.sstatic.net/qcGBA.jpg) However, the image I placed in for the slide () appears to be excessively enlarged. Is there a way to ...

I am experiencing an issue where the text is not appearing in my Bootstrap

As a newcomer to Bootstrap and CSS, I'm aware that my question might reflect my lack of experience in these areas. Here is my query: Scenario: I am utilizing Bootstrap along with the [Grayscale theme] to create a basic tool that will assist my stude ...

What is the process for enabling SASS line numbers using Yeoman's Grunt.js file?

Recently, I used Yeoman (1.4.5) to scaffold a new web application. Within my Gruntfile.js, I configured it as follows: ... // When requested, compile Sass to CSS and generate necessary files sass: { options: { lineNumbers:true, sourceMap: true, ...

Troubleshooting the issue of autoplay not functioning in HTML5 audio

I'm facing a strange issue with my code - the autoplay feature for audio is not working as expected. Typically, whenever I insert this particular piece of code into a website, the music starts playing automatically. However, it seems to be malfunctio ...

The concept of transparency and visual representation

Is there a way to prevent the opacity of the parent div from affecting the opacity of the img? <div class="parent_div" style="opacity:0.2"> <p>Some text</p> <img class="gif" style="opacity: 1;" src="ht ...