InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    Css :first-child selector – first child element

    By admin on Jan 25, 2016

    Css :first-child pseudo class apply to an element if it is first sibling in all direct child elements of its parent. Here are some examples.

    css-first-child

    :first-child positive example

    <style type="text/css" media="screen">
    .foo > p:first-child {
      background-color:lightgreen;
    }
    </style>
    <div class="foo">
      <p>p1</p>
      <p>p2</p>
      <p>p3</p>
    </div>
    refresh done
    try it online

    Few points to note:

    1. Here the :first-child pseudo class style applies to all p direct child elements of div of class foo.
    2. To apply style to all direct child and descendant p elements we could have used .foo p:first-child. See more info at CSS direct child vs any descendant selector.

    :first-child negative example

    Here no p element is first child. So background color won’t be set.

    <style type="text/css" media="screen">
    .foo > p:first-child {
      background-color:lightgreen;
    }
    </style>
    <div class="foo">
      <div>div1</div>
      <p>p1</p>
      <p>p2</p>
      <p>p3</p>
    </div>
    refresh done
    try it online

    :first-child – include nested child elements example

    This example contains two p elements which fulfil first-child requirements.

    <style type="text/css" media="screen">
    .foo p:first-child {
      background-color:lightgreen;
    }
    </style>
    <div class="foo">
      <p>p1</p>
      <p>p2</p>
      <div><p>in_div_p1</p><p>in_div_p2</p></div>
      <p>p3</p>
    </div>
    refresh done
    try it online

    :first-child – exclude nested child elements example

    This example contains two p elements which fulfil first-child requirements. But since we are using .foo > p, only direct p child elements are eligible for this style.

    <style type="text/css" media="screen">
    .foo > p:first-child {
      background-color:lightgreen;
    }
    </style>
    <div class="foo">
      <p>p1</p>
      <p>p2</p>
      <p>p3</p>
      <div><p>in_div_p1</p><p>in_div_p2</p></div>
    </div>
    refresh done
    try it online

    Specification

    • W3C CSS 3 :first-child pseudo class specification

    Suggested posts:

    1. PHP regex – word boundary examples
    2. Css :last-child selector – last child element
    3. CSS border radius and round corners examples
    4. CSS :nth-of-type
    5. awk sum examples
    6. CSS :not – negation pseudo-class
    7. Vim – how to change current character case to uppercase or lowercase
    8. CSS :root
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS Pseudo Classes, CSS selectors, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress