什么是CSS文件更好:许多语句或长语句?

时间:2015-01-23 19:32:34

标签: html css

只是想知道......我正在使用一个很长的CSS文件。 SAAS应用程序大约有1100行构建在大多数表中(嵌套非常深)。我无法更改HTML。该网站有各种风格。主要样式有一个基本样式表。对于用户的somne​​,有一个推翻样式表。这仅包含主要样式的更改。

考虑到这一点,我应该选择

.class_x{background-color:#123456;}
.class_y{background-color:#123456;}
.class_z{background-color:#123456;}
.class_p{background-color:#123456;}

或者这个:

.class_x, .class_y, .class_z, .class_p{background-color:#123456;}

其中一个更快吗?更好的方式?

1 个答案:

答案 0 :(得分:1)

喜欢使用第二种方式(小组)。

可以将类似的样式组合在一起以减少代码行,避免重复和帮助CSS的语义。

您可以按类似的惯例对项目进行分组,例如:

.class_x, .class_y, .class_z, .class_p{font-color: white}

.class_x{background-color:#333;}
.class_y{background-color:#111;}
.class_z{background-color:#999;}
.class_p{background-color:#666;}

当然,如果您只需更改其中一个类,请将其从组中删除并分开创建,例如:

.class_x, .class_y, .class_z{background-color:#123456;}
.class_p{background-color:#999999;}

看看这个页面(提示#7谈论它):http://www.queness.com/post/588/15-ways-to-optimize-css-and-reduce-css-file-size