在css中(div#container)和(#container)之间的区别是什么 [英] what is the difference between (div#container) and (#container) in css

查看:2334
本文介绍了在css中(div#container)和(#container)之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,id应该在html标签中是唯一的,因此可以直接使用:

  #container但是我注意到,一些开发人员会在其前面添加标签名称,如下所示:

$


b
$ b

  div#container 

现在有什么区别吗?它与性能有关吗?还是它的最佳实践约定?

解决方案

确定我提出这个问题的错误假设如下:


  1. 只有一个HTM页面,因此:


  2. 一个ID(容器)


  3. 此页面将有一个内部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:

    1. There will be only one HTM page, hence:

    2. There should be only one ID (container)

    3. 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:

    1. You have a website with multiple pages

    2. Although id(container) is unique in every page, however it might be assigned to different elements, e.g. div id="conainer" in one page. And p id="container" in another page

    3. most 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屋!

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