是否有任何理由在CSS中包含类或ID选择器的标记名称? [英] Is there any reason for including the tag name with a class or ID selector in CSS?

查看:151
本文介绍了是否有任何理由在CSS中包含类或ID选择器的标记名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 div.class .class 是否有CSS选择器的区别只有一个标签(DIV)具有该属性。同样的ID: div#ID #ID

I'm wondering if there is any difference between div.class and .class as CSS selectors if there is only one tag (the DIV) with that attribute. Same thing for IDs: div#ID and #ID.

任何想法什么,如果有什么,是区别?对我来说,我在这些情况下使用类或ID;

Any idea what, if anything, is the difference? For me, I use the class or ID in these situations; but only because it's quicker to type.

推荐答案


  1. 它们更具体,如果他们冲突,更短的版本。 div.foo 的特异性为11,而 .foo 的特异性为10.

  2. 因为它们更具体,你知道该类应用于哪个nodename,而不是作为所有节点名称的通用规则,如果你有一个巨大的应用程序,吨的元素都具有相同的类名称,它可以减少您在源/文本编辑器中查找元素的时间。

  1. They are more specific, so long ones will override the shorter version if they conflict. div.foo will have a specificity of 11 while .foo has a specificity of 10.
  2. Since they are more specific, you know exactly which nodename the class applies to instead of being a universal rule for all node names, this can help if you have a huge application with tons of elements that all have the same class names, it can lessen the time for you to find the element in the source/text editor.

这篇关于是否有任何理由在CSS中包含类或ID选择器的标记名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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