Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show both label and placeholder in material-ui TextField

Tags:

material-ui

In Material-UI (MUI) TextField, we can have both a label and a placeholder, but when the text field is empty, we can only see the label. We have to start editing the TextField in order to see the placeholder. Is there a way to say that both the label and placeholder should show initially when the field is empty? We'd also like to style the placeholder slightly differently by making it slightly gray compared to the black label text color. Is there a way to do this?

like image 287
Brian Avatar asked Jan 22 '26 08:01

Brian


1 Answers

you can force input label to shrink. https://mui.com/components/text-fields/#shrink

you can add input Text color with sx props on TextField. placeholder takes color from the input text color too. you can test this by removing inputProps props in below code.

if you want different colors for input text and placeholder use inputProps ref: Styling the placeholder in a TextField

<TextField
  sx={{
    "& .MuiOutlinedInput-root": {
      color: "red"
    }
  }}
  variant="outlined"
  label="Your custom label"
  placeholder="Placeholder Text"
  InputLabelProps={{ shrink: true }}
  inputProps={{
    sx: {
      "&::placeholder": {
        color: "green"
      }
    }
  }}
/>
like image 90
Prashant Jangam Avatar answered Jan 25 '26 21:01

Prashant Jangam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!