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,053
  • 2
  • 7
  • 12
dagda1
  • 23,659
  • 54
  • 216
  • 399

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>
LinkinTED
  • 17,122
  • 4
  • 31
  • 55