How to Use CSS :first-child

Download Now: Free HTML & CSS Coding Templates
Athena Ozanich
Athena Ozanich

Updated:

Published:

As a web developer, you may have encountered scenarios where you need to apply specific styles to the first child element within a parent container. This is where the CSS:first-child selector comes in handy. In this post, we will explore the :first-child selector, understand its usage, and provide code examples to help you leverage its power in your projects.

Young woman studying the power of CSS and how she can leverage the first-child selector.

Download Now: 50 Code Templates [Free Snippets]

What is the :first-child selector?

The :first-child selector is a pseudo-class in CSS that allows you to target the first child element within a parent container. It selects elements that are the first immediate child of their parent. This powerful selector makes it easier to apply unique styles or modifications to that specific element.

Syntax:

To utilize the :first-child selector, follow this syntax:

parentElement:first-child { /* Styles to be applied to the first child element */ }

This may look a little confusing at first but let's take a closer look at some practical examples of the actual usage for this CSS selector.

Code Examples:

Let‘s dive into some code examples to understand how to use the :first-child selector effectively. In the next example, you’ll see how to select the first list item child of an unordered list.

1. Changing the color of the first list item:

To utilize the :first-child selector, follow this syntax:

ul li:first-child { color: #ff0000; }

In this example, the first list item in an unordered list (`ul`) will be styled with a red color using a hexadecimal value. But lists and list items are not the only things that can be targeted and styled this way. Let's look at some other examples of how this can be done with different HTML elements.

2. Adding a margin to the first paragraph inside a section:

section p:first-child { margin-top: 20px; }

This code snippet adds a top margin of 20 pixels to the first paragraph inside a `section` element. This can be extremely useful for controlling the flow and or placement of your site's text copy. This can be especially useful if you want to create a specific pattern with your layout. For example, you may want only the first paragraph to have a top margin since all paragraphs have a bottom margin.

Let's look at how this can be used with tables and cells to customize how you present data.

3. Applying a different background color to the first table cell in a row:

tr td:first-child { background-color: #f1c40f; }

This example targets the first table cell (`td`) in each row (`tr`) and applies a background color of `#F1C40F`. Next up let's take a look at how you can apply this to styling the first in a set of images.

4. Styling the first image in a container:

.container img:first-child { border: 2px solid #3498db; border-radius: 8px; }

Here, the first image inside a container with the class `.container` will have a border of `2px` solid `#3498DB` and a border-radius of `8px`.

The CSS :first-child selector is a powerful tool that enables you to style or modify the first child element within a parent container. This selector provides more precise control and flexibility over your styles, allowing you to create unique and engaging user experiences.

Leveraging Your CSS Skills

By leveraging the :first-child selector, you can apply custom styles, such as changing colors, adding margins, or enhancing element appearance. Remember to experiment and combine this selector with other CSS properties to achieve the desired effects.

We hope this guide has provided you with a solid understanding of the CSS :first-child selector and inspired you to incorporate it into your coding practices. Start using this selector to create visually appealing and dynamic web designs with ease.

New Call-to-action

Related Articles

Dozens of free coding templates you can start using right now

    CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

    START FREE OR GET A DEMO