admin管理员组文章数量:1426215
I want to center the label and helper text in the middle of the Textfield. I got it working with adding a padding to MuiInputLabel-root. But this is not responsive on other screen sizes (Textfield changes size and the label isnt in the center anymore). I have tried using textAlign: 'center'
but it doesnt work.
Currently I have the Textfield with label on the left: Current
I want to center the label and eventually helper Text too: Expected
Currently when focused paddingLeft is set to 0 again: Focused
const StyledTextField = styled(TextField)({
'& .MuiInputLabel-root': {
paddingLeft: '85px',
},
'& label.Mui-focused': {
color: '#000000',
paddingLeft: '0px',
},
<StyledTextField
helperText="Privacy Policy "
InputProps={{
inputProps: {
style: { textAlign: 'center' },
},
}}
margin="normal"
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
sx={{
m: 1,
}}
/>
I want to center the label and helper text in the middle of the Textfield. I got it working with adding a padding to MuiInputLabel-root. But this is not responsive on other screen sizes (Textfield changes size and the label isnt in the center anymore). I have tried using textAlign: 'center'
but it doesnt work.
Currently I have the Textfield with label on the left: Current
I want to center the label and eventually helper Text too: Expected
Currently when focused paddingLeft is set to 0 again: Focused
const StyledTextField = styled(TextField)({
'& .MuiInputLabel-root': {
paddingLeft: '85px',
},
'& label.Mui-focused': {
color: '#000000',
paddingLeft: '0px',
},
<StyledTextField
helperText="Privacy Policy "
InputProps={{
inputProps: {
style: { textAlign: 'center' },
},
}}
margin="normal"
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
sx={{
m: 1,
}}
/>
Share
Improve this question
asked Jul 9, 2022 at 18:43
user18943198user18943198
1 Answer
Reset to default 3Here is how I was able to solve your issue (updated code).
const StyledTextField = styled(TextField)({
"& .MuiInputLabel-root": {
right: 0,
textAlign: "center"
},
"& .MuiInputLabel-shrink": {
margin: "0 auto",
position: "absolute",
right: "0",
left: "0",
top: "-3px",
width: "150px", // Need to give it a width so the positioning will work
background: "white" // Add a white background as below we remove the legend that had the background so text is not meshing with the border
// display: "none" //if you want to hide it pletly
},
"& .MuiOutlinedInput-root.Mui-focused": {
"& legend ": {
display: "none"
}
}
});
export default function BasicTextFields() {
return (
<StyledTextField
helperText="Privacy Policy "
InputProps={{
inputProps: {
style: { textAlign: "center" }
}
}}
margin="normal"
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
/>
);
}
Here is the UPDATED codesandbox to try it. Let me know what you think.
本文标签: javascriptHow to center the label in MUI TextfieldStack Overflow
版权声明:本文标题:javascript - How to center the label in MUI Textfield - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745415176a2657651.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论