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

5

14

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?

Correggio answered 7/1, 2022 at 16:17 Comment(0)
B
25

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"
      }
    }
  }}
/>
Burkey answered 7/1, 2022 at 19:33 Comment(0)
C
5

iam still looking the best resolution..

my current approach

<TextField focused label="Standard" placeholder='lorem ipsum' />

or

 <TextField  InputLabelProps={{ shrink: true }} label="Standard" placeholder='lorem ipsum' />
Callant answered 21/5, 2022 at 5:52 Comment(0)
D
5

This works for me:

<TextField  InputLabelProps={{ shrink: true }} label="Name" placeholder='lorem ipsum' />
Dov answered 1/6, 2022 at 23:7 Comment(0)
A
0

You can use placeholder and label both. but, when the user click on the textfield, the placeholder will be displayed

<TextField id="standard-basic" placeholder="enter" label="Standard" variant="standard" />

https://codesandbox.io/s/basictextfields-material-demo-forked-i7tcn?file=/demo.js

Amaranth answered 7/1, 2022 at 16:28 Comment(1)
Yes, in addition to seeing the label before the user edits the field, we would like to show the placeholder before the user clicks on the field (similar to how a placeholder works for a regular input). This allows the user to see the help information in the placeholder before they interact with the text field.Correggio
R
0

In actually it is not a placeholder in textfield. It is label. It changes with simple classname of textfield "MUI form label root". Assign colour on normal way.

Retro answered 6/10, 2022 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.