CSS父母的第一类型,但没有进一步的后代

时间:2017-01-11 13:39:14

标签: css less

https://codepen.io/bazzle/pen/jyWdjM

我想选择第一个p但不是p嵌套的内部块引用或任何其他元素。

换句话说,所有孩子p中的第一个p但不是孙子p的<{p>}

<div class="article__body">
  <p>
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</p>
<blockquote>
  <p>
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
  </p>
</blockquote>
<p>
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
</p>
</div>

CSS(减去)

.article__body{
        p:first-of-type{
            background-color:green;
        }
}

2 个答案:

答案 0 :(得分:0)

也许你应该试试这个:

.article_body p:nth-of-type(1){
    background-color:green;
}

或者只是创建一个类 HTLM:<p class="yourclass"></p>

CSS:.yourclass{background-color:green;}

答案 1 :(得分:0)

我试试

.article__body p:nth-of-type(1){
            background-color:green;
        }