使用 GZIP,我可以写“Sloppy”吗? CSS(和其他类型的代码)代码?

发布于 2024-08-06 17:53:15 字数 275 浏览 3 评论 0原文

多年来,编写简洁 CSS 代码的鼓点一直萦绕在我的脑海中。例如:

这样做: .foo,.bar{ 颜色:#f00; 不是

这个: .foo{ 颜色:#f00; } 。酒吧{ 颜色:#f00; 使用

GZIP 压缩,我如何编写上面的 CSS 不是无关紧要的吗?由于 GZIP 将创建一个包含 color:#f00 的动态库作为单个实例,并使用指向其多种用途的指针。我想知道,因为重复样式定义比搜索相同的声明更方便。

The drumbeat of writing terse CSS code has been beaten into my head for years. For example:

Do this:
.foo,.bar{
color:#f00;
}

Not this:
.foo{
color:#f00;
}
.bar{
color:#f00;
}

With GZIP compression isn't it irrelevant how I write the above CSS? Since GZIP will create a dynamic library with color:#f00 as a single instance and use pointers to its multiple uses. I want to know because it is more convenient to repeat style definitions versus searching for an identical declaration.

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

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

发布评论

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

评论(4

浅忆 2024-08-13 17:53:15

您可以使用 css 压缩器 发布前不再担心可读性和文件大小。

GZIP 肯定会对“草率”CSS 文件产生更大的前后大小影响,但在压缩之前拥有尽可能小的文件,总是大多数时候是一件好事tm,因为它会稍微小一些。


请记住:源代码是供人类阅读的,只是顺便供机器运行。

You could use a css minifier before publishing and stop worrying about readability and file size.

GZIP will definitely have a bigger before/after size impact with 'sloppy' CSS files, but having the smallest file possible before compression, is alwaysmost of the time a good thingtm, as it will be marginally smaller.


Remember: Source code is for humans to read, and only incidentally for machines to run.

好菇凉咱不稀罕他 2024-08-13 17:53:15

这取决于您想要实现的目标:

  • 如果 .foo 和 .bar 需要具有相同的颜色,您最好使用第一种方法,因为它更容易维护。
  • 如果您的第一个语句是您手动优化文件大小的结果,并且 .foo 和 .bar 具有相同的属性只是巧合,那么我肯定会拆分它并使用第二个解决方案。

最后 gzip 和/或 css 压缩器将处理文件大小,所以我不会担心这个。

相反,担心可维护性和可读性。

It depends on what you try to achieve:

  • If .foo and .bar need to have the same colour, you´re better of using the first method because it is easier to maintain.
  • If your first statement is a result of you manually optimizing your file-size, and it´s just a coincidence that .foo and .bar have the same properties, I would definitely split it and use the second solution.

In the end gzip and / or a css compressor will handle the file size so I wouldn´t worry about that.

Worry about maintainability and readability instead.

放手` 2024-08-13 17:53:15

如果这有意义,那就去做吧。否则,我会以逻辑方式编写 CSS,以便可以正确维护它,然后依赖 gzip + CSS 压缩器(例如 YUI 压缩机)来为您处理脏活累活。

If that makes sense, go for it. Otherwise, I'd write the CSS in a logical fashion so it can be maintained properly, and then rely on gzip + a CSS compressor (like YUI Compressor) to handle the dirty work for you.

提笔书几行 2024-08-13 17:53:15

我认为您仍然想继续使用第一种方法,因为它更具可读性。每件事都有一个单独的部分会让你的CSS变得臃肿并且人们很难理解它。

I think you would still want to continue using the first method because it's more readable. Having a separate section for every thing will make you css bloated and difficult for people to get through it.

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