如何在CSS模块文件中覆盖全局CSS?
可以在我的global.css
next.js项目的文件中说:
.flex {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
我也有一个layout.js
component和layout.module.css < /代码>文件。组件看起来像这样:
import styles from "../styles/Layout.module.css";
const Layout = ({ children }) => {
return (
<div>
<div className={styles.navbar}>
<div className="flex">
<h1>Structured Safety</h1>
<nav>
<ul>
<li> <a href="#">Home</a> </li>
<li> <a href="#">Demo</a> </li>
</ul>
</nav>
</div>
</div>
</div>
);
};
export default Layout;
layout.module.css
is:
/* Navbar */
.navbar {
background-color: var(--primary-color);
color: #fff;
height: 70px;
}
.navbar ul {
display: flex;
}
.navbar .flex {
justify-content: space-between;
}
这样结构,我的.navbar .flex .flex
不会覆盖全局.flex 类,然后将
H1
从nav
拆分。我如何从这种组件样式中完成覆盖全球风格?
Lets say in my global.css
file of a Next.js project I have:
.flex {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
I also have a Layout.js
component and a Layout.module.css
file. The component looks like this:
import styles from "../styles/Layout.module.css";
const Layout = ({ children }) => {
return (
<div>
<div className={styles.navbar}>
<div className="flex">
<h1>Structured Safety</h1>
<nav>
<ul>
<li> <a href="#">Home</a> </li>
<li> <a href="#">Demo</a> </li>
</ul>
</nav>
</div>
</div>
</div>
);
};
export default Layout;
and the Layout.module.css
is:
/* Navbar */
.navbar {
background-color: var(--primary-color);
color: #fff;
height: 70px;
}
.navbar ul {
display: flex;
}
.navbar .flex {
justify-content: space-between;
}
Structured like this, my .navbar .flex
does not overwrite the global .flex
class and split the h1
from the nav
. How can I accomplish overwriting my global style from this component style?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于
.flex
是指您需要使用:Global
选择器在CSS模块中定位它。或使用替代语法。
Since
.flex
refers to a global class you'll need to use the:global
selector to target it in your CSS module.Or using an alternate syntax.
/** CSS模块文件**/
/** CSS MODULE FILE **/
在nextjs中,当你做出反应
从“__。CSS”
样式
成为变量,因此您必须在HTML中使用它才能生效。当前,您尚未使用layout.module.css中的任何样式,如果要使用CSS,则将您的html更改为:
&lt; div className = {styles.navbar}这样的..
In NextJS and React when you
import styles from "__.css"
thestyles
becomes a variable so you have to use it in your HTML for it to take effect.Currently you're not using any styles from your Layout.module.css, if you want to use that css you would change your html to:
<div className={styles.navbar}>
and such..