You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何自定义Material UI TextField组件失焦状态下的标签文字颜色

你需要在makeStyles的样式定义中新增cssLabel类配置,用来控制标签失焦状态下的默认颜色,你当前代码已经在InputLabelProps中指定了标签的root类为classes.cssLabel,只需要补充对应的样式规则即可。

修改后的样式代码如下:

const useStyles = makeStyles({
  // 新增:控制失焦状态下标签的默认颜色
  cssLabel: {
    color: "blue !important", // 此处替换为你需要的失焦标签颜色
  },
  notchedOutline: {
    color: "red !important", // label focus color 
    borderWidth: "1px",
    borderColor: "red !important" // border color when out of focus
  },
  cssOutlinedInput: {
    color: "green !important", // text color when out of focus
    "&$cssFocused $notchedOutline": {
      borderColor: `yellow !important` // border color when Focused
    }
  },
  cssFocused: {
    color: "red !important" // text and label color when focused
  }, 
});
  • 你可以把cssLabel中color的值替换为任意你需要的失焦标签颜色值
  • 现有TextField组件的InputLabelProps配置已经绑定了cssLabel类,不需要额外修改组件代码,补充样式后即可生效。

内容的提问来源于stack exchange,提问作者Gil Edri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 17:06:00