CSS Portal

:first CSS Pseudo Class

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
:first
CSS Pseudo ClassThis pseudo class is supported by all modern browsers.
Updated 31st December 2025

Description

The :first pseudo-class in CSS is a structural pseudo-class used to select the first element of its type within a parent container. It allows developers to apply specific styles to only the first occurrence of an element, which can be useful for creating unique visual emphasis, spacing, or typography differences for the initial element in a list, block, or group of sibling elements. Unlike classes or IDs, which require explicit assignment, :first relies purely on the document structure.

This pseudo-class is often used in conjunction with elements like li, p, or div to style the first child element in lists or content blocks. For instance, it can be used to increase the font size of the first paragraph in an article, highlight the first item in a navigation menu, or add extra margin to the first card in a grid layout.

One important consideration is that :first only targets the first sibling of its type and does not affect nested elements. For example, using :first on a p tag inside a div will only target the first p directly inside that div, and not any subsequent p elements inside nested containers. This specificity allows precise control without interfering with other elements.

A common usage scenario involves combining :first with other selectors or properties. For example, you might want to style the first list item differently:

<ul>
  <li>First item</li>
  <li>Second item</li>
  <li>Third item</li>
</ul>
li:first {
  font-weight: bold;
  color: #2a9d8f;
  margin-bottom: 1em;
}

In this example, only the first li in the list is bold and colored, while the other items remain unaffected. You can also combine :first with CSS properties such as color, margin, or font-weight to achieve different visual effects.

It is worth noting that the more commonly used pseudo-class for selecting the first child of a parent is :first-child, which is more robust in many scenarios because it considers the actual first child element rather than the first occurrence of a type. However, :first can still be helpful for targeting the first instance of a specific element type when the exact child position is less relevant.

Syntax

:first {
  /* ... */
}

Example

<body>

<section class="content">
<h1>Report Title Page</h1>
<p>This is the first page of the document. Because of the :first pseudo-class, it will have a much larger top margin than the subsequent pages.</p>

<div style="page-break-after: always;"></div>

<h1>Page Two</h1>
<p>This page will follow the standard margins defined in the general @page rule.</p>
</section>

</body>
/* General settings for every printed page */
@page {
margin: 2cm;
}

/* Specific settings for ONLY the first printed page */
@page :first {
margin-top: 10cm;
}

/* Standard styling for screen viewing */
body {
font-family: sans-serif;
line-height: 1.6;
color: #333;
}

h1 {
color: #2c3e50;
}
Open in editor

Browser Support

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

This pseudo 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
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee