【发布时间】:2022-01-26 14:23:58
【问题描述】:
我希望能够不必使用!important,而只需使用更具体的选择器即可解决。以这个元素为例:
<div>
<p className={`${headerStyles.headerOuter} ${bodyStyles.something} ${otherStyles.another}`}>Test</p>
</div>
它使用三个类,每个类都定义在单独的 css 模块化文件中:
import headerStyles from ‘…’
import bodyStyles from ‘…’
import otherStyles from ‘…’
假设 headerStyles.module.scss 包含:
.headerOuter {
color: blue;
}
bodyStyles.module.scss 包含:
div .something {
color: red;
}
而 otherStyles.module.scss 包含:
.another {
color: green;
}
p 将显示红色文本,因为 bodyStyles 更具体。
但我希望能够在 headerStyles.module.scss 中做到这一点:
.headerOuter {
&.another {
color: blue;
}
}
// or .headerOuter.another
这样 headerOuter 和 another 可以一起工作以比 bodyStyles 具有更高的特异性,以强制元素应用蓝色文本。但是问题是headerStyles和otherStyles好像不能互相识别。
如何解决这个问题?
我在这里做了一个类似的演示,文本应该是黑色的,但不是:https://codesandbox.io/s/css-modules-react-forked-mxtt6 - 查看 another.module.scss,文本应该是黑色
谢谢
【问题讨论】: