Define the hover color options within your personalized Bootstrap 4 design

Currently, I have customized Bootstrap 4.5.3 with some simple modifications.

Below is the custom SCSS code I am using:

$newcolor1: #00a89c;
$newcolor2: #e61b53;

$colors: (
    "newcolor1": $newcolor1,
    "newcolor2": $newcolor2
);

$primary:   $newcolor1;

$theme-colors: (
    "primary": $primary,
    "newcolor2": $newcolor2
);

@import "bootstrap";

In my setup, I have introduced two new colors, assigned "primary" as newcolor1, and established a theme for newcolor2 to enable styling like btn-newcolor2.

Although Bootstrap generates color variants for hover states based on the base color, my client prefers specifying hover colors directly, which exceeds my Sass expertise level.

After inspecting the source files, it appears that Bootstrap darkens the base color by a set percentage to create hover effects. Is there a method to specify a particular color for .btn-primary:hover and .btn-newcolor2:hover? Ideally, I would like to enforce the specified color where provided and retain the default behavior (darkening) when no color is specified.

Answer №1

Named as button-variant(), the mixin responsible for creating different color states for buttons in CSS is located in bootstrap/scss/mixins.

This particular SASS mixin has the ability to handle custom colors for .btn hover and active states through various parameters...

@mixin button-variant(
  $background, 
  $border, 
  $hover-background: darken($background, 7.5%), 
  $hover-border: darken($border, 10%),
  $active-background: darken($background, 10%),
  $active-border: darken($border, 12.5%)
) { 
  //...
}

However, it's worth noting that the hover and active parameters are set as defaults using the SASS color darken() function to calculate the colors based on the passed $theme-colors value automatically.

As a result, relying solely on $theme-colors becomes ineffective in managing button state colors.




To customize the colors for the active and hover states of .btn while also minimizing compiled CSS output and avoiding duplicate selectors, there are a few approaches you can take.

Simplest Method

Refer to the following SCSS code which deals with importing individual Bootstrap vendors and appropriately handling custom variables and map removals to ensure the correct compilation and usage of variables by Bootstrap.

Follow the comments within the SCSS code to understand the process...

// import initial bootstrap vendors

@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";
@import "~bootstrap/scss/mixins";

//... omitted for brevity ...//

By emptying the $theme-colors map, Bootstrap will no longer compile loops that rely on $theme-colors since it's now empty.

You can manually utilize the button-variant() mixin within any .btn-* selector to create customized buttons without bloating the compiled output with unnecessary CSS declarations.

This method is straightforward as it allows you to specify any color for the button-variant() parameters.

.btn-primary {
  @include button-variant(
    // customization goes here
  );
}

.btn-secondary {
  @include button-variant(
    // customization goes here
  );
}



Advanced Method

Reintroduce the $theme-colors and alter the @include button-variant() in mixins.scss using the previous code as reference.

Before proceeding, remove the custom button selectors from the aforementioned code.

This approach is more complex as it involves controlling hover and active state colors precisely by modifying how @include button-variant() handles the colors.

One potential solution is to reintegrate $theme-colors before root.scss, right after the array declaration of $colors.

$theme-colors: (
  "primary": $new-color-1,
  "secondary": $new-color-2
);

Create an overriding button mixin to manage all button variant calls effectively.

Insert this modified button-variant mixin after

@import "~bootstrap/scss/mixins";
and prior to the map-removals declaration in a file named _mixins.scss which can be imported neatly into your project.

You can further organize all custom mixins for the project under "mixins".

Adjust the parameters of the custom @mixin button-variant() to accommodate the passed $theme-colors.

// Customizations made to params in button-variant() mixin below
// Default mixin params commented out below

// $background
// $border
// $hover-background: darken($background, 7.5%)
// $hover-border: darken($border, 10%) 
// $active-background: darken($background, 10%)
// $active-border: darken($border, 12.5%)

@mixin button-variant(
  $background,
  $border,
  $hover-background: lighten($background, 10%),
  $hover-border: lighten($border, 10%),
  $active-background: lighten($background, 2.5%),
  $active-border: lighten($border, 2.5%)
) {
  // Mixin content goes here
}

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

What is the best way to recreate this grid-like design using CSS?

By utilizing clever techniques such as margins and outlines, I was able to design the following. If you'd like to take a look at the live website, it can be found at https://i.sstatic.net/kl0an.jpg The CSS code that I used is as follows: body { ma ...

How to align text in Bootstrap/CSS to be centered on the left side

I require your assistance. I have a column with text that needs to be center-aligned, but laid out as demonstrated below. I attempted to use display: inline-block; text-align: left but it didn't produce the desired result. The desired layout is as ...

Exploring ways to distribute the height of parent div among child divs

