0

I've already tried anything I could find and basically the general idea is to use following css rule:

input[type=checkbox][disabled] {
    background: red !important;
    background-color: red !important;
    outline: 1px solid red;
}

The problem is that the only rule that is working is the outline. The background just wont change in any way. It remains gray. I'm providing a screenshot of the result of the css rules I posted above.

result

Is there any working way to successfully change the background color of the disabled checkboxes?

  • Does this answer your question? [How to change the background color on a input checkbox with css?](https://stackoverflow.com/questions/34388696/how-to-change-the-background-color-on-a-input-checkbox-with-css) – Sato Takeru Mar 10 '21 at 14:50

1 Answers1

2

There is no way do it with native appearance, so you must create custom template with label and use pseudo elements like :before or :after:

.flex-container {
  display: flex;
  flex-flow: row-wrap;
}

.flex-column {
  padding: 0 15px;
  flex: 0 0 auto;
}

[type="checkbox"]:checked,
[type="checkbox"]:not(:checked) {
    position: absolute;
    left: -9999px;
}
[type="checkbox"]:checked + label,
[type="checkbox"]:not(:checked) + label
{
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    color: #666;
}
[type="checkbox"]:checked + label:before,
[type="checkbox"]:not(:checked) + label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid #ddd;
    background: #fff;
    border-radius: 5px;
}
[type="checkbox"]:checked + label:after,
[type="checkbox"]:not(:checked) + label:after {
    content: '';
    width: 8px;
    height: 8px;
    background: #00579c;
    position: absolute;
    top: 6px;
    left: 6px;
    border-radius: 50%;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
}
[type="checkbox"]:not(:checked) + label:after {
    -webkit-transform: scale(0);
    transform: scale(0);
}
[type="checkbox"]:checked + label:after {
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
}

[type="checkbox"]:disabled + label {
    opacity: 0.7;
    cursor: not-allowed;
}

[type="checkbox"]:disabled + label:before {
    border-color: red;
}
 <div class="flex-container">
   <div class="flex-column">
    <input type="checkbox" id="test2">
    <label for="test2">Default</label>
  </div>
  
  <div class="flex-column">
    <input type="checkbox" id="test1" checked>
    <label for="test1">Checked</label>
  </div>
  
   <div class="flex-column">
    <input type="checkbox" id="test3" disabled>
    <label for="test3">Disabled</label>
  </div>
 </div>
Erik P_yan
  • 608
  • 5
  • 6