使用CSS选择第一个UL

时间:2014-11-25 15:12:04

标签: html css

是否可以在DIV中使用CSS选择第一个UL,这是我使用的HTML

<div class="block">
    <h2>External</h2>
    <ul>
        <li><span class="indicator success"></span>1</li>
        <li><span class="indicator success"></span>2</li>
    </ul>
    <ul>
        <li><span class="indicator success"></span>1</li>
        <li><span class="indicator success"></span>2</li>
    </ul>
</div>

5 个答案:

答案 0 :(得分:8)

使用first-of-type

ul:first-of-type {
    /* css rules */
}

<强> DEMO

ul:first-of-type {
  color: red
}
<div class="block">
  <h2>External</h2>
  <ul>
    <li><span class="indicator success"></span>1</li>
    <li><span class="indicator success"></span>2</li>
  </ul>
  <ul>
    <li><span class="indicator success"></span>1</li>
    <li><span class="indicator success"></span>2</li>
  </ul>
</div>

答案 1 :(得分:3)

两种方式来实现它:

  1. ul:first-of-type
  2. ul:nth-child(2)
  3. `

答案 2 :(得分:2)

使用:第一个孩子

ul:first-child {
    /* css rules */
}

答案 3 :(得分:1)

您可以这样做:

ul:nth-of-type(1) {

/* CSS Rules */

}

答案 4 :(得分:1)

之前的答案是正确的,但你应该在.block父选择器之前选择只在想要的div中选择:

&#13;
&#13;
.block ul:first-of-type {
  color: red
}
&#13;
<div class="block">
  <h2>External</h2>
  <ul>
    <li><span class="indicator success"></span>1</li>
    <li><span class="indicator success"></span>2</li>
  </ul>
  <ul>
    <li><span class="indicator success"></span>1</li>
    <li><span class="indicator success"></span>2</li>
  </ul>
</div>
&#13;
&#13;
&#13;