Search code examples
javascriptcssmaterialize

Making Two Materialize Switches of Different Colors


I was able to make a single materialize switch and it looks great, but now I want a second switch - with a different color setup than my first one. This is the code I want to run, but my switches just disappear when I try to change the class name!! Any help would be greatly appreciated!!!

HTML:

  <div class="switch-1" align="center">
    <label>
      1 OFF
      <input id="Switch1" type="checkbox">
      <span class="lever"></span>
      1 ON
    </label>
  </div>

 <div class="switch-2" align="center">
    <label>
      2 OFF
      <input id="Switch2" type="checkbox">
      <span class="lever"></span>
      2 ON
    </label>
  </div>

CSS:

.switch-1 label .lever {
   background-color: #79ff4d;
}
.switch-1 label input[type=checkbox]:checked+.lever {
   background-color: #ff6666;
}
.switch-1 label input[type=checkbox]:checked+.lever:after {
   background-color: #ff3300;
}
.switch-2 label .lever {
   background-color: #79ff4d;
}
.switch-2 label input[type=checkbox]:checked+.lever {
   background-color: #a6a6a6;
}
.switch-2 label input[type=checkbox]:checked+.lever:after {
   background-color: #737373;
}

Solution

  • Try this:

      <div class="switch red" align="center">
        <label>
          1 OFF
          <input id="Switch1" type="checkbox">
          <span class="lever"></span>
          1 ON
        </label>
      </div>
    
       <div class="switch grey" align="center">
        <label>
          2 OFF
          <input id="Switch2" type="checkbox">
          <span class="lever"></span>
          2 ON
        </label>
      </div>
    

    with this CSS:

    .switch.red label .lever {
       background-color: #79ff4d;
    }
    .switch.red label input[type=checkbox]:checked+.lever {
       background-color: #ff6666;
    }
    .switch.red label input[type=checkbox]:checked+.lever:after {
       background-color: #ff3300;
    }
    .switch.grey label .lever {
       background-color: #79ff4d;
    }
    .switch.grey label input[type=checkbox]:checked+.lever {
       background-color: #a6a6a6;
    }
    .switch.grey label input[type=checkbox]:checked+.lever:after {
       background-color: #737373;
    }