::first-line
Styles the first line of text in a block element, regardless of how many words fit in it.
Example
#.lead::first-line {
color: #fff;
font-weight: 700;
letter-spacing: 0.02em;
}<p class="lead" style="margin: 0; color: #a1a1aa;">Welcome to Ancient Egypt. In this module you will learn the HTML tags that every web page is built from, and you will write your first page.</p>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
#selector::first-line { property: value; }Common styles
#- The first line in uppercase. The range adapts to the paragraph’s width.
- A lighter, bold start of the paragraph.
- The background covers only the text of the first line, like a highlighter.
Common styles in practice
#text-transform
.demo::first-line {
text-transform: uppercase;
letter-spacing: 0.04em;
}color
.demo::first-line { color: #c4b5fd; font-weight: 700; }background
.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-linewith::first-letterand you get a classic newspaper-style opening for an article.
Related entries
#- ::first-letterStyles the first letter of the first line in a block of text. The classic way to make a drop cap at the start of an article.
- text-transformChanges letter case: all uppercase, all lowercase, or capitalized first letters.
- letter-spacingChanges the spacing between letters.
- font-weightSets the font weight, from thin to extra bold.
- ::selectionChanges the appearance of a piece of text selected by the user with the mouse or keyboard.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.