Sub topic 12
Styling the first line and first letter
This chapter assumes you know CSS rule syntax, type and class selectors, and the difference between a pseudo-class and a pseudo-element (two colons, and it styles a part of an element). Here you will style the first line and first letter of a block of text.
What these pseudo-elements select
::first-line and ::first-letter are pseudo-elements. They style a part of a paragraph that has no HTML tag of its own.
::first-linematches the first formatted line of a block container. The browser recalculates it when the viewport resizes, because the line breaks change.::first-lettermatches the first letter of the first formatted line. If punctuation such as an opening quote comes before the letter, the punctuation is included.
Both apply only to block containers, such as p, div, li, and h2. They do nothing on a plain inline element like span, unless you change its display.
A drop cap
A drop cap is a large first letter that sinks into the lines below it. The rule below uses ::first-letter.
p:first-of-type::first-letter {
float: left;
font-size: 3em;
line-height: 1;
margin-right: 0.1em;
}p:first-of-type limits the effect to the first paragraph among its siblings. Floating the letter to the left lets the text wrap around it. Setting line-height: 1 keeps the large letter from adding extra space above the first line.
Styling the first line
::first-line suits a lead-in that should stand out, such as an opening sentence in small caps.
article p:first-of-type::first-line {
font-variant: small-caps;
letter-spacing: 0.05em;
}If you resize the window, the capitalised portion grows or shrinks. The styled text always covers exactly the first rendered line.
Which properties are allowed
These pseudo-elements accept only a limited set of properties. Anything outside the set is ignored.
| Pseudo-element | Allowed property groups |
|---|---|
| `::first-line` | Font, color, background, `text-transform`, `letter-spacing`, `word-spacing`, `line-height`, `text-decoration`, `vertical-align` |
| `::first-letter` | The `::first-line` set, plus margin, padding, border, `float`, and `initial-letter` |
For example, ::first-line ignores margin and float. Use ::first-letter when you need box properties.
Common pitfalls
- Wrong element type. A rule on
span::first-letterdoes nothing while the span is inline. Make itdisplay: blockorinline-block, or target its block parent. - Generated content.
::beforecontent counts as the first letter if it comes first in the block. A decorative symbol added with::beforecan become the "first letter" you are styling. - Images and inline blocks first. If the paragraph starts with an image, an
inline-block, or a table,::first-letterhas no letter to match. - Specificity. A pseudo-element adds type-selector weight, so
p::first-letterscores (0,0,2). A class on the same element does not override it unless the class selector carries the pseudo-element too. - Accessibility. A very large or low-contrast drop cap can hurt readability. Check that the letter still reads as part of its word, and keep color contrast sufficient.
Exercise
Exercise
A paragraph begins with an opening quotation mark followed by the word "Once". You apply p::first-letter { font-size: 2.5em; }. Which characters are enlarged, and why does p::first-line { margin-left: 2rem; } have no effect?
Show answer
The quotation mark and the "O" are both enlarged, because ::first-letter includes leading punctuation before the first letter. The margin-left rule has no effect because ::first-line allows only text-related properties, not margin. Use text-indent on the paragraph instead, or style the whole paragraph with a margin.