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.
margin, padding, border, float, text-indent, most layout properties
Approach
How to Use ::first-line
Target a block of text, then apply only the properties that are allowed on ::first-line.
Choose a block container (p, div, heading, …).
Write E::first-line.
Set font, color, background, or text decoration—not margin or float.
Resize the container and confirm the styled span still looks right.
Piece
Required?
Role
Block container
Yes
Holds wrapping text
::first-line
Yes
Selects the first formatted line
Allowed properties
Yes
Font, color, background, text styles
Try it
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
Code
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>
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.
How It Works
Target the block.p::first-line selects the first formatted line.
Allowable styles. Font size, weight, and text-decoration are valid here.
Only line one. Later lines keep the normal paragraph look.
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>
::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 Chrome1+
Full support
Mozilla Firefox1+
Full support
Apple Safari1+
Full support
Microsoft Edge12+
Full support
Opera7+
Full support
Samsung Internet1+
Full support
Internet Explorer9+
Full support
::first-lineWidely supported
Bottom line: Safe for HTML block typography. Treat SVG text as progressive enhancement. It is not a JavaScript runtime API.
Remember
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.