CSS星形选择器是否有害(为什么)? [英] Is the CSS star selector considered harmful (and why)?

查看:118
本文介绍了CSS星形选择器是否有害(为什么)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CSS中的星形选择器会影响页面渲染性能吗?

Does the star selector in CSS affect page rendering performance?

有使用它的警告吗?

* {
  margin:0;
  padding:0;
}

推荐答案

在性能方面,史蒂夫·索德斯(Steve Souders)是 the 的人:

When it comes to performance, Steve Souders is the man:

  • Performance Impact of CSS Selectors
  • Simplifying CSS Selectors

其中一份报告的无耻报价:

Shameless quote from one of the reports:

优化CSS选择器的关键是 专注于最右边的选择器, 也称为键选择器 (巧合?).还有更多 昂贵的选择器:A.class0007 * {}. 尽管此选择器可能看起来 更简单,对于 浏览器进行匹配.因为浏览器 从右向左移动,开始于 检查所有匹配的元素 键选择器"*".这意味着 浏览器必须尝试与此匹配 选择器针对所有元素 页面.

The key to optimizing CSS selectors is to focus on the rightmost selector, also called the key selector (coincidence?). Here’s a much more expensive selector: A.class0007 * {}. Although this selector might look simpler, it’s more expensive for the browser to match. Because the browser moves right to left, it starts by checking all the elements that match the key selector, "*". This means the browser must try to match this selector against all elements in the page.

[大胆强调我的观点]

[bold emphasis mine]

这篇关于CSS星形选择器是否有害(为什么)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