1

我想知道何时以及如何对重复的属性进行分类。考虑结构之间的可读性、代码性能、优缺点。假设我有两个 CSS 代码:

第一个代码:

.a {
    text-align: center;
    font-size: 1em;
    background-color: red;
}

.b {
    text-align: center;
    font-size: 1em;
    background-color: green;
}

.c {
    text-align: center;
    font-size: 2em;
    background-color: blue;
}

.d {
    background-color: blue;
}

第二个代码:

.a,
.b,
.c {
    text-align: center;
}

.a,
.b {
    font-size: 1em;
}

.c,
.d {
    background-color: blue;
}

.a {
    background-color: red;
}

.a {
    background-color: green;
}

.c {
    font-size: 2em;
}

那么哪一个是更好的家伙,非常感谢之前:)

4

1 回答 1

1

关于可读性和结构,我认为这取决于您的应用程序的复杂性和组织。

为了组织您的 CSS 代码,存在常见的做法,当您在大型应用程序中工作并且涉及多个开发人员时,这特别有用。

边界元法

块、元素、修饰符——是一种命名方法。这是一种命名类的聪明方法,赋予更多的意义和可读性。

更多信息在这里:

https://css-tricks.com/bem-101/

http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/

SMACSS

代表 CSS 的 Scalable and Modular Architecture,它更像是 CSS 的样式指南。

更多信息在这里:

https://smacss.com/

于 2015-10-12T11:42:36.220 回答