[英]How to center the label in MUI Textfield
我想将标签和帮助文本居中在文本字段的中间。 我通过向 MuiInputLabel-root 添加填充来实现它。 但这对其他屏幕尺寸没有响应(文本字段更改尺寸并且标签不再位于中心)。 我试过使用textAlign: 'center'
但它不起作用。
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, }} />
这是我能够解决您的问题的方法(更新的代码)。
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 completly
},
"& .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
/>
);
}
这是尝试使用的更新代码框。 让我知道你的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.