CSS first-child Selector

What is the :first-child Selector?

The :first-child selector in CSS is used to style the first instance of an element within its parent. Unlike the :first-of-type selector, which targets the first element of a specific type, :first-child selects the very first child element regardless of its type. This makes it versatile for styling various elements like paragraphs, headings, or list items, as long as they are the first child of their parent.

This selector is especially useful when you need to change the appearance of the first child based on screen size. Using media queries, you can adjust styles for different screen dimensions, enhancing your layout for various devices.

How Does it Function in CSS?

The :first-child selector is a pseudo-class in CSS used to target the first immediate child of a parent element. It doesn't consider siblings of the same type and focuses only on the first element within the parent container.

Example

.container > :first-child {
    /* Your styles here */
}

This CSS snippet will apply styles only to the first child element within the element having the container class.

Enhancing Readability and Organization of Code

Maintaining readable and organized code is crucial for collaboration and quality. Here are some tips:

  1. Use Descriptive Names: Ensure variable names reflect their content or purpose clearly.
  2. Break Down Code into Sections: Use focused functions or methods to organize related tasks.
  3. Comment on Complex Logic: Add comments to explain complex algorithms or reasoning.
  4. Proper Indentation: Use consistent spacing and formatting to make your code visually structured.
  5. Minimize Nesting: Avoid deep nesting of loops or conditions; consider using functions or early returns.
  6. Logical Order: Arrange methods in a sequence that matches the flow of execution.

Targeting Specific Elements Within a Parent

The :first-child selector targets only the direct child of a parent element, not any nested children. It's important to note that it does not apply to pseudo-elements like :before or :after, which are generated by CSS.

Example

.parent > :first-child {
    /* Styles for the first direct child */
}

Parent Element Selection with :first-child

The :first-child selector helps apply styles to the first child within a parent element. It works only on HTML elements and not on pseudo-elements. If there are multiple levels of nesting, only the direct first child is styled.

Example

.container > :first-child {
    /* Styles for the first direct child */
}

Selecting the First Child of a Parent Element

To select the first child of a parent, use the :first-child pseudo-class.

Example

ul li:first-child {
    /* Styles for the first list item */
}

This rule targets only the first list item (li) within an unordered list (ul), leaving other sibling elements unaffected.

Applying Styles to Only the First Child Element

Styling the first child element can be useful when emphasizing certain content, like the first item in a list. By using the :first-child selector, you can apply unique styles without affecting the other elements.

Example

.parent > :first-child {
    /* Styles for emphasis */
}

Targeting Sibling Elements Using :first-child

The :first-child selector allows you to style siblings within the same parent container. It specifically targets the direct child, ignoring nested elements.

Example

.parent > .sibling:first-child {
    /* Styles for the first sibling */
}

Differentiating Between Sibling Elements

The :first-child selector is helpful for differentiating between siblings. It styles only the first child among a set of siblings within the parent.

Example

ul li:first-child {
    /* Style only the first list item */
}

Compatibility Across Browsers

The :first-child selector is widely supported across modern browsers like Chrome, Firefox, Safari, and Edge. However, it is not compatible with Internet Explorer 8 and older versions. For broader compatibility, consider alternative methods or selectors.

Browser Compatibility Summary

  • Supported: Chrome, Firefox, Safari, Edge
  • Not Supported: Internet Explorer 8 and older versions

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate