CSS使用其他类的颜色吗? [英] CSS use color from another class?

查看:179
本文介绍了CSS使用其他类的颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将颜色从一个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类,希望用于特定元素,但我希望它使用分配给Bootstrap"danger"类的相同颜色.有什么办法吗?还是只需要从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变量.它们可以让您覆盖颜色,但是您需要重新定义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变量类似的灵活性,但是 无需编译即可投放到浏览器.

CSS variables offer similar flexibility to Sass’s variables, but without the need for compilation before being served to the browser.

例如:

.text-teal {
  color: var(--teal);
}

Bootstrap 4 CSS变量演示

Bootstrap 4 CSS Variables Demo

这篇关于CSS使用其他类的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