CSS ::first-line Selector

Beginner
⏱️ 5 min read
📚 Updated: Jul 2026
🎯 4 Examples
Typography

What You’ll Learn

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.

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.

📝 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.

Basic Example

first-line-basic.css
p::first-line {
  font-weight: 700;
  color: #2563eb;
  font-size: 1.125rem;
}
p::first-line article p::first-line .intro::first-line blockquote::first-line

Syntax Rules

  • Use double colons :: for pseudo-elements in modern CSS.
  • Works on block containers, list items, table cells, and inline-block elements.
  • Only a limited set of properties applies (mostly font and text properties).
  • Margin, padding, border, and float do not work on ::first-line.
  • Can be combined with ::first-letter on the same element.

Supported Properties

SupportedNot supported
font-size, font-weight, font-family, font-stylemargin, padding
color, background, background-colorborder, float
text-decoration, text-transformwidth, height
letter-spacing, word-spacing, line-heightposition, display

Related Selectors

  • ::first-letter — styles only the first character of the first line
  • ::before / ::after — insert generated content
  • ::selection — styles user-highlighted text
  • :first-child — structural pseudo-class for element position (different purpose)

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-element
What it stylesEntire first formatted line of text
Line length depends onContainer width, font size, and content
Common useEmphasize opening sentence in articles
Typical patternp::first-line { font-weight: 700; color: #2563eb; }
Browser supportAll modern browsers

When to Use ::first-line

::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.

👀 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.

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>
Try It Yourself

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.

first-line-article.css
article p::first-line {
  font-variant: small-caps;
  letter-spacing: 0.06em;
  color: #1e293b;
  font-weight: 600;
}
Try It Yourself

How It Works

font-variant: small-caps on ::first-line gives a classic editorial look to the opening line only.

🎨 Enhanced Effects

Highlight and combine first-line styling with other pseudo-elements.

Example 3 — Background highlight on the first line

Use background-color to subtly highlight the opening line of text.

first-line-bg.css
.intro::first-line {
  background-color: #fef9c3;
  color: #713f12;
  font-weight: 600;
}
Try It Yourself

How It Works

Background color on ::first-line creates a highlighter effect on whatever text fits on line one at the current viewport width.

Example 4 — Combine ::first-line and ::first-letter

Stack both pseudo-elements: a drop cap on the first letter and distinct styling on the rest of the first line.

first-line-combined.css
p::first-letter {
  font-size: 2.5rem;
  font-weight: 800;
  color: #7c3aed;
  float: left;
  margin-right: 0.3rem;
  line-height: 0.9;
}

p::first-line {
  font-weight: 600;
  color: #334155;
  font-size: 1.05rem;
}
Try It Yourself

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.

⚠️ Common Pitfalls

  • Responsive wrapping — The styled portion changes when the container width or font size changes. Test on mobile.
  • Unsupported propertiesmargin, 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.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
::first-line pseudo-element 99% supported

Bottom line: ::first-line is safe for typography accents; test responsive line wrapping on different screen sizes.

🎉 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.

💡 Best Practices

✅ Do

  • Use ::first-line (double colon) in new code
  • Test on mobile and desktop widths
  • Stick to font, color, and text properties
  • Scope with article p::first-line
  • Combine with ::first-letter for drop caps

❌ Don’t

  • Apply margin, padding, or float to ::first-line
  • Expect a fixed number of words to be styled
  • Use on plain inline spans without display change
  • Rely on first-line styling for critical meaning
  • Overuse on every paragraph

Key Takeaways

Knowledge Unlocked

Five things to remember about ::first-line

Use these points when styling opening lines of text.

5
Core concepts
:: 02

Pseudo-element

Double colon.

Syntax
03

Width-based

Wraps vary.

Layout
Aa 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with ::first-line typography and responsive wrapping.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful