是否可以在css中定义常量? [英] Is it possible to define constants in css?

查看:192
本文介绍了是否可以在css中定义常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的CSS样式表中使用了几种颜色。例如

I use a few colors throughout my CSS stylesheet. For example

#testdiv{
  background: #123456;
}

可以按名称定义颜色, css sheet like this

Is it possible to define that color by name so I can reference it in the css sheet like this

#testdiv{
  background: COLORNAME;
}


推荐答案

定义一个CSS类,并重新使用它在你要分配颜色的每个元素,而不是编写它到一个特定的元素。

It is probably a better practice to define a CSS class and re-use it on each element you want to assign the color to rather than coding it to a specific element.

像这样:

.darkBackground {
   background: #123456;
}

.smallText {
   font-size:8pt;
}

它也有助于知道一个元素可以应用多个类,将您的Constant元素值分成单独的类,并根据需要应用多个。

It also helps to know that an element can have multiple classes applied so you can break out your "Constant" element values into separate classes and apply more than one as needed.

<div id="myDiv1" class="darkBackground smallText"></div>
<div id="myDiv2" class="darkBackground bigText"></div>

这篇关于是否可以在css中定义常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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