CSS Portal

:first-child CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :first-child pseudo-class in CSS is used to target an element that is the first child of its parent. This means that the element must be the very first among its siblings in the DOM tree to match. It is a structural pseudo-class, allowing developers to apply styles based on the element’s position rather than its type or class. This can be particularly useful when you want to style the first item in a list, table row, or any container without adding extra classes or IDs.

Unlike simple element selectors, :first-child works by considering the element’s placement among all types of children, not just elements of a particular type. For example, if the first child of a container is a text node or a comment, the pseudo-class will not select the next element. For situations where you want the first of a specific type, consider using :nth-of-type() instead.

This pseudo-class is often combined with other selectors to create more precise styling rules. For instance, you might want the first paragraph in every section to have extra spacing, or the first item in a navigation menu to have a different background color. It can also be paired with pseudo-elements like ::before to create decorative effects that only appear on the first child.

Example in HTML and CSS:

<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ul>

<div class="cards">
  <div class="card">Card 1</div>
  <div class="card">Card 2</div>
</div>
ul li:first-child {
  font-weight: bold; /* The first list item is bold */
}

.cards .card:first-child {
  border: 2px solid #007BFF; /* Only the first card has a border */
}

In this example, only the first <li> and the first .card element are affected. Other children remain unaffected, making :first-child a powerful tool for targeting elements based purely on their order in the DOM.

Syntax

:first-child {
  /* ... */
}

Example

<div class="container">
<p>This is the first paragraph. I will be styled!</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
</div>

<div class="container">
<p>I am also a first child in a new container, so I'll be styled too.</p>
<p>Just another paragraph here.</p>
</div>
/* Target the first paragraph in any container */
p:first-child {
color: #2a9d8f;
font-weight: bold;
font-size: 1.2rem;
border-left: 4px solid #2a9d8f;
padding-left: 10px;
}

/* Basic styling for layout */
.container {
margin-bottom: 20px;
padding: 10px;
border: 1px solid #ddd;
}

Browser Support

The following information will show you the current browser support for the CSS :first-child pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!