The ::first-line pseudo-element styles the entire first line of text inside a block container. It is useful for emphasizing opening sentences and creating editorial typography effects.
01
First line
Whole line.
02
Block boxes
p, article.
03
Width-based
Wraps vary.
04
Font & color
Main props.
05
vs ::first-letter
Line vs char.
06
:: syntax
Pseudo-element.
Fundamentals
Introduction
The ::first-line selector in CSS is a pseudo-element used to apply styles specifically to the first formatted line of text inside a block-level element.
It lets you enhance the opening line of paragraphs, articles, and other block containers without wrapping that text in extra HTML tags.
Definition and Usage
Append ::first-line to a selector with no space before the colons: p::first-line or article p::first-line. Only the first line receives the styles; wrapped lines keep the element’s default appearance.
💡
Beginner Tip
The “first line” is determined by layout — container width, font size, and word breaks. Resize the browser and a different amount of text may become the styled first line.
Foundation
📝 Syntax
The syntax for the ::first-line pseudo-element is:
syntax.css
element::first-line{/* CSS properties */}
Here, element is a block container such as p, div, or article. The pseudo-element selects the first line of text rendered inside that element.
::first-line adds subtle editorial emphasis to long-form text:
Article intros — Make the opening sentence bold or larger.
Blog posts — Draw attention to the lead line before the reader continues.
Pull quotes — Style the first line of a blockquote differently.
Small caps effect — Apply font-variant: small-caps to the first line.
Combined with ::first-letter — Drop cap on the letter, distinct styling on the rest of the line.
Preview
👀 Live Preview
The first line is bold and blue via p::first-line. Subsequent wrapped lines use normal styling:
The CSS ::first-line pseudo-element applies unique styling to the first line of a block-level element. This allows for special text effects limited to the opening line, making it visually distinctive as the paragraph continues below.
Hands-On
Examples Gallery
Practice ::first-line with basic emphasis, article intros, background highlights, and combined letter + line styling.
📜 Core Patterns
Style the opening line of paragraphs and block content.
Example 1 — Bold blue first line
Draw attention to the opening sentence with weight, color, and slightly larger text.
first-line-basic.html
<style>p::first-line{font-weight:700;color:#2563eb;font-size:1.125rem;}</style><p>
The CSS ::first-line pseudo-element applies unique styling to the first line
of a block-level element. This makes the opening sentence visually distinctive.
</p>
The CSS ::first-line pseudo-element applies unique styling to the first line of a block-level element. This makes the opening sentence visually distinctive.
How It Works
Only text on the first formatted line gets the bold blue styling. When the paragraph wraps, lines two and beyond revert to the paragraph’s base styles.
Example 2 — Article intro with small caps
Scope to article paragraphs and apply small-caps typography on the first line.
Typography shines when you combine ::first-letter and ::first-line for rich editorial layouts in long-form content.
How It Works
Both pseudo-elements can apply to the same paragraph. The first letter gets drop-cap styling; the full first line (including that letter) also receives first-line rules.
Watch Out
⚠️ Common Pitfalls
Responsive wrapping — The styled portion changes when the container width or font size changes. Test on mobile.
Unsupported properties — margin, padding, border, and float have no effect on ::first-line.
Inline elements — Plain span elements do not support ::first-line unless display is changed to block or inline-block.
Expecting a fixed word count — You cannot style “the first 10 words” with ::first-line; only the first visual line matches.
Single vs double colon — Prefer ::first-line (pseudo-element) in new code.
A11y
♿ Accessibility
Decorative enhancement — First-line styling is visual; screen readers still read the full paragraph normally.
Do not convey meaning with styling alone — Important information should not depend on first-line appearance.
Contrast — Ensure colored or highlighted first lines meet WCAG contrast requirements.
Readability — Avoid extreme size differences that make the opening line hard to read on small screens.
🧠 How ::first-line Works
1
Browser lays out the text
The block container’s width and font settings determine where line breaks occur.
Layout
2
First line is identified
The browser creates a pseudo-element for all text on the first formatted line.
Match
3
Styles apply to line one
Font, color, and text properties render on that line; wrapped lines stay default.
Render
=
📝
Opening line emphasized
Readers notice the lead sentence before continuing through the paragraph.
Compatibility
🖥 Browser Compatibility
The ::first-line pseudo-element is supported in all modern browsers and has been widely available for years.
✓ Baseline · Universal support
First-line styling everywhere
::first-line works reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
::first-line pseudo-element99% supported
Bottom line:::first-line is safe for typography accents; test responsive line wrapping on different screen sizes.
Wrap Up
🎉 Conclusion
The ::first-line pseudo-element is a useful tool for styling the opening line of block-level text. It adds editorial emphasis without extra HTML markup.
Remember that the first line is layout-dependent, only certain properties apply, and you can combine it with ::first-letter for richer typography. Test across viewport sizes for consistent results.
Use these points when styling opening lines of text.
5
Core concepts
L101
First line
Whole line.
Purpose
::02
Pseudo-element
Double colon.
Syntax
↔03
Width-based
Wraps vary.
Layout
Aa04
Font & text
Main props.
Limits
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The ::first-line pseudo-element matches the first formatted line of text inside a block container. You can style that entire line independently from the rest of the paragraph.
Font properties, color, background, text-decoration, letter-spacing, word-spacing, text-transform, line-height, and related text properties. Margin, padding, border, and float do not apply.
::first-line styles the whole first line of text. ::first-letter styles only the first character. You can combine both on the same element.
The first line depends on container width and font size. When text wraps differently on smaller screens, a different portion of the paragraph receives first-line styling.
Yes. All modern browsers support ::first-line on block containers. Test responsive layouts because line breaks change with viewport width.