::first-line (:first-line)

Summary

The ::first-line CSS pseudo-element applies styles only to the first line of an element. The amount of the text on the first line depends of numerous factors, like the width of the elements or of the document, but also of the font size of the text. As all pseudo-elements, the selectors containing ::first-line does not match any real HTML element.

A first line has only meaning in a block-container box, therefore the ::first-line pseudo-element has only an effect on elements with a display value of block, inline-block, table-cell or table-caption. In all other cases, ::first-line has no effect.

Only a small subset of all CSS properties can be used inside a declaration block of a CSS ruleset containing a selector using the ::first-line pseudo-element:

As this list will be extended in the future, it is recommended that you not use any other properties inside the declaration block, in order to keep the CSS future-proof.

In CSS 2, pseudo-elements were prefixed with a single colon character. As pseudo-classes were also following the same convention, they were indistinguishable. To solve this, CSS 2.1 changed the convention for pseudo-elements. Now a pseudo-element is prefixed with two colon characters, and a pseudo-class is still prefixed with a single colon.

As several browsers already implemented the CSS 2 version in a release version, all browsers supporting the two-colon syntax also support the old one-colon syntax.

If legacy browsers must be supported, :first-line is the only viable choice; if not, ::first-line is preferred.

Examples

p::first-line { text-transform: uppercase }
Change the letters of the first line of every paragraph to uppercase.
Reference Result

LOREM IPSUM DOLOR SIT AMET,  consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.

DUIS AUTE IRURE DOLOR IN reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

 

p::first-line { margin-left: 20px }
Does nothing, margin-left cannot be applied to a first-line pseudo-element.
Reference Result

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

 

p::first-line { text-indent: 20px }
Does nothing, text-indent cannot be applied to a first-line pseudo-element.
Reference Result

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Specifications

Specification Status Comment
CSS Text-decoration Level 3 Candidate Recommendation Allowed the usage of text-shadow with ::first-letter.
Selectors Level 3 Recommendation The definition of what is the first line of an element has been reworded. The two-colon syntax for pseudo-elements has been introduced.
CSS Level 2 (Revision 1) Recommendation No significant change, though CSS Level 2 still used the one-colon syntax.
CSS Level 1 Recommendation The initial definition used the one-colon syntax.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 1.0 (1.7 or earlier) 9.0 7.0 1.0 (85)
Old one-colon syntax (:first-line) 1.0 1.0 (1.7 or earlier) 5.5 3.5 1.0 (85)
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? 1.0 (1) Not supported ? ?
Old one-colon syntax (:first-line) ? 1.0 (1) ? ? ?

See also

Document Tags and Contributors

Last updated by: anotheruiguy,
Hide Sidebar