在CSS中使用非常具体的选择器有优势吗? [英] Is there an advantage to using very specific selectors in CSS?

查看:184
本文介绍了在CSS中使用非常具体的选择器有优势吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解在jQuery中,使用选择器时更具体,以便jQuery不必遍历整个DOM来找到您要查找的内容是有利的。例如, $('span.description')比仅仅 $('。description')知道描述类只应用于< span> 元素。

I understand that in jQuery, it's advantageous to be more specific when using selectors so that jQuery doesn't have to traverse the entire DOM to find what you're looking for. For example, $('span.description') is better than just $('.description') if I know that the description class is only ever applied to <span> elements.

这也是CSS的情况吗?对我来说,使用 span.description {} 而不是 .description {} 有什么特别的优势吗?

Is this the case with CSS, too? Is there any specific advantage for me to use span.description { } instead of .description { }? I'm thinking in terms of speed, optimization, etc. Am I saving the browser any work by telling it exactly where to look?

推荐答案

我在保存浏览器的任何工作,

这在CSS中是正确的 -

This is true in CSS -

一个好的规则是从最近的ID下降。
ID被索引,所以找到它们是非常快。没有理由在您的选择器中使用多个。

A good rule is to descend from the nearest ID. IDs are indexed so locating them is extremely fast. There is no reason to use more than one in your selector.

Google代码 - 优化浏览器呈现

Google Code- Optimize browser rendering

这回答了很多问题有关于这个问题包括这一个
我希望你觉得它有用。

This answered a lot of questions I had on the subject including this one- I hope you find it useful.

这篇关于在CSS中使用非常具体的选择器有优势吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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