【问题标题】:What does it mean when a class comes right after an id: [duplicate]当一个类紧跟在一个 id 之后是什么意思:[重复]
【发布时间】:2018-04-26 06:17:00
【问题描述】:

当一个类紧跟在一个 id 之后(它们之间没有空格),这在 css 中是什么意思?比如这样:

#bgimage.header-image

谢谢

【问题讨论】:

  • 它表示一个元素同时具有该 id 和 class - 如果选择器之间没有空格,则意味着它必须同时具有两者。您可能会发现这很有用:smashingmagazine.com/2007/07/…
  • 表示语言的基本语法规则所说的意思。这是一个研究不足的问题 IMO。

标签: css


【解决方案1】:

表示同时具有id和class的元素。例如:

<div id="bgimage" class="header-image"></div>

【讨论】:

    【解决方案2】:

    选择器之间的区别请参见下面的示例。

    /* _class should be a child of _id */
    #_id ._class{
      background: #ccc;
      width: 100px;
      height: 100px;
    }
    
    /* It should have both id and class */
    #_id._class{
      background: #faa;
      width: 100px;
      height: 100px;
    }
    <div id="_id">
      <div class="_class">
          Box 1
      </div>
    </div>
    
    
    <div class="_class" id="_id">
      Box 2
    </div>

    【讨论】:

      【解决方案3】:

      表示此公式将用于相同的元素,例如

      <div id="bgimage" class="header-image"></div>
      

      如果您要在 #bgimage 和 .header-image 之间留出空间,那么它将用于:

      <div id="bgimage"><div class="header-image"></div></div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-23
        • 2022-01-08
        • 1970-01-01
        • 2019-04-12
        • 1970-01-01
        • 1970-01-01
        • 2017-11-21
        • 1970-01-01
        相关资源
        最近更新 更多