html – 处理CSS类和id之间的速度差异

前端之家收集整理的这篇文章主要介绍了html – 处理CSS类和id之间的速度差异前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于这个问题,我只是比较浏览器在2个元素上渲染CSS的速度,这两个元素不同,只有一个有一个类,一个有id.

(这与JS识别,锚点使用等无关)

<div class="myclass">classed element</div>
<div id="myid">ided element</div>

有没有人有这个数字?我已经读过CSS ID’更快’,但是多少钱?我会冒险猜测它可以忽略不计,但知道它会很有趣.

解决方法

http://oli.jp/2011/ids/

ID在某些情况下更快,但不是全部

It’s a common belief that ID selectors are the fastest,but this comes with a big caveat: IDs are fastest CSS selector only if they’re the key selector. What’s that? Well,while you probably read selectors from left to right,browsers read them from right to left.

这里还有一个针对您的号码请求的性能测试:http://oli.jp/2011/ids/#table1

结论

ID的正确使用速度更快,但与类相比差别很小 – 不值得考虑.

It seems to me that there are no convincing reasons to use IDs in selectors for CSS styling¹,as CSS classes can do everything IDs can. Hopefully you’ll agree there are some good reasons not to. Think about it the next time you start a personal project or redesign your own site,and try adding a class (or ARIA landmark roles) for styling instead. Save IDs for fragment identifiers or JavaScript hooks

猜你在找的HTML相关文章