CSS Reference ยท Pseudo-elements
The ::first-line pseudo-element
Reference for the CSS ::first-line pseudo-element: style only the first rendered line of a block, with the allowed properties and how it reacts to resizing.
The ::first-line pseudo-element selects the first formatted line of a block container. Which words are on that line depends on the width of the box, the font and the window size, so the styled text changes as the layout changes. That is the point: it gives an editorial "lead-in" look that adapts by itself.
It accepts only a small set of text-related properties, so you cannot use it for margins, floats or backgrounds that change the box.
article p::first-line { font-variant: small-caps; font-weight: bold; }
Try it
Syntax
selector::first-line { /* a limited set of properties */ }
| Property | Value |
|---|---|
| Applies to | Block containers |
| Inherited | Inherits from the originating block |
| Animatable | Its properties follow their own animation rules |
Values
The pseudo-element takes no arguments. Allowed properties:
| Group | Examples |
|---|---|
| Fonts | font-*, line-height |
| Color and background | color, background-*, opacity |
| Text | text-decoration, text-transform, text-shadow, letter-spacing, word-spacing, vertical-align |
Box properties such as margin, padding, border and float are not allowed.
Usage notes
- Depends on layout. Resizing the window, zooming or changing the font can change which text is on the first line.
- Nested blocks. The first line is the first line of the first formatted block inside the element, so
div::first-linecan reach into a leading paragraph. - Specificity with inline tags. Properties set on inline elements inside the first line can override the pseudo-element when they have a higher specificity.
- Size changes reflow. A bigger
font-sizeon the first line can change how many words fit, and browsers then recompute it. Keep size changes modest. - Accessibility. Avoid conveying meaning only through the style of the first line. Uppercase transforms can hurt readability in long runs.
- Not for the first sentence. If you need exact words styled, wrap them in an element.
Browser support
Supported in every browser.