在css中(div#container)和(#container)之间的区别是什么 [英] what is the difference between (div#container) and (#container) in css
问题描述
众所周知,id应该在html标签中是唯一的,因此可以直接使用:
#container但是我注意到,一些开发人员会在其前面添加标签名称,如下所示: $
b
$ b
div#container
现在有什么区别吗?它与性能有关吗?还是它的最佳实践约定?
确定我提出这个问题的错误假设如下:
-
只有一个HTM页面,因此:
-
一个ID(容器)
-
此页面将有一个内部CSS块
根据上述错误的假设,它不必包含标签名称。
但是,正确的假设应该是:
虽然ID (容器)在每个页面中是唯一的,但是它可以被分配给不同的元素,例如 div id =conainer
。
很可能有一个外部css文件,或者在其他页面中有 p id =container
根据这些假设,更具体地标记标签名称的好处将阻止异常行为
这几乎是一样的 answer laaposto
It's well known that the id should be unique in html tags, so it can be referenced directly using:
#container
However I noticed that some developer will prefix it with the tag name like this:
div#container
Now is there a difference? Is it related to performance? Or is it best practice conventions? Or for easy code readability?
Ok I asked this question with a wrong assumptions as follow:
There will be only one HTM page, hence:
There should be only one ID (container)
There will be an internal CSS block for such page
Based on the above wrong assumptions, it's needless to include the tag name.
However, the correct assumptions should be:
You have a website with multiple pages
Although id(container) is unique in every page, however it might be assigned to different elements, e.g.
div id="conainer"
in one page. Andp id="container"
in another pagemost probably there is an external css file or is file that serves all these pages.
Its based on those assumptions the benefit of being more specific prefixing the tag name will prevent abnormal behavior
This is almost the same answer of laaposto
这篇关于在css中(div#container)和(#container)之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!