Adjust the checkmark color of MUI Checkbox

When using Material UI's MUI library for React, I encountered an issue with the checkbox checkmark color. By default, the checkmark takes on the background color of the container element, which is great for light backgrounds but not ideal for dark ones. I wanted the checkmark to be white on dark backgrounds. How can this be achieved?

I attempted to change the checkmark color by setting the color, but that only affected the accent color, not the actual checkmark. Setting the backgroundColor resulted in a thick white border around the checkbox.

In my quest for a solution, I even tried creating custom icons from SVG, but I couldn't figure out how to apply all the necessary CSS classes used by MUI for icons.

While exploring options from MUI's icon library, none seemed to support controlling both the background color and the checkmark color simultaneously.

As a last resort, I resorted to a cumbersome workaround of positioning a <div> behind the checkbox, but it failed when the checkbox wasn't checked. Surely there must be a more elegant solution to tackle this problem!

Check out the sandbox: https://codesandbox.io/s/mui-checkbox-check-mark-color-ss9dr6

<div
  className="App"
  style={{ color: "white", backgroundColor: "#333", padding: 20 }}
>
  <div>How to get a MUI checkbox with a white checkmark?</div>
  <Checkbox
    defaultChecked
    sx={{
      "& .MuiSvgIcon-root": {
        fontSize: 28,
        color: "green",
        backgroundColor: "white"
      }
    }}
  />
  <Checkbox
    defaultChecked
    sx={{
      "& .MuiSvgIcon-root": { fontSize: 28, color: "green" }
    }}
  />
  <div>
    Desired: white check mark (like left example above) but without the
    white border.
  </div>
</div>

https://i.stack.imgur.com/HHeop.png

Answer №1

I've found a clever solution that could be helpful in this situation. The issue lies in the unadjustable size of the SVG within the material, causing the border problem.

<Checkbox
  defaultChecked
  sx={{
    "& .MuiSvgIcon-root": {
      fontSize: 28,
      color: "green",
    },
    "&.MuiCheckbox-root": {
      borderRadius: 0,
      padding: 0,
    },
    "&.Mui-checked": {
      backgroundColor: "white",
    },
    "& svg": {
      scale: "1.4",
    },
  }}
/>;

With this change, you'll see the result you desire. While the checkbox may appear slightly larger, the border will no longer be visible after implementing the scaling feature.

https://i.stack.imgur.com/OlQf7.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Bootstrap dropdown feature is acting up when included in the header PHP file

I'm having an issue where the dropdown in my header.php file is not working when I include it in my home.php page. The navigation bar appears correctly, but the dropdown feature doesn't work. Can someone please assist me with this? Below are the ...

What is the best way to unselect the "all" selector if one of the inputs is no longer selected?

I am facing an issue with a search filter functionality. When all filters are selected and then deselected individually or together, the "all" button remains selected. I need help in ensuring that when any filter is deselected, the "all" button also gets d ...

Issue with importing CSS in Pug-Template

Looking to develop a Website using node.js and express with Pug as the template language, incorporating Bootstrap CSS. However, encountering an issue where Pug is not importing my custom CSS but is successfully importing Bootstrap CSS. Experimented with d ...

Having trouble getting a React app integrated with WordPress on an NGINX server to function properly over HTTPS?

I am currently in the process of developing a React application that integrates with a WordPress site. The setup I am aiming for is as follows: When users visit example.com, they will be directed to my React app. If they navigate to example.com/blog, they ...

When attempting to create a react app, I encounter difficulties using the npx create-react-app command

view image here I've attempted various solutions to fix the issue, such as deleting and reinstalling the node, but no matter what I do, it always ends up like this. I also tried removing node modules and reinstalling React, yet the problem persists. ...

Looking for assistance with a CSS selector in jQuery

I am working with a WordPress blog that has multiple pages with dropdown subpages on hover. However, I only want the main pages to have links, not the subpages. To achieve this, I am using some basic JavaScript. jQuery(document).ready(function(){ jQ ...

Is there a way to achieve a flex layout without having to modify the HTML structure?

I am working on achieving a specific layout using CSS: https://i.stack.imgur.com/xg7ZA.png This is the HTML structure I have and it cannot be altered, along with what I have managed to add with CSS. It's almost there but missing something: .conta ...

Guide to counting the number of image tags within a parent div and selectively removing them starting from a specific position

My dynamic image tag <img> is generated inside a div from the database using fetching. Here is how my HTML looks: <div class='forimgbox'> <p class='palheading'>Pals</p> <img src='userpic/2232323.p ...

What is the best way to assign a unique ID to every <td> element within a table using React Js?

Hello everyone. I am currently working on assigning unique ids to each td in a table based on data received through an API. This is what my code looks like so far. CodeSandbox const assignIdsToTableData = (data) => { var items = Object.values(data)[0 ...

What is the best way to display Material UI cards in a horizontal layout?

I'm currently incorporating Material UI into my project, Within my list of cards, I aim for them to display one after the other horizontally and then continue in the same fashion on the next row. However, they are currently stacking vertically. I&ap ...

Input of data and salt must be provided

(node:35) UnhandledPromiseRejectionWarning: Error: data and salt arguments required. Can someone please assist me in resolving this error that I am encountering? const validatePassword = (password) => { return password.length > 4; }; app.post("/r ...

Visual Latency when Quickly Toggling Between Images in Succession

I have a series of 50 images that need to be displayed sequentially inside a div. The time interval between displaying each image is initially set at 750 milliseconds and decreases with each subsequent image. To ensure all images are loaded before the an ...

The image will remain hidden until it is fully loaded and ready to be displayed

I am currently working on a script that involves displaying a loading icon until an image is fully loaded, at which point the loading icon should disappear and the image should be shown. Unfortunately, my current code is not working as intended. Here is a ...

Display every div element if none of the links have been clicked

On my webpage at url.com/yourfirstpage/, all div elements are hidden by default with a display:none property. If we specifically target #sec1 by going to url.com/yourfirstpage/#sec1, only sec1 is displayed while the others remain hidden. But what if we acc ...

Hold off on rendering until the useEffect is complete

Currently configuring the default access level An issue arises when altering the access level within useEffect, leading to the page rendering with the default access level intact. How can I delay the page rendering until useEffect has completed? function ...

Troubleshoot: Issue with Navbar Dropdown Expansion on Bootstrap Sass 3.3.6 with JavaScript

Beginner: Bootstrap Sass 3.3.6 - Incorporating Javascript - Issue with Navbar Dropdown Not Expanding application.html.erb Head: <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> ...

Exploring the implementation of --history-api-fallback in webpack

let path = require('path') module.exports = { entry:path.resolve('public/src/index.js'), output: { path:__dirname + "/public", filename: "bundle.js" }, module: { loaders: [{ exclude: / ...

A guide on collapsing a React MUI Accordion when it loses focus

I'm working on a React project with a MUI Accordion, and I want it to collapse when the user clicks away from it on the page. My goal is to achieve this using CSS, similar to how I have implemented the following: .focus { /* border: 2px solid red ...

Intro.js is not compatible with React and Remix.run

I am currently working on implementing onboarding modals for header links using intro.js within a React environment. Below is the code snippet: import { useState, type FC } from 'react' import type { Links } from '../types' import &apo ...

Generate a fresh Date/Time by combining individual Date and Time components

I have set up a form to gather dates from one input box and times from another. var appointment_date = new Date(); var appointment_start = new Date("Mon Apr 24 2017 20:00:00 GMT-0400 (EDT)"); var appointment_end = new Date("Mon Apr 24 2017 21:30:00 GMT- ...