CSS pseudo-elementPseudo-elements

::first-line

Styles the first line of text in a block element, regardless of how many words fit in it.

On this page

Example

#
CSS
.lead::first-line {
  color: #fff;
  font-weight: 700;
  letter-spacing: 0.02em;
}
Preview

Definition and usage

#

The ::first-line pseudo-element covers the first line of text as the reader sees it. It is not tied to specific words: when the window width changes, more or less text ends up in the first line, and the style adapts by itself.

It works on block elements and mainly supports text properties: font, colour, background, letter and word spacing, text-transform, text-decoration, and text-shadow. Margins, padding, and borders won’t work, because a line is not a separate box.

A typical use is to highlight the start of an article, for example with uppercase in the first line, often together with a drop cap from ::first-letter. When both apply at once, the first letter inherits the first line’s style and adds its own on top.

Syntax

#
Syntax
selector::first-line { property: value; }

Common styles

#
  • text-transform

    The first line in uppercase. The range adapts to the paragraph’s width.
  • color

    A lighter, bold start of the paragraph.
  • background

    The background covers only the text of the first line, like a highlighter.

Common styles in practice

#

text-transform

The first line in uppercase. The range adapts to the paragraph’s width.
.demo::first-line {
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

color

A lighter, bold start of the paragraph.
.demo::first-line { color: #c4b5fd; font-weight: 700; }

background

The background covers only the text of the first line, like a highlighter.
.demo::first-line { background: rgb(124 58 237 / 0.35); }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Don’t put information in the first line that must always be highlighted. On a phone the line is shorter, so fewer words stand out.
  • Combine ::first-line with ::first-letter and you get a classic newspaper-style opening for an article.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.