问题描述
是否可以将颜色从一个 CSS 类导入到另一个?或者创建某种全局颜色定义,然后在多个类中使用它,这样如果您想更改颜色就不必在多个地方更新?
Is it possible to import a color from one CSS class to another? Or create some sort of global color define and then use it in multiple classes so that doesn't have to be updated in multiple places if you want to change a color?
例如,假设我有一个 CSS 类,我想将其用于特定元素,但我希望它使用分配给 Bootstrapdanger"类的相同颜色.有没有办法做到这一点?还是我只需要从 Bootstrap CSS 文件中复制颜色值并在我自己的类中使用它?
For example say I have a CSS class that I want to use for specific elements but I want it to use the same color assigned to the Bootstrap "danger" class. Is there any way to do that? Or do I just have to copy the color value from the Bootstrap CSS file and use it in my own class?
推荐答案
您可能还想查看新的 CSS 变量 在 Bootstrap 4 中.它们可以让您覆盖颜色,但您需要重新定义 CSS 类.
You may also want to look at the new CSS variables in Bootstrap 4. They will let you override colors, but you'll need to redefine the CSS class.
CSS 变量提供与 Sass 变量类似的灵活性,但在提供给浏览器之前无需编译.
例如:
.text-teal {
color: var(--teal);
}
Bootstrap 4 CSS 变量演示
Bootstrap 4 CSS Variables Demo
这篇关于CSS 使用来自另一个类的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!