Search code examples
htmlcssmaterial-components-web

How to change checkbox background color?


I use Material.io for my UI and create a check box:

<div class="mdc-form-field">
  <div class="mdc-checkbox">
    <input type="checkbox"
           class="mdc-checkbox__native-control"
           id="checkbox-1"/>
    <div class="mdc-checkbox__background">
      <svg class="mdc-checkbox__checkmark"
           viewBox="0 0 24 24">
        <path class="mdc-checkbox__checkmark-path"
              fill="none"
              d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
      </svg>
      <div class="mdc-checkbox__mixedmark"></div>
    </div>
    <div class="mdc-checkbox__ripple"></div>
  </div>
  <label for="checkbox-1">Checkbox 1</label>
</div>

Like this:

https://material-components.github.io/material-components-web-catalog/#/component/checkbox

How can I change checkbox background to other color for example red?


Solution

  • You can use --mdc-theme-secondary:#ff0000 to change your color:

    <div class="mdc-form-field">
      <div class="mdc-checkbox" style="--mdc-theme-secondary:#ff0000">
        <input type="checkbox"
               class="mdc-checkbox__native-control"
               id="checkbox-1"/>
        <div class="mdc-checkbox__background">
          <svg class="mdc-checkbox__checkmark"
               viewBox="0 0 24 24">
            <path class="mdc-checkbox__checkmark-path"
                  fill="none"
                  d="M1.73,12.91 8.1,19.28 22.79,4.59"/>
          </svg>
          <div class="mdc-checkbox__mixedmark"></div>
        </div>
        <div class="mdc-checkbox__ripple"></div>
      </div>
      <label for="checkbox-1">Checkbox 1</label>
    </div>