Kat*_*ie7 3 css styles jsx reactjs material-ui
我有一个使用 Material UI 构建的 React 应用程序。我已经创建了自己的主题覆盖(请参阅下面的摘录),但字体的颜色需要是紫色 #391960,而不是 rgba(0, 0, 0, 0.87)。如何覆盖整个网站的默认字体颜色?
这是我的主题的摘录
import { createMuiTheme } from "@material-ui/core";
const theme = createMuiTheme({
palette: {
text: {
light: "#ffffff",
main: "#391960",
dark: "#140b1d",
},
});
export default theme;
Run Code Online (Sandbox Code Playgroud)
我希望在 theme.js 文件中添加上述内容会导致所有字体颜色更改为 #391960,因为我尚未对组件内的字体应用任何样式。例如:
import theme from "./styles/theme";
<ThemeProvider theme={theme}>
<Typography variant="h1" component="h1">
Page Title
</Typography>
</ThemeProvider>
Run Code Online (Sandbox Code Playgroud)
但是,上述 Typography 组件中的文本仍保持深灰色。我成功地能够使用以下代码更改此版式组件的大小,因此这证明我正确地将主题拉入组件中:
typography: {
h1: {
fontSize: "2rem",
fontWeight: 700,
},
Run Code Online (Sandbox Code Playgroud)
如果我无法使用 theme.js 文件设置站点范围的颜色,我想我可以有一个全局样式表,以某种方式从主题中提取正确的颜色?例如(我知道这行不通!)
body {
color: theme.palette.main
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!
谢谢,
凯蒂
小智 5
有两件事不允许您的主题得到有效应用。
palette.text为参考,这些是对象当前的默认属性const theme = createMuiTheme({
palette: {
text: {
primary: 'rgba(0, 0, 0, 0.87)',
secondary: 'rgba(0, 0, 0, 0.54)',
disabled: 'rgba(0, 0, 0, 0.38)',
hint: 'rgba(0, 0, 0, 0.38)',
},
},
});
Run Code Online (Sandbox Code Playgroud)
如果您想设置不同的全局原色,那么您的 theme.js 文件应如下所示:
import { createMuiTheme } from '@material-ui/core/styles';
const theme = createMuiTheme({
palette: {
text: {
primary: '#391960',
},
},
});
export default theme;
Run Code Online (Sandbox Code Playgroud)
请注意,如果您也想覆盖这些属性,您还可以为其余属性(辅助、禁用、提示)设置不同的颜色。
Material-UI 提供了一个 CssBaseline 组件来启动一个优雅、一致且简单的构建基线。
import React from "react";
import { ThemeProvider, CssBaseline, Typography } from "@material-ui/core";
import theme from "./theme";
export default function App() {
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Typography variant="h1" component="h1">
Page Title
</Typography>
</ThemeProvider>
);
}
Run Code Online (Sandbox Code Playgroud)
这里是CodeSandbox的功能复现,希望能对大家有所帮助。
| 归档时间: |
|
| 查看次数: |
3265 次 |
| 最近记录: |