Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change bootstrap version 4 button color

I can change the primary button color by changing the theme-color in bootstrap v4. But it affects all other components as well. How to set the primary button colour without affecting the theme color? I dont want to set brand-primary and achieve it.Any other alternatives?

like image 331
Janier Avatar asked Mar 08 '18 23:03

Janier


People also ask

How do you change the color of a button in Bootstrap?

Bootstrap Button ColorsUsing one of the default modifier classes will change the color of the text and background color of your buttons. .Btn-danger will make the button red and font white, while . btn-warning will make the button yellow and font black, and so on.

How do I change the toggle button color in Bootstrap 4?

If you work with sass version of bootstrap in _variables. scss you can find $navbar-inverse-toggler-bg or $navbar-light-toggler-bg where you can change the color and style of your toggle button. In html you have to use navbar-inverse or navbar-light depending on which version you want to use.

How do I change the color of a Bootstrap 4 button?

Now that Bootstrap 4 uses SASS, you can easily change only the button color using the button-variant mixins. Since you only want to change the primary button color, and not the entire primary theme color, you need to use the button-variant mixins in SASS. You can set whatever $mynewcolor and/or lighten () and darken () percentages you'd like.

What's new in Bootstrap 4?

This component of Bootstrap 4 can make reading your code easier by providing contextual clues through the class names. Using different prefixes, these classes can be used for both Bootstrap text color and background color, as well as coloring elements.

How many types of buttons are there in Bootstrap?

There are various colors that Bootstrap provides for the buttons, there are 8 classes that you can make use of along with btn, As you can the buttons are not named as per color but rather what the color signifies, let's see examples and see how they look,

What is Bootstrap text color?

Bootstrap text color is a set of colors that can be used to change the font's color. A text area can also be set up to change color when a mouse hovers over it.


3 Answers

2019 Update for Bootstrap 4.1+

Now that Bootstrap 4 uses SASS, you can easily change only the button color using the button-variant mixins. Since you only want to change the primary button color, and not the entire primary theme color, you need to use the button-variant mixins in SASS. You can set whatever $mynewcolor and/or lighten() and darken() percentages you'd like.

$mynewcolor:teal;

.btn-primary {
    @include button-variant($mynewcolor, darken($mynewcolor, 7.5%), darken($mynewcolor, 10%), lighten($mynewcolor,5%), lighten($mynewcolor, 10%), darken($mynewcolor,30%));
}

.btn-outline-primary {
    @include button-outline-variant($mynewcolor, #222222, lighten($mynewcolor,5%), $mynewcolor);
}

https://www.codeply.com/go/f3uTwmsCVZ (SASS demo)

This SASS compiles into the following CSS...

.btn-primary{color:#fff;background-color:teal;border-color:#005a5a}

.btn-primary:hover{color:#fff;background-color:#004d4d;border-color:#009a9a}
.btn-primary:focus,.btn-primary.focus{box-shadow:0 0 0 .2rem rgba(0,90,90,0.5)}
.btn-primary.disabled,.btn-primary:disabled{color:#fff;background-color:teal;border-color:#005a5a}
.btn-primary:not(:disabled):not(.disabled):active,.btn-primary:not(:disabled):not(.disabled).active,.show>.btn-primary.dropdown-toggle{color:#fff;background-color:#00b3b3;border-color:#000}
.btn-primary:not(:disabled):not(.disabled):active:focus,.btn-primary:not(:disabled):not(.disabled).active:focus,.show>.btn-primary.dropdown-toggle:focus{box-shadow:0 0 0 .2rem rgba(0,90,90,0.5)}

.btn-outline-primary{color:teal;background-color:transparent;background-image:none;border-color:teal}.btn-outline-primary:hover{color:#222;background-color:#009a9a;border-color:teal}
.btn-outline-primary:focus,.btn-outline-primary.focus{box-shadow:0 0 0 .2rem rgba(0,128,128,0.5)}
.btn-outline-primary.disabled,.btn-outline-primary:disabled{color:teal;background-color:transparent}
.btn-outline-primary:not(:disabled):not(.disabled):active,.btn-outline-primary:not(:disabled):not(.disabled).active,.show>.btn-outline-primary.dropdown-toggle{color:#fff;background-color:#009a9a;border-color:teal}
.btn-outline-primary:not(:disabled):not(.disabled):active:focus,.btn-outline-primary:not(:disabled):not(.disabled).active:focus,.show>.btn-outline-primary.dropdown-toggle:focus{box-shadow:0 0 0 .2rem rgba(0,128,128,0.5)}

https://www.codeply.com/go/l9UGO7J6V1 (CSS demo)


To change the primary color for all contextual classes (bg-primary, alert-primary, etc..) see: Customizing Bootstrap CSS template and How to change the bootstrap primary color?

Also see:
https://stackoverflow.com/a/50973207/171456
How to theme bootstrap

like image 101
Zim Avatar answered Oct 07 '22 09:10

Zim


Try this method

Add a class to the button , Here custom-btn and write the css for that in our stylesheet.

.btn-primary.custom-btn {
	background-color: #000;
	border-color: #000;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
	<button type="button" class="btn btn-primary custom-btn">Custom</button>
  <button type="button" class="btn btn-primary">Default</button>
</div>
like image 37
Athul Nath Avatar answered Oct 07 '22 07:10

Athul Nath


You can add custom color or redefine colors by change them via variables in sass files (Bootstrap 4).

$theme-colors: (
   primary: red,
);
@import "~bootstrap/scss/bootstrap";

If you use same keys for your colors, you will redefine Bootstrap. Otherwise, with new keys you create new classes.

This example set primary color from blue to red.

like image 13
vml Avatar answered Oct 07 '22 09:10

vml