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

Animate the leftward disappearance of a div to make it vanish

My goal is to create a user interface where users can navigate through different divs. Here is the HTML code: <article id="realize" class="realizeBox"> <div class="shown"> <div class="heading"> <h2>Realisati ...

Can we specify the type of a destructured prop when passing it as an argument?

I have implemented Material UI's FixedSizeList which requires rendering rows in the renderRow function and passing it as a child to the component. The renderRow function accepts (index, style, data, scrolling) as arguments from the FixedSizeList comp ...

Button background must be grayscale, while the text should remain unaffected

I have a variety of buttons with different background images. My goal is to apply a grayscale filter to the backgrounds by default and remove the filter on hover, without affecting the color of the button text. Currently, when I apply the grayscale filter ...

Bootstrap 4 Card Body Spinner Overlay with Flex Alignment

Seeking to center a spinner both vertically and horizontally within a bootstrap 4 card body. Despite trying my-auto, justify-content-center & align-items-center, it seems like I'm missing something. I've double-checked the display types and ...

IE is notorious for not playing well with CSS

I am facing an issue with my Google search box code. It works perfectly on Firefox, Chrome, and other browsers except for Internet Explorer. Can anyone help me troubleshoot this? Below is the code snippet: Abulkas Tablet #outerContainer{ width: ...

Is there a way to incorporate a floating label into react-select components?

Currently, I am in the process of constructing a web app using the MaterialUI theme which can be found at this link: Material Dashboard React. My main obstacle lies in the fact that MUI v2 does not come equipped with AutoComplete functionality, as the docu ...

Fade in/out overlay effect when clicking on a content block

I've hit a roadblock while trying to implement overlay fading in and out to cover a block created by some JavaScript code. Here is a link to the project. When you click on any of the continents, a series of blocks with country flags will appear. You& ...

Add custom CSS styles to a webpage using the CSS Style element retrieved from

I am working on an HTML page that has a TextArea and label. My goal is to create CSS classes in the TextArea and apply them to the label: <textarea id="txtCSS" name="txtCSS" rows="4" cols="50"> .FC{color:gr ...

The art of styling <li> elements compared to <a> elements

While these inquiries may lean towards being subjective, I am interested in learning about the industry's generally accepted best practices: 1) With responsive design and collapsible menus becoming more common, should a nav or nav menu element be sty ...

Struggling to properly center the footer on my Django template

I'm currently facing an issue when attempting to align my footer in the center of my Django template. The specific class for the footer is Pagination. Base HTML: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict ...

BottomNavigation wrapping React Router is failing to load the updated component

I'm struggling to understand why I can't seem to load the DMO page from a BottomNavigation component. Navigating to "/dom" works fine, but clicking the buttons doesn't do anything. The initial "/" loads perfectly. Any ide ...

How to customize the color of the clock symbol in a MUI TextField set to type 'time'

<TextField id="endTime" label="End Time" onChange={onEndTimeChange} type="time" defaultValue="16:00" className={classes.textField} /> By using the attribute 'type="time"', an ic ...

Footer button overrides list components due to improper implementation of vertical ion-scroll

Having some trouble setting up ion-scroll on a specific screen in my mobile application built with Ionic. On the Book page of my app, I'm encountering two main issues: https://i.stack.imgur.com/MnheG.png 1) The placement of the Confirm button doesn& ...

Displaying an additional section using hover effects in Bootstrap

I recently utilized Bootstrap to create pricing tables which can be viewed here: http://www.bootply.com/VyHsJBDoNc Is there a way for me to implement hover functionality on a span element (+ More Information!) that will display additional information as s ...

The CSS Validator does not recognize the CSS pointer-events and appearance properties

I'm a CSS beginner who recently created an app. I've encountered some errors and warnings when validating my CSS code: https://i.stack.imgur.com/g4MUz.png https://i.stack.imgur.com/Es1DE.png Could someone kindly explain these errors and warnin ...

Is StyledEngineProvider necessary for utilizing the sx prop with React and Mui v5?

In my current project, I am utilizing react along with Material-UI version 5 (MUI) and exclusively utilizing the sx prop for styling purposes. My question is, is it necessary to utilize the StyledEngineProvider as a wrapper for my ThemeProvider? Thank yo ...

Tips for aligning two TD elements with varying lengths in the same row:- Consider setting a specific width for each

Can anyone assist me in properly aligning this structure? I am looking to reduce the height of the "Todays Special" td significantly. The size of this td expands based on the content, for example, if new fruits are added. Any help in resolving this would ...

Arranging divs with CSS positioning

I struggle with positioning divs, especially in this particular situation. I am attempting to position boxes in the following layout: _ ___ _ |_|| ||_| _ | | |_||___| Is there a way to avoid manually defining pixel positions for each box and ins ...

The width of table columns is altered upon the second rendering in JQuery DataTables

Is there a way to prevent JQuery DataTables cells from extending width after adding hyperlink to cell? I am facing an issue where I have two tables on the same page. The first table column has a link which populates the second table. It works fine when th ...

Organizing ToggleButtonGroup in Material Ui: Creating a Grid Layout with 2 Rows and 2 Columns

When using grid within toggle buttons, the OnChange event does not work The event works fine when using Grid or Stack How can I arrange 2 by 2 buttons? I'm unable to find any example of this on the material ui website Could someone please offer as ...