0

In the following snippet, I only want the first div after the legend which has the text one and nothing else but is selecting three also

.container fieldset div:first-of-type {
  border: 1px solid red;
}
<html>

  <body>
    <div class="container">
      <fieldset>
      <legen>blah</legen>
        <div>one</div>
        <div>two
          <div>three</div>
        </div>
        <div>three</div>
      </fieldset>
    </div>
  </body>

</html>
Danny
  • 1,083
  • 2
  • 8
  • 12
dagda1
  • 26,856
  • 59
  • 237
  • 450

1 Answers1

4

You mean you only want to select the div with "one" and not the div with "three" who is inside div "two", right?

Then use the direct descendant selector >

.container fieldset > div:first-of-type {
  border: 1px solid red;
}
<html>

  <body>
    <div class="container">
      <fieldset>
      <legen>blah</legen>
        <div>one</div>
        <div>two
          <div>three</div>
        </div>
        <div>three</div>
      </fieldset>
    </div>
  </body>

</html>

.container fieldset div:first-of-type {
  border: 1px solid red;
}
<html>

  <body>
    <div class="container">
      <fieldset>
      <legen>blah</legen>
        <div>one</div>
        <div>two
          <div>three</div>
        </div>
        <div>three</div>
      </fieldset>
    </div>
  </body>

</html>
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59