Suggested method to override button background colour?

7.7k Views Asked by At

How should I go about changing background-color CSS property for <button> element using Materialize CSS? It seems that for most other elements simply adding a class of, for example, .red or .yellow will change the background colour, but not for the button elements.

Is there any non-obtrusive way?

EDIT:

To be more precise, my <button> element has these classes: btn waves-effect waves-light red, but its colour remains default blue.

2

There are 2 best solutions below

1
On BEST ANSWER

It seems that red and green works fine.

<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.css" rel="stylesheet" />

<button class="btn waves-effect waves-light red" type="submit" name="action">Submit
  <i class="mdi-content-send right"></i>
</button>

<button class="btn waves-effect waves-light green" type="submit" name="action">Submit
    <i class="mdi-content-send right"></i>
  </button>

0
On

JQuery

Add this script to the end of document

//Preassign a default color

$(".btn").css("background-color", "blue");

// get random color
function getRandomColor() {
   var letters = '0123456789ABCDEF'.split('');
   var color = '#';
   for (var i = 0; i < 6; i++ ) {
       color += letters[Math.floor(Math.random() * 16)];
   }
   return color;
}

$(document).ready(function(){

    // update button color
    $("button").click(function(){
      var themeColor = getRandomColor();
      $(".btn").css("background-color", themeColor);
    });

Live Demo

https://codepen.io/hiteshsahu/pen/EXoPRq