Definition and Usage
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:
- all font-related properties: font, font-style, font-variant, font-weight, font-size, line-height and font-family
- the color property
- all background-related properties: background-color, background-image, background-position, background-repeat, background-size, and background-attachment
- word-spacing, letter-spacing, text-decoration, text-transform, and line-height
- text-shadow.
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.
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. |
Compatibility
Desktop browsers
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) |
Mobile browsers
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | NA | 1.0 (1) | Not supported | NA | NA |
Old one-colon syntax (:first-line ) | NA | 1.0 (1) | NA | NA | NA |