Pseudo-Elements: ::first-line and ::first-letter
You'll learn how to apply special styling to the very first line or letter of text, creating elegant typographic effects like drop caps and distinctive opening lines.
What Are ::first-line and ::first-letter?
These pseudo-elements let you target and style specific parts of text content without adding extra HTML markup. Think of them like invisible decorations you can apply to just the beginning of a paragraph — perfect for making your text look more polished and professional, like in a printed book or magazine.
::first-letter targets the first character of a block-level element's text. It's commonly used to create "drop caps" (those large decorative first letters you see at the start of chapters).
::first-line targets the entire first line of text in a block element, adjusting dynamically as the viewport resizes and text reflows.
How to Use Them
p::first-letter {
font-size: 3em;
font-weight: bold;
color: #8b4513;
float: left;
margin-right: 5px;
}
p::first-line {
font-weight: bold;
color: #2c3e50;
text-transform: uppercase;
}
Analogy
Imagine you're decorating the first page of a handwritten letter. ::first-letter is like drawing an elaborate illuminated capital at the start, while ::first-line is like using your fanciest handwriting for just the opening sentence — everything else stays normal.
Important Limitations
These pseudo-elements only work on block-level elements (like paragraphs) and support limited CSS properties — mainly font, color, background, and text decorations. You cannot use them with inline elements like <span>.
Key Takeaway: Use ::first-letter and ::first-line to add professional typographic touches to your text openings without cluttering your HTML with extra elements.