I have started using Material UI for a project. I created a TextField for a form field as shown below. I am trying to apply padding to this input field as demonstrated below, but the label is not being affected by this padding.
<TextField
sx={{ display: "flex", padding: "20px 0px" }}
name="mail"
label="Email"
type="mail"
id="mail"
placeholder="Enter email"
variant="standard"
inputProps={{
sx: { fontSize: "13px", marginTop: "4px" }
}}
InputLabelProps={{
shrink: true,
sx: {
fontSize: "12px"
}
}}
/>
What should I do in order to provide padding to the input along with the label?
2
Answers
The label is not affected by your padding changes because it is
position: absolute
making it apositioned element
. You can move the label around by addingtop, left, bottom, right
props to theInputLabelProps
sx
value.Here is a Code Sandbox repro with the label moved to align with the inputs padding.
And here’s a screenshot showing the result of adding
top
andleft
values to theInputLabelProps
stylingHope this Helps!
The input label has the absolute position. So padding and margin will not work here. top, bottom, right, and left positions will work. You can try the below code.