The Wayback Machine - https://web.archive.org/web/20240917134149/https://www.geeksforgeeks.org/css-first-child-selector/
Open In App

CSS First Child Selector

Last Updated : 12 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

CSS :first-child selector is used to select the elements that are the first child of the parent element.

Syntax: 

:first-child {
      /* CSS property */
}

Example 1: This example shows the use of the CSS first child selector.

html
<!DOCTYPE html>
<html>

<head>
    <title>first child selector</title>
    <style>
        h1 {
            color: green;
        }

        p:first-child {
            background-color: green;
            color: white;
            font-weight: bold;
        }

        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <p>GeeksforGeeks</p>
    <h1>GeeksforGeeks</h1>
    <h2>:first-child Selector</h2>
    <p>GeeksforGeeks</p>
    <div>
        <p>GFG</p>
        <p>Geeks</p>
    </div>
</body>

</html>

Output: 


Example 2: This example shows the use of the CSS first child selector.

html
<!DOCTYPE html>
<html>

<head>
    <title>first child selector</title>
    <style>
        h1 {
            color: green;
        }

        h1,
        h2 {
            text-align: center;
        }

        li:first-child {
            background: green;
        }

        body {
            width: 70%;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>:first-child Selector</h2>
    <ul>
        <li>Data Structure</li>
        <li>Computer Network</li>
        <li>Operating System</li>
    </ul>
    <ol>
        <li>Java</li>
        <li>Ruby</li>
        <li>Pascal</li>
    </ol>
</body>

</html>

Output: 

Supported Browsers: 

  • Apple Safari 3.1 and above
  • Google Chrome 4.0 and above
  • Edge 12.0 and above
  • Firefox 3.0 and above
  • Opera 9.5 and above

CSS :first-child Selector – FAQs

What does the :first-child selector do in CSS?

The :first-child selector targets an element that is the first child of its parent element.

How do I style the first paragraph in a container?

Use the :first-child selector, like div p:first-child { font-weight: bold; }, to style the first paragraph within a div container.

Does :first-child work with all element types?

Yes, the :first-child selector can target any element type that is the first child of its parent, regardless of its tag.

Can :first-child be combined with other pseudo-classes?

Yes, :first-child can be combined with other pseudo-classes, such as :hover or :active, to apply more specific styling.

Is the :first-child selector supported by all browsers?

Yes, the :first-child selector is supported by all modern browsers, including older versions.



Previous Article
Next Article

Similar Reads

Difference between :first-child and :first-of-type selector in CSS
The :first-child and :first-of-type selectors in CSS are used to target specific child elements within a parent. The :first-child selector targets an element if it is the first child of its parent, regardless of type. On the other hand, the :first-of-type selector targets the first occurrence of a specified element type within its parent. Understan
2 min read
How to use a not:first-child selector in CSS?
This selector is used to select every element which is not the first-child of its parent element. It is represented as an argument in the form of :not(first-child) element. Syntax: :not( element ) { // CSS property } Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;not first child selector&lt;/title&gt; &lt;style&gt; h1 { color
1 min read
jQuery :first-child Selector
jQuery :first-child Selector is used to select every element that is the first child of its parent element. Syntax: $("selector:first-child")It selects and returns the first child element of its parent element. Example 1: In this example, we select the first paragraph element and set its background color to green. C/C++ Code &lt;!DOCTYPE html&gt;
1 min read
CSS :last-child Selector
The CSS :last-child selector targets the last child element within its parent. It applies styles exclusively to the last child element based on its position in the parent container, regardless of the element type or class. Syntax: :last-child { //property}Example: In this example we applies limegreen background, white color, italic font-style, and
2 min read
CSS :nth-child() Selector
The :nth-child() CSS pseudo-class selector is useful for targeting elements based on their position among siblings. This selector can match elements by specific numeric positions, odd/even positions, or complex functional notations like An+B. Understanding and utilizing :nth-child() can enhance web design by allowing for precise and dynamic styling
3 min read
CSS :nth-last-child() Selector
The nth-last-child() selector in CSS is used for selecting elements based on their position among sibling elements, but starting the count from the end rather than the beginning. This selector is particularly useful for styling elements dynamically based on their order in reverse, allowing for flexible and responsive design solutions. Understanding
3 min read
CSS :only-child Selector
The:only-child selector in CSS is used to match every element that is the only child of its parent. It represents an element without any siblings.  Syntax: :only-child { // CSS property} Example 1:  [GFGTABS] html &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;:only-child selector&lt;/title&gt; &lt;style&gt; h1 { color: green; text-al
2 min read
CSS :not(:last-child):after Selector
The CSS :not(:last-child): after selector is used to style elements, specifically adding content after each child element except the last one. This selector is useful when we want to target elements that cannot be directly selected. Syntax:element :not(:last-child):afterExample 1: This example shows the uses of: not(:last-child): after selector wit
2 min read
jQuery :nth-last-child() Selector
The jQuery :nth-last-child() selector is used to select all elements that are the nth last child of their parent. The counting of elements starts from the last element. Syntax: :nth-last-child( n | even | odd | formula )Parameters: The :nth-last-child() selector contain parameters which are listed below: n: It holds the child index number of the se
2 min read
jQuery :last-child Selector
It is a jQuery Selector used to select every element that is the last child of its parent. Syntax: $(":last-child")Example 1: Here is the basic example of a last-child selector. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;jQuery :last-child Selector&lt;/title&gt; &lt;script src= &quot;https://ajax.googleapis.com/ajax/lib
1 min read
jQuery :only-child Selector
It is a jQuery Selector used to select every element that is the only child of it's parent i.e only a single child is selected. Syntax: $(":only-child")Example 1: In this example, we are using only-child selector. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;jQuery :only-child Selector&lt;/title&gt; &lt;script src= &quot;
1 min read
jQuery parent &gt; child Selector
It is a jQuery Selector used to select all elements that are the direct child of its parent element. Syntax: ("parent &gt; child")Parameter Values: parent: Using this, the parent element will be selected.child: Using this, the direct child element of the specified parent element will be selected.Example 1: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html
1 min read
jQuery :nth-child() Selector
The jQuery :nth-child() Selector Selects all elements that are the nth-child of their parent elements. Syntax: $("Selector:nth-child( index | even |odd |equation )")Index: Index provided. Index starts fromeven: even number of child elements get selected.odd: odd number of child elements will get selected.equation: select child elements by solving a
1 min read
Last-child and last-of-type selector in SASS
SASS is also called Syntactically Awesome Stylesheet. It is a programming language that is interpreted into CSS. The Last Child selector is a selector that allows the user to target the last element inside the containing element. This selector is also known as a Structural Pseudo class which means it is used for styling content on the basis of pare
2 min read
How to create a combined child selector in SASS ?
Introduction: Lets first talk about the various types of Combinators available in CSS. Combinator: A combinator is something that explains the relationship between the selectors. A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator. There are four different combinators in CSS:Descendant
2 min read
How to apply styles only to the first child of an element using CSS ?
The :first-child selector is a pseudo-class in CSS that enables you to target the initial child element within a parent container. This selector handpicks elements that are the first immediate child of their parent. It serves as a powerful selector that makes it easier to apply unique styles or modifications to that specific element. &lt;div class=
1 min read
What is first child in CSS ?
In CSS, the :first-child selector is used to target the first child element within its parent. It applies styles to the first element only if it is the first child of its parent, allowing for specific formatting and design changes. Syntax: :first-child { //property}Example: Here is the demonstration of the first child in CSS. [GFGTABS] HTML &lt;!DO
1 min read
CSS :first-of-type Selector
The :first-of-type Selector is used to targeting the first child of every element of it's parent. if we want to style the first child of an element without giving a class, we can use it. Syntax: :first-of-type { //property}Example: [GFGTABS] HTML &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;style&gt; p:first-of-type { background: limegreen;
2 min read
CSS ::first-letter Selector
The ::first-letter selector CSS Pseudo-element is used to apply the style to the first letter of the first line of a block-level element, the condition is it should not be preceded by other content ( such as images or inline tables). Syntax: ::first-letter { // CSS Property}Accepted Properties: background propertiesborder propertiesclearcolor prope
2 min read
CSS ::first-line Selector
The ::first-line selector in CSS is used to apply style to the first line of a block-level element. The length of the first line depends on many factors, including the width of the element, the width of the document, font-size of the text, etc. Syntax: ::first-line { //CSS Property}Example: [GFGTABS] html &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head
2 min read
How to set DOM Element as First Child ?
In this article, we will explore different approaches to setting a DOM element as the first child of its parent. When working with HTML documents, it is common to manipulate the DOM (Document Object Model) to rearrange elements dynamically, Setting a DOM element as the first child means inserting it at the beginning of its parent's list of children
2 min read
jQuery :first-of-type Selector
The jQuery :first-of-type Selector is used to select all elements that are the first child, of a particular type, of their parent. Syntax: $(":first-of-type")The below examples illustrate the:first-of-type selector in jQuery: Example 1: This example changes the background color to green and the text color to white, of the first heading of their par
2 min read
jQuery :first Selector
The jQuery :first Selector is used to select the first element of the specified type. Syntax: $(":first")Example 1: In this example, we will select the first &lt;p&gt; element by using jQuery :first Selector. C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src= &quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.mi
1 min read
How to select all children of an element except the last child using CSS?
When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes. Syntax: element:not(:last-child) { // CSS Style } Example 1: This example creates a navigation menu separat
2 min read
How to get specific number of child elements using CSS?
The :nth-child() in CSS Selector is used to select only those elements that are the nth child, regardless of type, of its parent. Syntax: : nth-child(arg) { // CSS Property; } where arg is an argument that represents the pattern for matching elements. It can be number, odd, even or linear equation. number: It represents the elements whose position
2 min read
How to apply css property to a child element using JQuery?
The task is to apply CSS property to a child element using jQuery. To do that, first we select the child element with the help of children() method in jQuery and then apply CSS property to it with the help of css() method in jQuery. Syntax: // Note : children method selects the direct child to parent $(selector).children("selector").css("property-n
2 min read
How to apply CSS in last child of parent using jQuery ?
In this article, we will see how to set styles in the last child of the parent element using jQuery. To set the style on the last child of parent, we use jQuery :last-child selector. The :last-child selector is used to select every element that is the last child of its parent element. Syntax: $("Selector:last-child") Approach: First, we create an H
1 min read
How to Style Parent Element when Hover Over a Child Element in CSS ?
In this article, we will learn how to style the parent element when hovering over a child element. Styling the parent element when hovering over a child element is a useful CSS technique that allows you to change the appearance of the parent element when the mouse cursor is hovering over one of its child elements. This can be useful for adding inte
2 min read
How to select "last child" with a specific class using CSS ?
In this article, we will learn to select the "last-child" with a specific class name in CSS. The last-child selector allows us to select the last element inside a set of siblings' elements within its defining element. the last-child selector is a pseudo-class that chooses the final member of the siblings in the block that contains it. Approach: The
3 min read
How to expand floated child div's height to its parent's height in CSS ?
Making the height of the floating child div equal to the height of the parent div is referred to as expanding the height of the floated child div to the height of the parent div. Approach 1: Using the CSS "clearfix" method is the first approach to increase a floated child div's height to match its parent's height. When an element is floated, it is
4 min read