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

Bringing elements into perfect harmony in Angular

Seeking guidance on HTML alignment using bootstrap for prebuilt components like buttons. Struggling with aligning divs and looking to learn more about grouping them together for proper alignment. Goal is to center elements on screen one above the other. ...

Ways to troubleshoot the following issue:u003cemailu003e: The module is not compatible with the "node" engine

linix@linix-HP-ProBook-6475b:~/projects/builds$ npx create-next-app hellochat npx: installed 1 in 2.333s Creating a new Next.js app in /home/linix/projects/builds/hellochat. Installing react, react-dom, and next using yarn... yarn add v ...

Formik is encountering errors and warnings while loading the Material UI autocomplete component

Hey there! I've been working on a code sample for an Autocomplete component using Material UI in next JS with formik. Unfortunately, I'm running into some errors and warnings upon loading the code. Despite trying various solutions found online, n ...

How to dynamically change the border-top color of an <a> tag in a menu using a jQuery loop

I am interested in adding a colorful border-top to my menu using jQuery. I know this can be achieved with HTML by including style="border-top-color: red;", but I want to explore the jQuery method. Here is what I have attempted so far: var colors = [ ...

Creating movement in an SVG patterned background

Currently in the process of designing a brand new website using NextJS and Tailwind. I am looking to incorporate an infinitely translating background effect that moves towards the bottom right, similar to this example (but with my own unique pattern): htt ...

Set up the sw-precache WebPack plugin so that it will route to a server-rendered page when loading the navigateFallback path

Here's a situation to consider: The HTML for the "/" route of my single page application is dynamically generated by my express server. During offline periods, I want this same dynamically generated HTML to be re-served as the service worker navigat ...

There is a lack of CSS import in the HTML document

I have organized my files in the following structure: - index.html - css/styles.css This is the HTML code I am using: <!DOCTYPE html> <html lang="en> <head> <title>Bootstrap Case</title> <meta charset="utf-8"> & ...

organizing various kinds of data values within an array

Within this array, I have two types of variables - an integer and a string. My goal is to sort the array either alphabetically or by the length of the string. However, it seems that the sorting algorithm is prioritizing the integer over the string. ...

Web design featuring an aesthetic reminiscent of an open folder on an iPhone

I'm struggling a bit and could really use some assistance. I've been searching for a long time but can't seem to figure out how to create an iPhone folder-like design for the web. The design should have that "slide out" effect when you click ...

Ways to Halt Every Single CSS Animation

Is there a way to stop all CSS animations in a document from the beginning? My idea is to assign a class to elements containing CSS animations at the start, such as '.paused'. Then, using jQuery in my JavaScript, I would remove the '.paused& ...

Header Menu Click Causes Blurring of Website Background

I need help adding a blur effect to my site background only when the header menu is clicked and active. Once the menu is activated, the .ct-active class is added to it. The CSS code below works for styling individual items - the menu turns red when activ ...

I'm encountering a RangeError in nextjs when trying to pass props to a child component

I'm a beginner with Next.js. I've been attempting to pass props to a child component that is a response from an API call. However, every time I try to add the props in the child component, I encounter a RangeError: Maximum call stack size exceed ...

React Native is designed to easily stick an image to the bottom of the screen

I am struggling to make the image stick to the bottom of the app without any extra space underneath it. Despite trying various solutions from Stack Overflow, I have been unable to achieve the desired result. The issue is the large white space between the r ...

Stop cascading styles when using nested rules in LESS to prevent unintended style application

In the process of developing a sophisticated front-end system using plugins, we are exploring different methods for composing CSS rules. Currently, we are considering two main approaches: Including parents in the class name Nesting parents in the selecto ...

What is the correct method for aligning to the right or left within the material-ui appbar using material-ui-next version?

Having trouble getting the login/logout buttons to float right using material-ui-next ("material-ui": "^1.0.0-beta.22"). It looks like they removed iconElementRight= from the api. Is the new solution to use <Grid> in the appbar? Seems a bit clunky. W ...

Looking to showcase your logo prominently at the center of the page, flanked by two elegant

Is it possible to create a grid-like structure with dimensions 5.5-1-5.5? I would like to center a logo on the page with two lines on the left and right. (Please excuse my limited English skills) https://i.sstatic.net/cgjfS.png ...

Angular Material: Enhanced search input with a universal clear button

After searching for a cross-browser search control with a clear button similar to HTML5, I found the solution rendered by Chrome: <input type="search> The code that gave me the most relevant results can be found here. I used the standard sample w ...

The error encountered during rendering: the specified element type is not valid - it should be a string or a class/function,

Hey there, I'm just starting out with react native and have a question about using button elements. I found this helpful website: Unfortunately, when I try to run it, I keep encountering an error. I've tried some solutions from the website but h ...

Central alignment of div with cursor

I'm experimenting with creating a unique custom cursor using a <div> that trails the movement of the mouse pointer. While the current setup works smoothly, I've noticed that when scrolling down the page, the div lags behind until the scrol ...

Issue arising during the connection between Node.js and MySQL, which is linked to MongoDB using JWT authentication

Encountering an issue while connecting Node.js with MySQL, where it is already connected with MongoDB using JWT authentication. I am currently utilizing Node.js MongoDB JWT redux-based authentication but transitioning to a MySQL database has resulted in an ...