如何正确更改 MUI TextField 标签的字体大小

huangapple go评论53阅读模式
英文:

How to properly change the font size from MUI TextField Label

问题

我想要能够从Material UI TextField组件中更改标签的字体大小。我已经想出了InputLabelProps,它非常有效,但正如您在下面的图片中所看到的,当焦点在标签上时,标签太过窄。

这是我的TextField的实现:

<TextField {...params} InputLabelProps={{
    style : {color: '#518eb9', fontSize: 18, fontWeight: 1000}
}} label="Hovedkategori" />

我发现当焦点在标签上时显示的标签实际上来自另一个标签:

<legend class="css-1ftyaf0"><span>Hovedkategori</span></legend>
英文:

I would like to be able to change the label fontSize from the material ui TextField component. I've come up with the InputLabelProps which works great, but as you can see in the picture bellow the label is too squeezy when focused

如何正确更改 MUI TextField 标签的字体大小

Here is my implementation of the TextField

&lt;TextField {...params} InputLabelProps={{
    style : {color: &#39;#518eb9&#39;, fontSize: 18, fontWeight: 1000}
}} label=&quot;Hovedkategori&quot; /&gt;

I've found out that the label displayed when focused actually comes from another tag:

&lt;legend class=&quot;css-1ftyaf0&quot;&gt;&lt;span&gt;Hovedkategori&lt;/span&gt;&lt;/legend&gt;

答案1

得分: 1

如果你想解决这个问题,你应该在你的TextField组件中添加一个修改MuiOutlinedInput-notchedOutline类的CSS。使用sx你可以修改这个类,并设置相同的字体大小来解决你的问题。

"&.MuiOutlinedInput-notchedOutline": { fontSize: "28px" }
<TextField
    InputLabelProps={{
        sx: {
            color: "#518eb9",
            fontSize: "28px",
            fontWeight: 1000,
            "&.MuiOutlinedInput-notchedOutline": { fontSize: "28px" }
        }
    }}
    label="Hovedkategori"
/>
英文:

If you want resolve this problem you should add into your TextField component an css that modify MuiOutlinedInput-notchedOutline class.
With sx you can modify this class, and set the same font-size for fix your problem.

"&.MuiOutlinedInput-notchedOutline": { fontSize: "28px" }

&lt;TextField
    InputLabelProps={{
      sx: {
        color: &quot;#518eb9&quot;,
        fontSize: &quot;28px&quot;,
        fontWeight: 1000,
        &quot;&amp;.MuiOutlinedInput-notchedOutline&quot;: { fontSize: &quot;28px&quot; }
      }
    }}
    label=&quot;Hovedkategori&quot;
  /&gt;

答案2

得分: 1

你可以使用 MUI 的 Box 组件,并设置 fontSizefontWeight

const LabelValue = (label) => {
    return (
        <Box color="#518eb9" fontSize={18} fontWeight={1000}>
            {label}
        </Box>
    )
}

<TextField label={LabelValue("Hovedkategori")} />
英文:

You can use mui Box component and set fontSize and fontWeight

const LabelValue = (label) =&gt; {
    return (
        &lt;Box color=&quot;#518eb9&quot; fontSize={18} fontWeight={1000}&gt;
            {label}
        &lt;/Box&gt;
    )
}

 &lt;TextField label={LabelValue(&quot;Hovedkategori&quot;)} /&gt;

答案3

得分: 0

我遇到了同样的问题,尝试使用 Mui 主题提供程序创建一个覆盖样式主题。那里有许多小的 div,它们可能会妨碍你,隐藏它们或移动它们。 MUI 主题

英文:

I've encountered the same problem, try to use Mui Theme provider to create a override style theme. There are many small divs there that can get in your way, hide them or move them.
MUI themes

huangapple
  • 本文由 发表于 2023年3月7日 20:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定