I am looking to dynamically adjust the height of child divs within a fixed-height parent div. The parent div's height is fixed, but the number of child divs can vary. I want each child div to have a height equal to the parent div's height divided ...

Specify the location of the resize button (corner)

Scenario : At the bottom of the page, there is a resizable textarea, However, having the resize button at the bottom-right corner does not provide the best user experience (try it out), Issue : I am wondering if there is a way to move the resize butt ...

Page items do not expand to fill the page when zooming out

When I try to zoom out in my browser, the elements within #workspaceMain such as #pagename, #toolbar, #content, and #tabs do not expand to fill the remaining space within #workspaceMain, even though I have set all widths to 100%. The width of #workspaceMai ...

Bootswatch alternative for Bootstrap CSS without utilizing Google Fonts

Currently, I am in the process of developing a webpage and utilizing Bootswatch for styling. However, there are times when I need to work offline and host locally. The issue I am facing is that Bootswatch cannot be used offline due to the fact that it util ...

Is there a way to keep this function running endlessly when hovering?

Is there a way to modify this function so that the classes of my links continuously change colors while I hover over them, rather than changing only once? <script type="text/javascript"> $(".nav a").hover(function(e){ var randomC ...

Options for regular expressions in CSS: match letters regardless of case and search for all occurrences

Here is the HTML I am working with: <div> <img class="hi" src="http://i.imgur.com/f9WGFLj.png"></img> <img class="HI" src="http://i.imgur.com/f9WGFLj.png"></img> </div> Additionally, I have the following CSS co ...

Is there a way to program custom key assignments for my calculator using JavaScript?

As a beginner in the world of coding and JavaScript, I decided to challenge myself by creating a calculator. It's been going smoothly up until this point: My inquiry is centered around incorporating keys into my calculator functionality. Currently, th ...

Applying Regular Expressions in Java to "convert" a CSS style into an HTML tag style

In my Java code, I have a String variable that contains HTML code like this: <span style="text-decoration: underline;">test</span> My goal is to convert it to look like this: <u>test</u> If the HTML code is like this: <span ...

The rotation of an image in Microsoft Edge results in an HTML file display

One of the images on my website is sourced like this: <p> <img src="images/image.jpg" style="width: 50%;" /> </p> Surprisingly, in Chrome and Firefox, the image looks fine (just how I uploaded it to the server). H ...

Finding all instances of a specific class in Sublime Text 2

Is there a way in Sublime Text 2 to search for every instance of a specific class applied to an element? For example, if I have 20 .jsp's and I want to find every element with the class "sample," is there a method to do this beyond just searching for ...

What is the best way to retrieve the border-color inline style using jQuery?

I have a HTML tag like this. <span id="createOrderFormId:accountNo" style="border-color: red;"><</span> To retrieve the style value for the border-color property, I tried using the following jQuery code: $( document ).ready(function() { ...

I aim to utilize JavaScript to capture the constantly changing font color that is randomly generated

Is there a way to extract the font color from a table element? #FF0000 For example, it could be #568000 etc.. Here's my table <table class="mytable"> <th class=vertical bgcolor=#C0C0C0 align=center> <font color=#FF0000 size= ...

Is there a way to adjust the saturation and desaturation of an image using CSS?

Issue Update After further testing, I have discovered that the desaturation effect is only functioning properly in Chrome. How can I adjust it to work in other browsers such as FF and IE? (Title modified) Currently, I am working on turning a color image ...

Animating a div's width with CSS from left to right

My goal is to create an animation effect for two overlapping divs that will reveal or hide the text inside them. I want one div to animate from left to right while the other animates from right to left, creating a wiping effect where as one piece of text d ...

Mastering the Art of Defining SVG Gradients in a Page-wide CSS File

On my HTML page, I have implemented a JavaScript code that dynamically generates SVG elements and inserts them into the HTML page. Currently, all the styling attributes such as fill colors are specified in the CSS of the entire webpage. This approach allo ...

What could be causing my website to have a horizontal scroll even when the width is set to 100%?

My website seems to be scrolling horizontally, possibly due to an issue with the footer. I suspect it might be related to the social media icons in the footer, but I'm not entirely sure. Any guidance on resolving this problem would be greatly apprecia ...

CSS hover effects are malfunctioning

Having an issue with hover in CSS. Attempting to create a menu bar using saved PNG files, each file within its own div. When applying a class name and hover modifier, it only works on the first element. As the pointer moves to subsequent elements, they are ...

Adjust div height to match the dynamic height of an image using jQuery

I'm facing an issue with my image setup. It has a width set to 100% and a min-width of 1024px, but I can't seem to get the 'shadow' div to stay on top of it as the window size changes. The height of the shadow div also needs to match th ...