【问题标题】:how to apply scss styles my CRA app with Material UI?如何使用 Material UI 应用我的 CRA 应用程序的 scss 样式?
【发布时间】:2020-09-13 01:51:09
【问题描述】:

我通过 CRA 制作了 React 应用程序。 我将使用材质 ui 和 SCSS 样式。

我使用 node-sass 制作了 main.css 文件。

我尝试将 main.css 文件应用到 Material-ui Typography。

在 main.css 中

.logoTitle {
  font-size: 20rem;
}

但是如果加载了我的网页版式元素不应用 logoTitle 样式..

我有什么问题..?

【问题讨论】:

  • 请将您的代码示例作为文本(就像您对 css 所做的那样)而不是图像。

标签: css reactjs sass material-ui


【解决方案1】:

CSS specificity 相同时,CSS 中最后声明的样式将获胜。默认情况下,Material-UI 将其样式放置在 <head> 元素的末尾。这意味着 Material-UI 中的默认样式将胜过 <head> 中声明的具有相同特性的其他样式(例如您的 font-size 案例)。您可以通过使用带有injectFirst 属性的StylesProvider 元素包装应用程序的顶级来更改此行为。这将导致 Material-UI 将其样式放置在 <head> 元素的开头。

这是一个工作示例:

App.js

import React from "react";
import Typography from "@material-ui/core/Typography";
import { StylesProvider } from "@material-ui/core/styles";
import "./styles.css";

export default function App() {
  return (
    <StylesProvider injectFirst>
      <Typography variant="h1" color="primary" className="logoTitle">
        Hello
      </Typography>
    </StylesProvider>
  );
}

styles.css

.logoTitle {
  font-size: 20rem;
}

相关回答:How to overwrite styles with classes and css modules?

文档:https://material-ui.com/styles/api/#stylesprovider

【讨论】:

  • 感谢您的努力...!好善良
猜你喜欢
  • 2022-06-22
  • 2021-12-01
  • 2020-03-31
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 1970-01-01
  • 2019-04-29
  • 1970-01-01
相关资源
最近更新 更多