Sub topic 12
Styling the first line and first letter
This chapter covers ::first-line and ::first-letter, two pseudo-elements that style part of a block's text without any extra HTML. It assumes you know CSS rule syntax, the type, class, ID and attribute selectors, the difference between a pseudo-class and a pseudo-element (double colon), and the basics of float.
What these two pseudo-elements select
A pseudo-element targets a piece of an element that has no tag of its own. These two target pieces of text inside a block container:
::first-lineselects the first formatted line of a block container, such as a paragraph.::first-letterselects the first letter of the first formatted line of a block container.
"Formatted line" is the key phrase. The browser decides where lines break when it renders the page. So ::first-line covers different words at different window widths. You cannot count on it containing a fixed number of words.
p::first-line {
font-variant: small-caps;
letter-spacing: 0.05em;
}If you resize the window, the small-caps portion grows and shrinks to match the first line. No JavaScript or extra markup is involved.
Where they apply
Both pseudo-elements apply only to block containers. These are elements whose display value is block, list-item, table-caption, table-cell or inline-block. A plain span or a, which is display: inline, has no first formatted line of its own, so the rule does nothing.
/* Works: p is a block container */
p::first-line {
color: #444;
}
/* Does nothing: span is inline by default */
span::first-line {
color: #444;
}
/* Works again once the span becomes a block container */
span.lead {
display: block;
}
span.lead::first-line {
color: #444;
}The pseudo-element attaches to the first block container that holds the text. If a div contains a p as its first child, div::first-line styles the first line of that paragraph. This is often a surprise, so check the nesting when a rule seems to hit the wrong text.
Which properties are allowed
You cannot use every property on these pseudo-elements. The allowed list is deliberately short, because the first line is not a box you can size or position.
| Property group | `::first-line` | `::first-letter` |
|---|---|---|
| Font properties (`font-family`, `font-size`, `font-weight`, `font-variant`) | Yes | Yes |
| Color and background | Yes | Yes |
| Text decoration, `text-transform`, `letter-spacing`, `word-spacing`, `line-height` | Yes | Yes |
| Margin, padding, border | No | Yes |
| `float`, `vertical-align` | No | Yes |
| `width`, `height`, `position` | No | No |
The rule of thumb is that ::first-line accepts properties that change how text looks. ::first-letter accepts those too, and also box properties, because the letter can be floated and given spacing. An unsupported property is ignored. It does not raise an error, so a typo-like mistake looks like "nothing happens".
/* margin is ignored here, so this has no effect on the line */
p::first-line {
margin-left: 2rem;
}
/* text-indent on the block itself is the right tool */
p {
text-indent: 2rem;
}A drop cap with ::first-letter
The classic use is a drop cap, a large first letter that sinks into the lines below it. Floating the letter is what makes the text wrap around it.
.article > p:first-of-type::first-letter {
float: left;
font-size: 3.5em;
line-height: 0.85;
margin: 0.05em 0.1em 0 0;
font-weight: bold;
color: #8a1c1c;
}Each declaration has a job:
float: leftpulls the letter out of the line so following text wraps beside it.font-size: 3.5emmakes the letter larger. Theemis relative to the paragraph's font size.line-height: 0.85keeps the oversized letter from pushing the first line apart.marginleaves a gap between the letter and the wrapped text.
Tuning font-size and line-height so the letter spans exactly two or three lines depends on the font. Check the result with your real font, not a fallback. The CSS initial-letter property is designed for this job, but browser support has varied, so test it before relying on it. The float approach works everywhere.
<div class="article">
<p>Every morning the harbor woke before the town did. Fishing boats
slid out past the breakwater while the lamps were still lit.</p>
<p>By noon the quay was busy with carts and shouting.</p>
</div>The selector p:first-of-type::first-letter limits the drop cap to the first paragraph. The pseudo-class :first-of-type filters the elements and the pseudo-element then picks part of the survivor. The pseudo-element must always come last in a compound selector. p::first-letter:first-of-type is invalid.
What counts as the first letter
The first letter is not always a letter. The browser applies some rules that catch people out:
- Leading punctuation is included. An opening quote mark before the first letter is part of the pseudo-element, so
"Hello"styles"Htogether. Punctuation means opening quotes and brackets, not every symbol. - Digits count. In
1984 was a year, the1is the first letter. - Generated content counts. If a
::beforepseudo-element adds text to the paragraph,::first-lettercan apply to that generated text instead of the first character of the real text. Browsers differ in edge cases here. - Images and other replaced elements do not count. If the paragraph begins with an inline image, there is no first letter to match, and the rule does nothing.
- Preceding inline markup is ignored. If the paragraph starts with
<em>Once</em> upon a time, the first letter is still theO, and the style applies to it even though it sits inside theem. - Non-Latin text may behave differently. In some scripts the browser decides the unit by typographic character rather than by code point. Test it if you publish in those languages.
A leading quote is the most common trap. A drop cap that includes a floated quote mark looks off, because the quote becomes the big character. Many sites accept this. Others remove the quote from the paragraph and wrap it in markup.
Inheritance and the cascade
Pseudo-elements do not inherit from the element in the usual way for every property. The first line inherits from the block it sits in. Anything you set on p::first-line overrides the inherited value for that line only.
Both pseudo-elements add specificity of one element-type selector, the same weight as a type selector like p. For example, p::first-line counts as two type selectors, which is (0, 0, 2). A class on the paragraph still outranks them:
p::first-line {
color: crimson; /* (0,0,2) */
}
.intro::first-line {
color: navy; /* (0,1,1) wins when both match */
}There is one subtlety with inline elements inside the first line. Styles set directly on an inline child, like a strong with its own color, win over ::first-line for that child's text. The ::first-line style acts as if it were applied to a wrapper around the line, so inline descendants with their own declarations override it.
p::first-line {
color: crimson;
}
p strong {
color: black; /* stays black even on the first line */
}Interaction between the two
A letter can sit inside both pseudo-elements. When both apply, ::first-letter is a sub-part of the first line, so its rules are applied on top of ::first-line. Properties that you set on both, such as font-size, resolve in favor of ::first-letter for that letter.
.story::first-line {
font-weight: 600;
text-transform: uppercase;
}
.story::first-letter {
font-size: 2.5em;
color: #8a1c1c;
}The first line becomes bold uppercase, and its first character is also larger and colored.
Common mistakes and edge cases
- Applying the rule to an inline element. The rule matches nothing until the element is a block container.
- Expecting a fixed number of words.
::first-linefollows the layout. A narrow phone screen and a wide desktop give different results. - Using unsupported properties. Margin on
::first-lineandwidthon either pseudo-element are ignored. - Putting the pseudo-element before another selector part. It has to be the last piece:
p:first-child::first-letter, notp::first-letter:first-child. - Using
<br>to force the first line. A forced line break ends the formatted line early, so::first-linecovers only text before the break. - Making a drop cap with real markup. Wrapping the first letter in a
spanadds noise for screen readers and content managers. The pseudo-element keeps the HTML clean. However, if you need a precise design that the pseudo-element cannot reach, markup may be the better tool. - Text a user can select. Style from these pseudo-elements is purely visual. Selecting and copying text yields the normal text, with no extra characters.
Exercises
Exercise
A page has <span class="note">Remember to save your work early and often.</span>. The rule .note::first-line { font-weight: bold; } has no effect. Why, and what is the smallest change that fixes it?
Show answer
A span is display: inline by default, and ::first-line applies only to block containers. Add display: block (or inline-block) to .note, for example .note { display: block; }, and the rule will match.
Exercise
Write one rule that makes only the first paragraph inside .post start with a floated, bold, 3em first letter. Then say why p::first-letter:first-of-type would not work.
Show answer
.post > p:first-of-type::first-letter { float: left; font-size: 3em; font-weight: bold; }. A pseudo-element must be the last part of a compound selector, so p::first-letter:first-of-type is invalid and the whole rule is dropped.
Exercise
A paragraph begins with the text "Never again," she said. You style p::first-letter with a large font. Which characters are enlarged?
Show answer
Both the opening quotation mark and the letter N. Leading punctuation such as an opening quote is included in the first letter, so the pseudo-element covers "N.