如何更改FormControlLabel的插槽CSS?

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

How can I change the slot CSS of FormControlLabel?

问题

"mui5"

```jsx
"@mui/material": "^5.8.6",

我想要标签的字体是轻的。

如何更改FormControlLabel的插槽CSS?

我尝试了这个,但是仍然得到了粗体:

<FormControlLabel
  sx={{ label: { fontWeight: "light" } }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

如何更改FormControlLabel的插槽CSS?

但是当我尝试使用 CSS 类时它有效。

<FormControlLabel
  sx={{ '& .MuiFormControlLabel-label': { fontWeight: "light" } }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>
英文:

mui5

&quot;@mui/material&quot;: &quot;^5.8.6&quot;,

I want to have light font weight for label.

如何更改FormControlLabel的插槽CSS?

I tried this but i still get bold:

&lt;FormControlLabel
  sx={{ label: { fontWeight: &quot;light&quot; } }}
  value=&quot;range&quot;
  control={&lt;Radio /&gt;}
  label=&quot;DateTime Range&quot;
/&gt;

如何更改FormControlLabel的插槽CSS?

But when I tried using css class it works.

&lt;FormControlLabel
  sx={{ &#39;&amp; .MuiFormControlLabel-label&#39;: { fontWeight: &quot;light&quot; } }}
  value=&quot;range&quot;
  control={&lt;Radio /&gt;}
  label=&quot;DateTime Range&quot;
/&gt;

答案1

得分: 0

诚然,在MUI文档中,这一点可以更加清晰。

您可以使用以下自定义选项来覆盖组件的样式:

CSS表格中的第一列是使用主题级别的主题样式覆盖来自定义组件的“规则名称”。 (这些通常与插槽名称相匹配,但不应混淆。) 例如:

const theme = createTheme({
  components: {
    MuiFormControlLabel: {
      styleOverrides: {
        label: {
          color: "red"
        }
      }
    }
  }
});

第二列列出了可以用于通过sx/styled在组件[实现级别]上样式化组件的“全局类名”。 例如:

<FormControlLabel
  sx={{
    "& .MuiFormControlLabel-label": {
      color: "green"
    }
  }}
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

或者

const StyledFormControlLabel = styled(FormControlLabel)`
  .MuiFormControlLabel-label {
    color: blue;
  }
`;

...

<StyledFormControlLabel
  value="range"
  control={<Radio />}
  label="DateTime Range"
/>

工作中的CodeSandbox链接:https://codesandbox.io/s/theme-style-overrides-example-256pzg?file=/demo.tsx

附言

light不是有效的命名 font-weight(有效的命名值为normalboldlighterbolder),但sx会将light自动翻译为其相对字体重量theme.typography.fontWeightLight),而styledcreateTheme()不会自动执行此操作。

fontFamilyfontSizefontStylefontWeight属性将它们的值映射到theme.typography值:

<Box sx={{ fontWeight: 'light' }} />
// 等同于 fontWeight: theme.typography.fontWeightLight
英文:

Admittedly, this could be made a bit more clear in the MUI docs.

> You can override the style of the component using one of these
> customization options:
>
> - With a global class name.
> - With a rule name as part of the component's
> styleOverrides property in a custom theme.

The first column in the CSS table is the "Rule Name" for customizing the component using the Theme style overrides at the theme level. (These generally align with the slot names, but should not be confused for them.) For example:

const theme = createTheme({
  components: {
    MuiFormControlLabel: {
      styleOverrides: {
        label: {
          color: &quot;red&quot;
        }
      }
    }
  }
});

The second column lists the "Global class name" that can be used to style a component at the component [implementation] level via sx/styled. For example:

&lt;FormControlLabel
  sx={{
    &quot;&amp; .MuiFormControlLabel-label&quot;: {
      color: &quot;green&quot;
    }
  }}
  value=&quot;range&quot;
  control={&lt;Radio /&gt;}
  label=&quot;DateTime Range&quot;
/&gt;

or

const StyledFormControlLabel = styled(FormControlLabel)`
  .MuiFormControlLabel-label {
    color: blue;
  }
`;

...


&lt;StyledFormControlLabel
  value=&quot;range&quot;
  control={&lt;Radio /&gt;}
  label=&quot;DateTime Range&quot;
/&gt;

Working CodeSandbox: https://codesandbox.io/s/theme-style-overrides-example-256pzg?file=/demo.tsx

Aside

The value light is not a valid named font-weight value (valid named values are normal, bold, lighter, and bolder), but sx will translate light for you to it's relative font weight (theme.typography.fontWeightLight), whereas styled and createTheme() will not do it for you automatically.

> The fontFamily, fontSize, fontStyle, fontWeight properties map their
> value to the theme.typography value:
>
&gt; &lt;Box sx={{ fontWeight: &#39;light&#39; }} /&gt;
&gt; // equivalent to fontWeight: theme.typography.fontWeightLight
&gt;

huangapple
  • 本文由 发表于 2023年7月11日 10:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658458.html
匿名

发表评论

匿名网友

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

确定