CSS ::first-line Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The ::first-line pseudo-element styles the first formatted line of a block container. How long that line is depends on width, font size, and wrapping—not on a fixed word count.

Prefer the double-colon form. Browsers still accept legacy :first-line. For only the opening character, use ::first-letter.

Baseline · Widely available.

Not deprecated. Core support is broad; SVG <text> first-line styling has limited support.

Limited support:

::first-line on SVG text is incomplete in many browsers. Stick to HTML block containers for production.

CSS Syntax

syntax.css
E::first-line { { … }

Example: p::first-line { font-weight: bold; text-decoration: underline; }

Selector Forms

FormDescriptionTry it
p::first-lineStyle the first line of paragraphsTry it
Color + backgroundHighlight the first line with text and background colorTry it
With ::first-letterCombine line and letter emphasisTry it
Narrow boxSee how wrapping changes which words are styledTry it

Allowed Properties

Only a small subset of CSS properties works on ::first-line. Others are ignored.

AllowedExamples
Fontfont, font-size, font-weight, font-family, …
Color & backgroundcolor, background, background-color, …
Texttext-decoration, text-transform, text-shadow, letter-spacing, word-spacing, line-height, vertical-align
Not allowedmargin, padding, border, float, text-indent, most layout properties

How to Use ::first-line

Target a block of text, then apply only the properties that are allowed on ::first-line.

  1. Choose a block container (p, div, heading, …).
  2. Write E::first-line.
  3. Set font, color, background, or text decoration—not margin or float.
  4. Resize the container and confirm the styled span still looks right.
PieceRequired?Role
Block containerYesHolds wrapping text
::first-lineYesSelects the first formatted line
Allowed propertiesYesFont, color, background, text styles

Live Preview

Toggle presets on the sample. Resize the window width—the first line (and which words are styled) can change.

::first-line presets

Margin, padding, border, and float do not work on ::first-line.

Paragraph

In warm ocean waters around the world, you may see a fish leaping from the water and soaring dozens of meters before returning to the ocean.

Preset: bold

CSS Examples

Each snippet focuses on ::first-line. Extra styles stay minimal for study. Use Try It Yourself for a live editor.

Example 1 — Bold and Underline

Emphasize the first line with weight and decoration.

CSS
<style>
  p::first-line {
    font-size: 1.2rem;
    font-weight: bold;
    text-decoration: underline;
  }
</style>

<p>In warm ocean waters around the world, you may see a fish leaping from the water and soaring dozens of meters before returning to the ocean.</p>
Try It Yourself

How It Works

  1. Target the block. p::first-line selects the first formatted line.
  2. Allowable styles. Font size, weight, and text-decoration are valid here.
  3. Only line one. Later lines keep the normal paragraph look.
  4. Wrap matters. Resize the pane to see which words stay on line one.

Example 2 — Color Highlight

Make the first line stand out with color and background.

CSS
<style>
  p::first-line {
    color: #1d4ed8;
    background: #dbeafe;
    font-weight: 700;
  }
</style>

<p>Styles apply only to the first line of this paragraph. After that, all text looks normal again.</p>
Try It Yourself

How It Works

  1. Color the line. color is allowed on ::first-line.
  2. Add background. Background is allowed; it behaves like an inline highlight.
  3. Weight. Bold makes the first line easier to scan.
  4. Not full-bleed. On a left-aligned line, the background may not stretch to the right edge.

Example 3 — Combine with ::first-letter

Style the first line and the opening letter together.

CSS
<style>
  p::first-line {
    font-weight: 700;
    color: #334155;
  }

  p::first-letter {
    color: #dc2626;
    font-size: 1.5rem;
  }
</style>

<p>Flying fish leap from warm waters and glide above the surface before diving again.</p>
Try It Yourself

How It Works

  1. Line styles. ::first-line boldens and colors the first line.
  2. Letter styles. ::first-letter enlarges the opening character.
  3. Both apply. The letter sits inside the first line, so both rules can stack.
  4. Different jobs. Line = whole row; letter = single character.

Edge Cases & Pitfalls

Wrapping and disallowed properties cause most surprises.

props

No margin, padding, border, or float

Those properties are invalid on ::first-line. Stick to font, color, background, and text styles.

wrap

First line changes with width

A narrower box wraps sooner, so different words receive the styles. Always test responsive widths.

inline

Needs a block container

A plain span usually will not get ::first-line styles. Use a block-level element.

child

Inline-block first child can block it

If the first child is an inline block-level box (for example an inline table), ::first-line may have no effect.

vs letter

Not the same as ::first-letter

Need only the opening character? Use ::first-letter.

Browser Support

::first-line is a browser CSS pseudo-element. Node.js, Deno, and Bun do not evaluate it.

✓ Baseline · Widely available

::first-line

Core support is wide (Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, IE 9+). SVG text first-line styling remains limited.

97% Core support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 7+
Full support
Samsung Internet 1+
Full support
Internet Explorer 9+
Full support
::first-line Widely supported

Bottom line: Safe for HTML block typography. Treat SVG text as progressive enhancement. It is not a JavaScript runtime API.

Key Takeaways

  • Role: style the first formatted line of a block.
  • Syntax: prefer ::first-line (pseudo-element).
  • Limits: font/color/background/text styles only—no box model float tricks.
  • Responsive: wrapping changes which words are on line one.

One line: ::first-line styles whatever words currently sit on the first wrapped line of a block.

Frequently Asked Questions

It styles the first formatted line of text inside a block container. Which words count as that line depends on width, font size, and wrapping.
Prefer the double-colon ::first-line form for pseudo-elements. Browsers still accept the older single-colon :first-line for compatibility.
Mainly font, color, background, text-decoration, letter-spacing, word-spacing, text-transform, line-height, text-shadow, and vertical-align. Margin, padding, border, and float do not apply.
::first-line styles the whole first line. ::first-letter styles only the opening letter (plus nearby punctuation). You can use both on the same element.
The first line is not a fixed number of words. When the container gets narrower or wider, wrapping changes and a different stretch of text receives the styles.
No. It is a CSS pseudo-element evaluated by the browser layout engine, not a Node.js or server runtime feature.

Next: ::marker

Style list markers with the ::marker pseudo-element.

::marker tutorial →

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.

8 people found this page helpful