19
import { Checkbox } from 'antd' 

<Checkbox style={{ color: 'red', backgroundColor: 'black' }}>Dr John</Checkbox>

How do I change the color of the check box, not the label 'Dr John'? The style above only changes the styles of the label, not the check box.

Pang
  • 9,564
  • 146
  • 81
  • 122
Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84

8 Answers8

27

You can use simple css

.ant-checkbox-checked .ant-checkbox-inner {
  background-color: red;
  border-color: red;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
humanbean
  • 542
  • 4
  • 11
3

I was able to dynamically change the checkbox's color using @humanbean's and This answer.

Checkboxes.js

<Checkbox
    value={key}
    key={index}
    style={{
        "--background-color": boxColors[index],
        "--border-color": boxColors[index],
    }}>
    Title
</Checkbox>

CSS

.ant-checkbox-checked .ant-checkbox-inner {
    background-color: var(--background-color);
    border-color: var(--border-color);
}

Using var() function in css and providing the color in inline style of the component.

Inyoung Kim 김인영
  • 1,434
  • 1
  • 17
  • 38
3
const CustomCheckbox = styled(Checkbox)`
  ${props =>
    props.backgroundColor &&
    css`
      & .ant-checkbox-checked .ant-checkbox-inner {
        background-color: ${props.backgroundColor};
        border-color: ${props.backgroundColor};
      }
    `}
`;

with styled-component package you can do something like this.

<CustomCheckbox backgroundColor='green' />

Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84
2

Override the default antd checkbox classes in your css and provide the necessary attributes to custom it.

For example,

index.css

.ant-checkbox .ant-checkbox-inner {
  width: 25px;
  height: 25px;
  background-color: red;
  border-color: red;
}

.ant-checkbox-disabled .ant-checkbox-inner {
  width: 25px;
  height: 25px;
  background-color: gray;
  border-color: gray;
}

.ant-checkbox-checked .ant-checkbox-inner {
  width: 25px;
  height: 25px;
  background-color: transparent;
  border-color: red;
}

index.js

import React from "react";
import ReactDOM from "react-dom";
import { Checkbox } from "antd";
import "antd/dist/antd.css";
import "./index.css";

class App extends React.Component {
  onChange(e) {
    console.log(`checked = ${e.target.checked}`);
  }
  
  render() {
    return (
      <div>
        <Checkbox onChange={this.onChange} disabled={true}>Checkbox</Checkbox>
      </div>
    );
  }
}
ReactDOM.render(<App />, document.getElementById("root"));
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
1
    .checkbox { 
        .ant-checkbox .ant-checkbox-inner {
            border-color: red;                 // for mouse focus color
        }
      .ant-checkbox-checked .ant-checkbox-inner {
            background-color: green;         // selected color
            border-color: green;
        }
    }




.checkbox {     
  .ant-checkbox-checked .ant-checkbox-inner {
        background-color: green80;
        border-color: green80;
    }   
    .ant-checkbox-wrapper:hover .ant-checkbox-inner,
  .ant-checkbox:hover .ant-checkbox-inner,
  .ant-checkbox-input:focus+.ant-checkbox-inner {
    border-color: green80 !important;
  }

  .ant-checkbox-indeterminate .ant-checkbox-inner::after {
    background-color: green80;
  }
}
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
1

Just override the @checkbox-color Less variable.

Tárcio Zemel
  • 821
  • 10
  • 28
1

if this doesn't work

.ant-checkbox-checked .ant-checkbox-inner {
    background-color: #3350ac;
    border-color: #3350ac;
  }

then it means it is being overridden but the native ant CSS so put ! important to you CSS so that it overr

.ant-checkbox-checked .ant-checkbox-inner {
    background-color: #3350ac !important;
    border-color: #3350ac !important;
  }
Badal S
  • 507
  • 1
  • 3
  • 20
0

To dynamically change Checkbox using only CSS you can also add className to Checkbox and then access it in CSS like this:

<Checkbox className="my-checkbox">Your checkbox</Checkbox>

And then in CSS

.my-checkbox > ant-checkbox-checked .ant-checkbox-inner {
    background-color: "red";
    border-color: "red";
}
Mihajlo T.
  • 326
  • 3
  • 8