Google Page Speed - 这些消息是什么意思?

发布于 2024-08-12 09:40:02 字数 510 浏览 2 评论 0原文

我在几个页面上运行了 Google Page Speed Firefox 扩展,在“高效 CSS 选择器”下,它列出了 CSS 中效率低下的各种内容。

但有些消息似乎有点神秘——这些(粗体)是什么意思:

div#menu h3.soon 小
带有 2 个后代选择器的标签键,ID 与标签过度限定,Class 与标签过度限定

表.data tr:nth-child(2n) td
带有 2 个后代选择器的标签键和带有标签的过度限定类

table.data tr.disabled td
带有 2 个后代选择器的标签键和使用标签过度限定的类以及使用标签过度限定的类

我假设他们认为后代选择器很糟糕,但也有很多“过度限定”。我可能不会花太多精力来修复所有这些问题(有很多),但很高兴知道 Google 在这里的实际含义!

I ran the Google Page Speed Firefox extension on a few pages, and under "efficient CSS selectors" it listed various things that are inefficient in my CSS.

But some of the messages seem a bit cryptic - what do these (in bold) mean:

div#menu h3.soon small
Tag key with 2 descendant selectors and ID overly qualified with tag and Class overly qualified with tag

table.data tr:nth-child(2n) td
Tag key with 2 descendant selectors and Class overly qualified with tag

table.data tr.disabled td
Tag key with 2 descendant selectors and Class overly qualified with tag and Class overly qualified with tag

I'm assuming they think descendant selectors are bad but there are lots of "overly qualified" as well. I probably won't go to too much effort fixing all these up (there are many) but it would be nice to know what Google actually means here!

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

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

发布评论

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

评论(3

帅气称霸 2024-08-19 09:40:03

首先,我从未使用过 Page Speed,但如果您花点时间阅读一下,该消息并不那么神秘。

div#menu h3.soon 小

带有 2 个后代选择器的标签键和过度限定的 ID
标签和类过度限定
标签

带有 2 个后代选择器的标签键: 您有多少个小标签没有包含在另一个带有 class 的标签中?没有任何?在这种情况下,CSS 嵌套是完全没有必要的。

ID 与标签过度限定: #menu 不需要在前面加上 div。
您很可能在带有 id 菜单的标记中没有任何其他标签(您不应该,它是一个 ID!),因此在菜单前面添加 div 是多余的。

类过度限定标签: .soon 不需要在前面加上 h3。
除了 h3 标签之外,您的标记中很可能没有任何其他带有 class Soon 的标签,因此无需在 .soon 前面添加 h3 。

其他消息也类似。

-史蒂芬

First off, I have never used Page Speed, but the message is not that cryptic if you take a second to read it.

div#menu h3.soon small

Tag key with 2 descendant selectors and ID overly qualified with
tag and Class overly qualified with
tag

Tag key with 2 descendant selectors: How many small tags do you have that are not contained in another tag with class soon? None? The CSS nesting would be totally unnecessary in this case.

ID overly qualified with tag: #menu does not need to be prepended with div.
You most likely don't have any other tags in your markup with id menu (you shouldn't, its an ID!), so prepending menu with div in redundant.

Class overly qualified with tag: .soon does not need to be prepended with h3.
You most likely don't have any other tag in your markup with class soon other than h3 tags, so prepending .soon with h3 is unnecessary.

The other messages follow similarly.

-Stephen

墨小墨 2024-08-19 09:40:03

斯蒂芬说得好。

他们标记你的选择器的原因是 CSS 规则是从右到左匹配的。

不需要在 ID 前面添加元素(如 div#content 中),因为浏览器在到达“div”时已经匹配了选择器。但浏览器仍然被迫对其进行评估。

后代选择器的成本很高,因为浏览器必须根据所有可能的祖先检查最右边的简单选择器中引用的 dom 元素的所有实例。多个后代会加剧性能损失。

也就是说,通过优化选择器(在大多数情况下)获得的性能增益可以忽略不计。

Stephen said it well.

The reason they're flagging your selectors is that CSS rules are matched right to left.

Prepending an ID with an element (as in div#content) is unnecessary because the browser has already matched the selector by the time it reaches "div". But the browser is still forced to evaluate it.

Descendant selectors are expensive because the browser has to check all instances of the dom element referenced in the right-most simple selector against all possible ancestors. Multiple descendants compound the performance penalty.

That said, the performance gain achieved from optimizing selectors (in most cases) is negligible.

耳钉梦 2024-08-19 09:40:03

这意味着没有理由使用该标签,因为您给出了一个类,所以您已经限制了它,并且它必须进行额外的搜索。

例如:

div#menu h3.soon .small

这里没有理由以 div 开头,因为您很快只会在带有 id menu 的 html 元素下面的 h3 标签中查看 class Small 。

他们正在建议这样的事情

#menu .soon .small {...}

,甚至

#menu {...}

.soon {...}

.small {...}

It is saying there is no reason to use the tag since you give a class so you are already limiting it and it have to do extra searching.

For example:

div#menu h3.soon .small

Here there is no reason to start with div since you will only look at class small in class soon in an h3 tag below html element with id menu.

They are suggesting something like this

#menu .soon .small {...}

or even

#menu {...}

.soon {...}

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