Is there a way to customize the slot CSS for FormControlLabel?

mui5

"@mui/material": "^5.8.6",

In order to achieve a lighter font weight for the label, I encountered an issue.

https://i.sstatic.net/2JcpY.png

Despite my efforts, the text still appears in bold:

<FormControlLabel
  sx={{ label: { fontWeight: "light" } }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

https://i.sstatic.net/Qcxlf.png

Interestingly, when I utilized a CSS class, it did work as expected.

<FormControlLabel
  sx={{ '& .MuiFormControlLabel-label': { fontWeight: "light" } }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

Answer №1

It's worth noting that the MUI documentation could benefit from more clarity on this topic.

In order to customize the style of a component, you have a few options to choose from:

The first column in the CSS table contains the "Rule Name" for customizing a component using Theme style overrides at the theme level. (These are generally aligned with the slot names, but should not be mistaken for them.) For example:

const theme = createTheme({
  components: {
    MuiFormControlLabel: {
      styleOverrides: {
        label: {
          color: "red"
        }
      }
    }
  }
});

The second column displays the "Global class name" that can be utilized to style a component at the implementation level via sx or styled. Here's an example:

<FormControlLabel
  sx={{
    "& .MuiFormControlLabel-label": {
      color: "green"
    }
  }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

or

const StyledFormControlLabel = styled(FormControlLabel)`
  .MuiFormControlLabel-label {
    color: blue;
  }
`;

...

<StyledFormControlLabel
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

View Working CodeSandbox Example: https://codesandbox.io/s/theme-style-overrides-example-256pzg?file=/demo.tsx

Additional Information

Please note that the value "light" is not considered a valid named font-weight value (valid named values include normal, bold, lighter, and bolder). However, when using sx, it will automatically map "light" to its relative font weight (theme.typography.fontWeightLight), while styled and createTheme() won't perform this mapping for you by default.

The properties like fontFamily, fontSize, fontStyle, and fontWeight will align their value with the theme.typography setting:

<Box sx={{ fontWeight: 'light' }} /> 
// equivalent to fontWeight: theme.typography.fontWeightLight 

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

Activate Click, or Pop-up Box

I've been attempting to log in to the Udemy site using JavaScript, but I'm having trouble triggering the click action on the "log in" link. Unfortunately, the .click() method doesn't seem to be working when I try to select the element. The l ...

You cannot render objects as a React child (discovered: object containing keys {id, name}). To display a collection of children, make sure to use an array instead

My goal is to retrieve data from an API and present it in a table using the material-table library. I suspect that I am struggling to properly manipulate objects and arrays. The response from my API is as follows: { "data": [ { " ...

Arranging list items on top of each other without using absolute positioning

Hey there! I am trying to figure out a way to stack list items containing images on top of each other, like a deck of cards, but without resorting to absolute positioning. I attempted to do it using absolute positioning, here's what I came up with: ...

Leveraging an external script for enhanced functionality in React/Meteor application

I'm currently facing a challenge incorporating an external script into my React component within Meteor. I've experimented with directly placing the script tag in my component as follows: TheLounge = React.createClass({ render() { return ( ...

Vertical scroll bar positioned on the left side of a DIV

Can a vertical scroll bar be placed on the left side of a DIV using CSS? What about JavaScript? ...

React.js issue: "Unable to iterate over 'this.state.product.response' as it is not a function"

I am a complete beginner diving into the world of React.js while working on constructing a CRUD application using Visual Studio 2017 with an MVC framework and Entity framework. I seem to have hit a roadblock and have been stuck for quite some time attempti ...

Are there any tools available that can generate CSS code automatically

Our team offers a pre-set CSS file along with the HTML mock-up for partners to customize, such as changing colors and background images, to match their desired aesthetic. They then provide feedback on the modified CSS files back to us. However, we face a ...

Tips for integrating npm packages into Rails applications

I am experimenting with integrating the Ace editor into my Ruby on Rails application, which mainly consists of React components. I am utilizing the react-rails gem and opting not to use flux at this time. I came across the react-ace package, but it requir ...

3-Column Layout with Left-Floated Columns Displaying Incorrect Order

Currently, I am in the process of converting a 3-column website to be responsive by using percentages and em's instead of negative pixel margins. This will ensure that the site functions well on various devices. I am facing a similar issue as discuss ...

The origin of the image is specified within the HTML document

I recently encountered an issue while trying to include images in my HTML file. When I used a local file path like background-image: url('file:///C:/Users/faycel/Desktop/site%20guide/paris.jpg'), the image displayed correctly. However, when I tri ...

Guide on how to show a selection filter next to the table column in Material-Table ReactJS

I am utilizing the tool material-table to showcase table data while also incorporating filters for a specific column located outside of the table. For instance: https://material-table.com/#/docs/features/filtering My goal is to filter only the "birth pl ...

What is causing the sorting table to fail in React when using useState?

import React, { useState } from "react"; import "./App.css"; const App = () => { const [data, setData] = useState([ { rank: 1, name: "John", age: 29, job: "Web developer", }, { rank: 2, name: "Micha ...

Exploring the Depths of React by Cycling Through Arrays in Tabular Format

One issue I'm facing is that I have an array named paymentMethods which I'd like to iterate through in tabs. However, I seem to be struggling with the iteration part. To take a closer look at my code, please visit my codesandbox HERE <div& ...

JS Nav Dots are not activating the Active Class

I have been utilizing a code snippet from this source to incorporate a vertical dot navigation feature into a single-page website. The navigation smoothly scrolls to different sections when a link is clicked, with an active highlight on the current section ...

The file type stored in Firebase storage is identified as 'octet-stream' rather than the expected 'png/jpg' format

I am experiencing an issue with uploading images to Firebase storage. After uploading them, I notice that they are labeled as application/octet-stream instead of the expected types like image/jpeg or image/png. Below is the code snippet I am using: <in ...

How can I extract a substring from a URL and then save it to the clipboard?

Hi there! I'm working on a project for my school and could really use your expertise. I need help extracting a substring from a URL, like this one: URL = https://www.example.com/blah/blah&code=12432 The substring is: 12432 I also want to display ...

Dynamic resizing of grids using React and TypeScript

I'm attempting to create a dynamic grid resizing functionality in React and TypeScript by adjusting the lgEditorSize value on onClick action. Setting the initial lgEditorSize value const size: any = {}; size.lgEditorSize = 6; Adjusting the lgEditorS ...

What is the process of displaying an image every 5 seconds in React?

Each time you visit this URL: , a new image is displayed. I am trying to make my React component show a different image with this URL every 5 seconds, but I'm having trouble. Here is the code I have: import { useEffect, useState } from "react"; ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Retrieving data from Firebase query and displaying as an array of results

Currently utilizing the @react-native-firebase wrapper for interaction with Firebase's Firestore. Within my function, some querying to a specific collection is performed, with the expected result being an Array object containing each located document. ...