Modify the textfield label color using Material-UI

Hey there! I'm running into a little issue when trying to change the color of the text label in a MUI text field. I've successfully customized the border colors and hover states, including the label, but for some reason, I can't seem to get the label color to change when not in the hover state. I've experimented with different class names (including MuiInputBase-input) that I found in the DOM just like I did with the others, but unfortunately, nothing seems to work. I also gave inputProps a shot, but that didn't have any effect either. Here's the code snippet I'm working with:

               <TextField 
                    className="w-full my-2 "
                    id="outlined-basic" 
                    label="Distance (miles)" 
                    inputProps={{ sx: {color: '#F1F4F9'} }} <- this doesn't do anything 
                    variant="outlined"
                    onChange={(e) => {setSearchParams({...searchParams, dist: e.target.value})} }
                    sx={{
                        // focused border color
                        "&& .Mui-focused .MuiOutlinedInput-notchedOutline": {  
                            border: "1px solid #3B82F6",
                        },

                        // focussed label color
                        "&  .css-1sumxir-MuiFormLabel-root-MuiInputLabel-root.Mui-focused": {
                            color: "#3B82F6",
                        },

                        // normal border color 
                        "& .MuiOutlinedInput-notchedOutline": {  
                            border: "1px solid #F1F4F9",
                        },
                        
                        // normal label color - <- DOESN'T DO ANYTHING
                        "& .MuiInputBase-root-MuiOutlinedInput-root": {
                            color: "#F1F4F9"
                        },
                    }}
                                       />

Answer №1

Here's a simple way to update the text color of a label:

    <TextField
      className="w-full my-2 "
      id="outlined-basic"
      label="Distance (miles)"
      InputLabelProps={{
        sx: { color: "red", "&.Mui-focused": { color: "green" } },
      }}
      variant="outlined"
    />  

Helpful hints:

  • Avoid using or copying classes directly from the DOM, as they may change!
    .MuiFormLabel-root YES
    .css-1sumxir-MuiFormLabel-root - NO

  • Using double ampersands is not valid syntax
    & .MuiFormLabel-root YES
    && .MuiFormLabel-root NO

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 ...

Exploring the Power of JQuery Load and CSS styling

Currently, I am dynamically loading text into a div. However, I am encountering an issue where the content below this div is constantly shifting depending on the loaded text. Is there a way to restrict the space allocated to the div with the dynamic conte ...

Assistance with Validating Forms Using jQuery

I have a form located at the following URL: . For some reason, the form is not functioning properly and I am unsure of the cause. Any suggestions or insights on how to fix it? ...

Encountering an "Unmet Peer Dependency" error message while attempting to integrate bootstrap-ui into my Angular project

Currently, my goal is to successfully install angular-ui. Following the tutorials, I have attempted all commands such as: npm install angular-bootstrap However, this command results in an error message: +-- UNMET PEER DEPENDENCY angular@>=1.5 After ...

Using @font-face to apply a font on an SVG file is not supported

I attempted to incorporate custom fonts into my SVG file, but unfortunately, my efforts were unsuccessful. While I have successfully implemented custom fonts on regular HTML/CSS webpages before, I encountered difficulties when trying to do the same with SV ...

Adjust the background color of the container when transitioning to a new slide in a Slick carousel

I am using a Slick slider where each slide has its own background color. The container color is set to white, but I want the container color to change instantly to match the color of the currently active slide when swiping or changing slides. This is my J ...

Styling with floated divs in CSS

I'm facing a rather unique issue while working on creating a menu for a web page. The main challenge lies in ensuring that the parent element containing floated child divs adjusts its height dynamically, especially when changes are made to the padding ...

list without specific order spacing

I have implemented an unordered list in the footer section of my HTML document. Within this list, the first two items consist of social media logos displayed as images. To enhance their visibility and prominence within the footer, I have assigned them a wh ...

"When using FireFox, you can easily create a hyperlink that scrolls to a specific section of a webpage,

I have created a style for image button: .sticky { position: fixed; top: 50px; width: 120%; z-index: 1; } @media screen and (min-device-width : 100px) and (max-width: 600px) { .sticky{ display:none; } } and also implemented a script for it: ...

Methods for concealing the title and date when printing web content using JavaScript

When utilizing the window.print method to print out a specific screen, I encountered an issue. I need to hide the date in the top left corner of the image as well as the title (not the big heading) which has been intentionally blurred. I've come acro ...

What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen? <span style="" class="yavbc-color-tip"><span style="">Some text</span></span&g ...

Having trouble getting the useState hook to work properly in a Next.js application when using a

Having trouble with the useState hook. I've tried to find a solution by searching through various forums like https://github.com/vercel/next.js/issues/7626, https://github.com/vercel/next.js/issues/17592, and also came across discussions on Stack Over ...

Deactivate the production css classnames of Material-UI when using React

Currently, I am utilizing Material UI for React and I have encountered an issue with how it manages classnames when NODE_ENV=production. Specifically, in development mode the class is displayed as .MuiAppBar-root-12, whereas in production mode it shows up ...

Encountering the following error: Uncaught TypeError: Super expression must be null or a function, not object

After executing the code below, the browser displays the following errors: Uncaught TypeError: Super expression must either be null or a function, not undefined Uncaught TypeError: Cannot read property '__reactInternalInstance$uuzyb025gr28cqm0v65ka9 ...

Developing gaps or margins among table components

Struggling to create spacing between text and an image in an HTML email, but the image just won't budge no matter what I do. Check out my Fiddle here: https://jsfiddle.net/rwcgs0g8/ All I want is for the red "Download your free" image to shift down ...

Ways to align content on navbar (Bootstrap 5)

I am currently working on a minimal navbar design using Bootstrap 5. In this setup, there are two main elements - the 'navbar-brand' for the logo/brand and the navigation links. My goal is to have the brand/logo aligned all the way to the left, ...

Chart displaying an errant scrollbar at the bottom of the screen

Having trouble with a table that displays a scrollbar when a specific row is added. The first and second rows in the code below display correctly. However, adding the third row within the foreach loop causes a scrollbar to appear at the bottom. This issu ...

The outline feature cannot be applied to buttons within a btn-group in Bootstrap 4

I have successfully removed the outline for a single button, but I am struggling to remove it for a button within a btn-group class. Check out my JSFiddle DEMO This is the HTML code I am working with: <button class="btn btn-primary">Button without ...

Image not showing up on HTML page following navigation integration

Having trouble with my responsive website setup - the background image for ".hero-image" isn't showing up after adding the navigation. Google Chrome console is throwing a "Failed to load resource: the server responded with a status of 404 (Not Found)" ...

Using a background image from your own website does not display anything, but using an image from an

It's really strange from where I'm standing. I'm working on a website and trying to set up a banner with a background image using the background-image property. The issue I'm facing is: when I use two images with the same dimensions, ...