Personalizing Bootstrap 5 button designs and attributes for a customized look

In my project, I've set up my custom.scss file with the following code:

$primary: #e84c22;

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

@import "~bootstrap/scss/bootstrap.scss";

Currently, the color looks like this:

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

However, I am not happy with the text appearance inside the btn-primary class. It is currently dark and regular font, but I want it to be white and bold without having to add the classes text-light and fw-bold to each button element. My desired look is like this:

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

How can I achieve this by making changes to the custom.scss file?

Additional information:
In contrast to the btn-danger class
https://i.sstatic.net/ixBlc.png
which has white text by default. There should be a way to adjust the text color based on the brightness of the background color in bootstrap.

Answer №1

"There should be a feature that automatically adjusts text color based on the brightness of the background in bootstrap."

Absolutely, Bootstrap utilizes the WCAG 2.0 algorithm as detailed here. The shade of red you've chosen triggers the transition of text color to dark following the WCAG protocol.

Buttons are engineered through mixins (refer to: Buttons in the documentation). In this scenario, the 3rd argument of the button-variant mixin $color is described as:

$color: color-contrast($background, $color-contrast-dark, $color-contrast-light, $min-contrast-ratio)

Thus, the button's text color is either $color-contrast-dark or $color-contrast-light, contingent upon the WCAG 2.0 computation using the $min-contrast-ratio.

Hence, tweaking the custom red color slightly darker would switch the text color to white...

$primary: #d73b11;

@import "bootstrap";    

Alternatively, you can retain the original custom color and enforce white bold text on btn-primary like so...

$primary: #e84c22;

@import "bootstrap";

.btn-primary {
    color: #fff;
    font-weight: bold;    
}   

Demo

Or,

You can adjust the value of the $min-contrast-ratio variable (3, 4.5, and 7 are the accepted values) as demonstrated in this provided solution


Furthermore, note that by overwriting the theme-color map in the manner you are doing, you are erasing all other theme colors. Simply amending the $primary value will change the primary theme color.

Answer №2

To ensure the integrity of all bootstrap colors, the recommended method is to create your color map and incorporate it into the existing bootstrap setup.

First, you must include the necessary functions and variables in your custom.scss file.

Step 1

Begin by importing the functions and variables

@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";

Step 2

Create your custom color map instead of using the default $theme colors to prevent overwriting existing colors

$mycolors: (
  "primary": #e84c22,
);

Step 3

Merge your custom colors with the existing theme-colors using map-merge()

$theme-colors: map-merge($theme-colors, $mycolors);

Step 4

Lastly, import the bootstrap file

@import "../node_modules/bootstrap/scss/bootstrap";

Final code

@import "../node_modules/bootstrap/scss/functions";
@import "../node_modules/bootstrap/scss/variables";

$mycolors: (
  "primary": #e84c22,
);
$theme-colors: map-merge($theme-colors, $mycolors);

@import "../node_modules/bootstrap/scss/bootstrap";

Output

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

Why is the text color not changing?

If the button background color lacks contrast with the text, the text color will not change accordingly.

You can use the following site () to verify the contrast levels. Take note of the Fail indication for the Small Text for AA

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

Adjusting the Lightness slider for better contrast ratio resolves the issue.

Observe the Pass indication for the Small Text

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

Implementing the new color scheme will automatically update the text color

$custom: (
    "orange": #C52B03
);

Voilà!

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

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

Is it necessary to treat headings within figure elements as document headings?

Imagine a scenario where we are working with a page structured like this: <!DOCTYPE HTML><HTML><BODY> <ARTICLE> <H1>Heading Example</H1> <P>Here is what a heading (level 2) looks like:</P> <FIG ...

Issue with if statement when checking element.checked

Hey everyone, I'm currently working on a calculator app and running into an issue. I have set up 3 radio buttons and I would like to check them using an 'if statement' in my JS file. However, the problem is that the 'main' element ...

Ways to show alternative data from a database in Laravel 8

I am working on a project where I need to display additional data based on the option selected from a dropdown menu populated from a database. Can anyone guide me on how to achieve this using javascript or jquery? https://i.stack.imgur.com/k3WLl.png Belo ...

I'm currently experiencing a challenge with a project I'm tackling that specifically deals with chart.js

