英文:
How can I change the slot CSS of FormControlLabel?
问题
"mui5"
```jsx
"@mui/material": "^5.8.6",
我想要标签的字体是轻的。
我尝试了这个,但是仍然得到了粗体:
<FormControlLabel
sx={{ label: { fontWeight: "light" } }}
value="range"
control={<Radio />}
label="DateTime Range"
/>
但是当我尝试使用 CSS 类时它有效。
<FormControlLabel
sx={{ '& .MuiFormControlLabel-label': { fontWeight: "light" } }}
value="range"
control={<Radio />}
label="DateTime Range"
/>
英文:
mui5
"@mui/material": "^5.8.6",
I want to have light font weight for label.
I tried this but i still get bold:
<FormControlLabel
sx={{ label: { fontWeight: "light" } }}
value="range"
control={<Radio />}
label="DateTime Range"
/>
But when I tried using css class it works.
<FormControlLabel
sx={{ '& .MuiFormControlLabel-label': { fontWeight: "light" } }}
value="range"
control={<Radio />}
label="DateTime Range"
/>
答案1
得分: 0
诚然,在MUI文档中,这一点可以更加清晰。
您可以使用以下自定义选项来覆盖组件的样式:
- 使用全局类名。
- 在自定义主题中作为组件的
styleOverrides
属性的一部分,使用规则名称。
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
值(有效的命名值为normal
、bold
、lighter
和bolder
),但sx
会将light
自动翻译为其相对字体重量(theme.typography.fontWeightLight
),而styled
和createTheme()
不会自动执行此操作。
fontFamily
、fontSize
、fontStyle
、fontWeight
属性将它们的值映射到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: "red"
}
}
}
}
});
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:
<FormControlLabel
sx={{
"& .MuiFormControlLabel-label": {
color: "green"
}
}}
value="range"
control={<Radio />}
label="DateTime Range"
/>
or
const StyledFormControlLabel = styled(FormControlLabel)`
.MuiFormControlLabel-label {
color: blue;
}
`;
...
<StyledFormControlLabel
value="range"
control={<Radio />}
label="DateTime Range"
/>
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:
>
> <Box sx={{ fontWeight: 'light' }} />
> // equivalent to fontWeight: theme.typography.fontWeightLight
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论