jQuery API

:first-child Selector

first-child selector

version added: 1.1.4jQuery(':first-child')

Description: Selects all elements that are the first child of their parent.

While :first matches only a single element, the :first-child selector can match more than one: one for each parent. This is equivalent to :nth-child(1).

Example:

Finds the first span in each matched div to underline and add a hover state.

<!DOCTYPE html>
<html>
<head>
  <style>
  span { color:#008; }
  span.sogreen { color:green; font-weight: bolder; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>
    <span>John,</span>
    <span>Karl,</span>
    <span>Brandon</span>

  </div>
  <div>
    <span>Glen,</span>
    <span>Tane,</span>
    <span>Ralph</span>

  </div>
<script>
    $("div span:first-child")
        .css("text-decoration", "underline")
        .hover(function () {
              $(this).addClass("sogreen");
            }, function () {
              $(this).removeClass("sogreen");
            });

</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :first-child Selector or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to :first-child Selector? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • JHJHGJHGJ

    JHGJGHJGHJGHJ

  • http://karelkapteyn.com/ Karel Kapteyn

    I was actually looking for the css code to the second child and glad I found this :nth-child(2) here :D

    • Alex

      Yes, :nth-child() is handy, but what css code are you talking about?

  • http://karelkapteyn.com/ Karel Kapteyn

    I was actually looking for the css code to the second child and glad I found this :nth-child(2) here :D