In my recent coding project, I created a script to gather user input and then present it in various chart formats. However, upon executing the code, the selected chart fails to display after inputting values and clicking on the "generate chart" button. Her ...

Is there a way to identify if you are at the bottom row of a column defined by CSS styling?

I have implemented the new CSS-style column to divide a single unordered list into multiple columns. Now, I am trying to figure out how to target the last element in each column using JavaScript or CSS. Here is my HTML: <ul> <li /> <li ...

What is the best method for creating thumbnail URLs for video files in HTML with JavaScript?

I am facing an issue with generating a thumburl for uploaded video files. After dynamically creating an input file and uploading local video files, I am able to retrieve the name and size of the file along with other information as shown in the screenshot ...

Using React Redux to Calculate the Grand Total Price of all Products Added to Cart

<div className='container py-4 my-5'> {item.length === 0 ? ( <p> cart is empty, <NavLink to='/'>continoue shopping</NavLink> </p> ) : ( item.map((cart) => { return ( & ...

Incapable of stacking one canvas on top of another

I'm new to javascript and looking for help with positioning a canvas element behind another. The code I have may be a bit messy, so any assistance is greatly appreciated. My goal is to have the canvas named "myCanvas" appear behind "coinAnimation". Th ...

Designing HTML columns to adapt to different screen resolutions

Looking for a clever jQuery or CSS trick to dynamically adjust the number of columns in a website layout based on the size of the viewport. For instance, an iPhone display would have 1 column, while an 800x600 screen would have 2, and a 1024x768 screen wou ...

Presenting XML data in HTML using a customized CSS stylesheet

I've explored various methods for showcasing an XML file on a web page using HTML, but I haven't yet encountered one that incorporates a CSS style sheet. The XML file I have already includes a CSS style sheet. When viewed in a web browser, the . ...

Tips for stopping the background color from affecting the text color

I'm dealing with a situation where I have an h6 nested inside a div. <div class="cylinder" data-v-5147e140=""> <div data-v-5147e140=""> <h6 class="quantite" data-v-5147e140="&qu ...

What could be causing the data-* prefix to malfunction in Angular 9?

I'm facing an issue with a basic component in Angular 9. Check out the code below: Component : @Component({ selector: 'hello', template: `<h1>Hello {{name}}!</h1>`, styles: [`h1 { font-family: Lato; }`] }) export class He ...

Exploring the Intersection of HTML5 File API and AJAX Chunked Uploads

Recently, I created a drag and drop multiple file upload feature with individual progresses, which has been working well. However, there is one issue that I have encountered. During the uploading of larger files, sometimes the browser freezes until the upl ...

Conditionals in Angular 2 using Sass

Looking to apply this style with Sass or CSS: IF :host.form-control MATCHES .ng-valid[required] OR .ng-valid.required THEN :host ::ng-deep input.form-control = border-left: 5px solid #42A948; Appreciate the help! ...

JS How can I generate individual buttons in a loop that trigger separate actions?

Currently, I am working with loops in jQuery to create a series of boxes that contain specific values. The problem arises when I try to assign actions to each box based on the values from an array. For example, I want each box to trigger a different alert ...

Tips for aligning the first row of a table with a line of text

I need assistance aligning the first row of a table with a line of text. Specifically, I am trying to achieve ** some text ** |first row of table      |             ...

Integrate a PHP link into a Gatsby/React project

I've been attempting to connect my contact form page, contactpage.php, with my Gatsby application. In the navigation bar (found in the Components folder), I've added the following code: <div> <a className="int_link_about" ...

AngularJS's ng-click function seems to be malfunctioning

Currently, I am in the process of learning angularJS with the help of the book titled "Learning Web Development with Bootstrap and AngularJs." One challenge I am facing is creating a ng-click functionality for a button in my project. Unfortunately, when I ...

Unable to include checkout and clear cart buttons in popover within bootstrap 5

I am currently working with BootStrap version 5.0.0 beta-2 and facing an issue when attempting to dynamically add a button within my popover. I have ensured that the scripts are included in the following order: <script src="https://ajax.googleapis. ...

Ways to showcase a collection of divs in a dual-column layout while minimizing any unnecessary vertical spacing

Is there a way to create two columns without changing the HTML structure? The content within each child element is dynamic, so there needs to be dynamic vertical spacing as well. I'm experimenting with different CSS properties for both the parent and ...