更改未选中的 Material-UI 复选框的背景颜色

Pep*_*Pep 1 css checkbox reactjs material-ui react-redux

我正在使用 React-Redux 和 Material-UI 来设计样式。我在工具栏上有一个 < Checkbox />。工具栏的背景颜色为蓝色 (#295ED9)。

在选中时,我设法更改复选框的颜色。当未选中时,我还将复选框轮廓的颜色更改为白色。

问题:当复选框未选中时,我无法更改复选框的背景颜色/填充颜色。它始终与它所在的工具栏颜色相同(蓝色 - #295ED9)。

我尝试将 < svg > 和 < path > 的背景颜色、颜色和填充属性更改为白色。

我能得到的最好结果是当我将 < svg > 填充属性更改为白色时。不幸的是,这不会使复选框在未选中时填充为白色背景。

JSX

<div className="hasBalance">
  <FormControlLabel
    control={<Checkbox color="primary"/>}
    label="Has Balance"
  />
</div>
Run Code Online (Sandbox Code Playgroud)

社会保障局

.hasBalance {
  grid-area: balance;

  .MuiSvgIcon-root {
    fill: white;
  }
}
Run Code Online (Sandbox Code Playgroud)

Chrome 检查工具中的元素

<div class="hasBalance">
  <label class="MuiFormControlLabel-root">
    <span class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-143 MuiCheckbox-root MuiCheckbox-colorPrimary MuiIconButton-colorPrimary" aria-disabled="false">
      <span class="MuiIconButton-label">
        <input class="PrivateSwitchBase-input-146" type="checkbox" data-indeterminate="false" value="">
        <svg class="MuiSvgIcon-root" focusable="false" viewBox="0 0 24 24" aria-hidden="true" role="presentation">
          <path d="M19 5v14H5V5h14m0-2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2z"></path>
        </svg>
      </span>
      <span class="MuiTouchRipple-root"></span>
    </span>
    <span class="MuiTypography-root MuiFormControlLabel-label MuiTypography-body1">Has Balance</span>
  </label>
</div>
Run Code Online (Sandbox Code Playgroud)

目标是将未选中的 Material-UI 复选框的背景颜色/填充颜色更改为白色。谢谢你。

Rya*_*ell 6

下面是一种似乎有效的方法。这使用 ":after" 伪元素在 SVG 后面放置一个白色背景。

import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "@material-ui/core/styles";
import Checkbox from "@material-ui/core/Checkbox";

const WhiteBackgroundCheckbox = withStyles(theme => ({
  root: {
    color: "red",
    "& .MuiIconButton-label": {
      position: "relative",
      zIndex: 0
    },
    "&:not($checked) .MuiIconButton-label:after": {
      content: '""',
      left: 4,
      top: 4,
      height: 15,
      width: 15,
      position: "absolute",
      backgroundColor: "white",
      zIndex: -1
    }
  },
  checked: {}
}))(Checkbox);

function App() {
  return (
    <div style={{ backgroundColor: "blue" }}>
      <WhiteBackgroundCheckbox />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)

编辑复选框背景颜色

相关问题:更改 MuiCheckbox 材质 UI 中的刻度颜色