避免在CSS中重复的常量 [英] Avoiding repeated constants in CSS

查看:87
本文介绍了避免在CSS中重复的常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么有用的技术来减少CSS文件中常量的重复?

Are there any useful techniques for reducing the repetition of constants in a CSS file?

(例如,一堆不同的选择器应该应用相同的颜色,或相同的字体大小)?

(For example, a bunch of different selectors which should all apply the same colour, or the same font size)?

推荐答案

元素可以属于多个类,所以你可以这样做:

Elements can belong to more than one class, so you can do something like this:

.DefaultBackColor
{
    background-color: #123456;
}
.SomeOtherStyle
{
    //other stuff here
}
.DefaultForeColor
{
    color:#654321;
}

然后在内容部分:

<div class="DefaultBackColor SomeOtherStyle DefaultForeColor">Your content</div>

这里的弱点是它在身体里变得很冗长,你不太可能得到它只列出一种颜色一次。但你可能只能做两到三次,你可以将这些颜色组合在一起,也许在自己的表。现在当你想改变颜色方案他们都在一起,变化很简单。

The weaknesses here are that it gets pretty wordy in the body and you're unlikely to be able to get it down to listing a color only once. But you might be able to do it only two or three times and you can group those colors together, perhaps in their own sheet. Now when you want to change the color scheme they're all together and the change is pretty simple.

但是,是的,我最大的抱怨CSS是无法定义您自己的常数。

But, yeah, my biggest complain with CSS is the inability to define your own constants.

这篇关于避免在CSS中重复的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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