CSS 格式的优点和缺点

发布于 2024-09-15 16:23:28 字数 610 浏览 2 评论 0原文

CSS 格式的优缺点

我知道四种 CSS 格式。 您认为哪个更好?

A) 经典

.class1 {
}
.class1 .class2 {
}
.class1 .class2 .class3 {
}
.classFoo {
}

B) 经典识别

.class1 {
}
    .class1 .class2 {
    }
        .class1 .class2 .class3 {
        }
.classFoo {
}

C) 同一行

.class1 {}
.class1 .class2 {}
.class1 .class2 .class3 {}
.classFoo {}

D) 同一行识别

.class1 {}
    .class1 .class2 {}
        .class1 .class2 .class3 {}
.classFoo {}

Pros and cons of CSS formatting

I know four kinds of CSS formatting.
Which do you think are better?

A) classic

.class1 {
}
.class1 .class2 {
}
.class1 .class2 .class3 {
}
.classFoo {
}

B) classic idented

.class1 {
}
    .class1 .class2 {
    }
        .class1 .class2 .class3 {
        }
.classFoo {
}

C) same line

.class1 {}
.class1 .class2 {}
.class1 .class2 .class3 {}
.classFoo {}

D) same line idented

.class1 {}
    .class1 .class2 {}
        .class1 .class2 .class3 {}
.classFoo {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乜一 2024-09-22 16:23:28

老实说,我认为您使用的风格并不重要,只要您始终如一地使用它,并且与可能与您一起从事同一项目的任何其他人都同意并根据您的工作场所/办公室规定的任何具体文件要求。

话虽如此,我更喜欢 A 型,即大致按血统组织的群体:

.className {
    width: 50%;
    background-color: #fff;
    /* ...etc... */
}

.className .descendantOne {
    /* ...stuff... */
}

.className .descendantTwo {
    /* ...stuff... */
}

.anotherClassName {
    /* ...stuff... */
}

话虽如此,其他人也有其他有效的偏好。

显然,对于生产代码,提供精简代码会更有效,因此单行格式更接近我在开发完成后使用的格式。

I honestly don't think it makes a difference what style you use, so long as you use it consistently and it was agreed with anyone else who is likely to work with you on the same projects and according to any specific documentation requirements imposed by your work-place/office.

Having said that, my preference is for style A, with groups organised roughly by descent:

.className {
    width: 50%;
    background-color: #fff;
    /* ...etc... */
}

.className .descendantOne {
    /* ...stuff... */
}

.className .descendantTwo {
    /* ...stuff... */
}

.anotherClassName {
    /* ...stuff... */
}

Having said that, other people have other, valid, preferences.

Obviously, for production code it's more efficient to serve minified code, so the one-line format is closer to what I use once development is complete.

泅渡 2024-09-22 16:23:28

对于一个巨大的样式表,B,因为由于所有选项卡,导航更容易。 (使用 SASS 之类的东西可以强化这一点)。

对于非常小的一个,A.C

和 DI 不推荐,因为它更难读取/操作属性和值。我认为唯一的情况是,如果您急于修复某些问题并且不关心缩进,否则我看不到任何好处。

当然,对于制作,您可以选择最小化版本,因此我的意见反映了正在编辑的版本。

For a huge stylesheet, B since it's easier to navigate because of all the tabbing. ( Using something like SASS reinforces this ).

For a very minimal one, A.

C and D I wouldn't recommend because it's harder to read/manipulate the properties and values. I think the only case for this would be if you were rushing to fix something and didn't care about indenting, otherwise I don't see a benefit.

Of course for production you can opt with a minimized version, so my opinions reflect the one being edited.

血之狂魔 2024-09-22 16:23:28

如果是由 sass 或更少生成的 css,您通常只会看到缩进。如果你不使用 sass(如果可以的话你应该使用),这基本上是 a 与 c 的问题。当规则只有一个设置时,c 才有意义;当规则有更多设置时,a 才有意义。

you usually only see indentation if it is css generated by sass or less. If you aren't using sass (which you should if you can), it is basically a question of a vs c. c makes sense when you only have a single setting for a rule, a makes sense for when you have more then that.

会发光的星星闪亮亮i 2024-09-22 16:23:28

优点:

  • 非常容易快速导航和查找属性

  • 重新定位元素时更容易理解

缺点:

  • 文件大小较大(由于额外的制表符/空格),加载速度较慢

  • < p>其他人可能不熟悉您的格式(但我不会太担心)

Pros:

  • Very easy to navigate and find properties quickly

  • Easier to understand when repositioning elements

Cons:

  • Larger file size (due to extra tabs/spaces) which in turn loads slower

  • Other people may be unfamiliar with your formatting (but I wouldn't worry about that much)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文