哪种 CSS 组织方法最快?

发布于 2024-08-11 19:32:05 字数 767 浏览 2 评论 0原文

如果我有以下 HTML:

<p id="p1">Paragraph 1</p>
<p id="p2">Paragraph 2</p>
<p id="p3">Paragraph 3</p>
<p id="p4">Paragraph 4</p>

我应该如何组织 CSS 以实现最快的页面加载时间?

我可以通过 HTML 元素来组织它,如下所示:

#p1 { font-size:12px; color:red; }
#p2 { font-size:12px; color:blue; }
#p3 { font-size:11px; color:red; }
#p4 { font-size=11px; color:blue; }

或通过 CSS 样式来组织它,如下所示:

#p1, #p2 { font-size: 12px; }
#p3, #p4 { font-size: 11px; }
#p1, #p3 { color:red; }
#p2, #p4 { color:blue; }

如果是的话,哪一个的读取和处理速度会更快?

编辑:我应该提到,我现在正在使用 GreaseMonkey,这意味着两件事可能很重要:

1)我无法编辑任何 HTML,只能编辑 CSS

2)我的所有 CSS 都被读取页面正常加载完成后。

If I have the following HTML:

<p id="p1">Paragraph 1</p>
<p id="p2">Paragraph 2</p>
<p id="p3">Paragraph 3</p>
<p id="p4">Paragraph 4</p>

how should I organize the CSS to achieve the fastest page load time?

I could organize it by HTML element, like this:

#p1 { font-size:12px; color:red; }
#p2 { font-size:12px; color:blue; }
#p3 { font-size:11px; color:red; }
#p4 { font-size=11px; color:blue; }

or by CSS style, like this:

#p1, #p2 { font-size: 12px; }
#p3, #p4 { font-size: 11px; }
#p1, #p3 { color:red; }
#p2, #p4 { color:blue; }

Which, if either, would be read and processed faster?

EDIT: I should have mentioned, I'm working with GreaseMonkey right now, which means two potentially important things:

1) I can't edit any HTML, only CSS

2) All of my CSS is read after the page finishes loading normally.

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

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

发布评论

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

评论(4

追风人 2024-08-18 19:32:05

嗯,第二个肯定会读取更快,因为它更小。

至于“处理”,是通过什么浏览器进行的?除非有问题的样式表处理数以万计的 ID,否则我怀疑您不会看到两者之间有任何显着差异。

Well, 2nd would certainly be read faster since it's smaller.

As far as "processed" goes, by what browser? Unless the style sheet in question deals with tens of thousands of ids I doubt you'd see any significant difference between the two.

澉约 2024-08-18 19:32:05

我不明白 CSS 处理如何成为页面加载时间的瓶颈。如果您希望页面加载速度更快,请减小 HTML/CSS 的大小,并将 CSS 粘贴到 HTML 的 head 标记中的 style 标记中。这将避免额外的 GET 请求,我猜测这是加载时间缓慢的实际根源。

I don't see how CSS processing would be a bottleneck in page load time. If you want the page to load faster, reduce the size of the HTML/CSS and paste the CSS into a style tag in the head tag of the HTML. This will avoid an extra GET request, which I'm guessing is the actual source of the slow load time.

忆伤 2024-08-18 19:32:05

为什么不:

.red {color: red}
.blue {color: blue}
.small {font-size: 11}
.big {font-size: 12}

然后:

<p id="p1" class="big red">Paragraph 1</p>
<p id="p2" class="big blue">Paragraph 2</p>
<p id="p3" class="small red">Paragraph 3</p>
<p id="p4" class="small blue">Paragraph 4</p>

Why not:

.red {color: red}
.blue {color: blue}
.small {font-size: 11}
.big {font-size: 12}

And then:

<p id="p1" class="big red">Paragraph 1</p>
<p id="p2" class="big blue">Paragraph 2</p>
<p id="p3" class="small red">Paragraph 3</p>
<p id="p4" class="small blue">Paragraph 4</p>
花辞树 2024-08-18 19:32:05

您可能应该在不同的浏览器中对此进行分析,因为它们可能会给出不同的结果。

You should probably profile this in different browsers as they are likely to give you different results.

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