加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

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

发布时间:2020-12-14 18:28:28 所属栏目:资源 来源:网络整理
导读:对于这个问题,我只是比较浏览器在2个元素上渲染CSS的速度,这两个元素不同,只有一个有一个类,一个有id. (这与JS识别,锚点使用等无关) div class="myclass"classed element/divdiv id="myid"ided element/div 有没有人有这个数字?我已经读过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 styling1,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读