【问题标题】:CSS target specific classCSS 目标特定类
【发布时间】:2017-10-10 16:25:24
【问题描述】:

我试图定位一个特定的类,它是 div 中的第一个类名。我所做的尝试如下所示:

.container-fluid:first-child div.row {
    padding-top: 200px;
}

CSS 的 HTML。

<div class="container-fluid">
    <div class="row justify-content-md-center"> <<TRYING TO TARGET
        <div class="col-3 text-center">
            <div class="row">
                <img src="/assets/images/fssLogo.png"/>
            </div>
        </div>
    </div>
</div>

如您所见,我只想定位容器流体标签内的第一行,但填充也被应用于 col-3 类内的行。

有人能给我指出正确的方向吗?

【问题讨论】:

    标签: html css


    【解决方案1】:

    应该是

    .container-fluid >.row:first-child {
        padding-top: 200px;
    }
    

    .container-fluid > .row:first-child {
      background-color: red;
      padding-top: 200px;
    }
    
    .innerRow {
      background-color: pink;
    }
    <div class="container-fluid">
      <div class="row justify-content-md-center">
        <div class="col-3 text-center">
          <div class="row innerRow">
            <img src="http://via.placeholder.com/350x150" />
          </div>
        </div>
      </div>
    </div>

    或者

    .container-fluid >.row {
        padding-top: 200px;
    }
    

    .container-fluid > .row {
      background-color: red;
      padding-top: 200px;
    }
    
    .innerRow {
      background-color: pink;
    }
    <div class="container-fluid">
      <div class="row justify-content-md-center">
        <div class="col-3 text-center">
          <div class="row innerRow">
            <img src="http://via.placeholder.com/350x150" />
          </div>
        </div>
      </div>
    </div>

    但是第二个 sn-p 更可取,因为您的目标是 .row,它是 container-fluid 的直接子代,除非您还有另一个 row,它也是 container-fluid 的直接子代,您可以使用第一个 sn-p 仅针对第一个 row 孩子。

    &gt; 用于定位父级的直接子级,无论层次结构中是否存在同名的类

    .parent > .someClass {
      color: blue;
    }
    <div class="parent">
      <p class="someClass">TARGETED</p>
      <div>
        <p class="someClass">NOT TARGETED</p>
      </div>
    </div>

    删除&gt;,两个文本都会变成蓝色

    【讨论】:

      猜你喜欢
      • 2020-07-05
      • 2021-11-02
      • 2020-10-03
      • 2011-11-18
      • 2017-05-09
      • 1970-01-01
      • 2019-05-17
      • 2016-05-02
      • 1970-01-01
      相关资源
      最近更新 更多