日本語

How to Style Only the First Line of a Paragraph with CSS ::first-line

​​CSS​HTML,Playwright

To make only the first line of an article lead or section opening bold or a different color, use the CSS ::first-line pseudo-element. Styles written as p::first-line apply only to the first line as it is displayed. You do not need to wrap the text in a <span>. If the screen width changes how many characters fit on the first line, the styles follow whatever the first line is at that moment.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. The font was Noto Sans CJK JP, the font size was 16px, the line height was 1.8, and the paragraph width was 320px. I measured each character in the paragraph to find the number of characters and the position of each line. I also counted the pink pixels on each line in screenshots. I did not test Firefox, Safari, real smartphones, or vertical writing. The browser support versions are values from the compatibility data (@mdn/browser-compat-data 8.1.8).

Applying bold and color with p::first-line

css
p::first-line {
  font-weight: bold;
  color: #d85878;
}

::first-line is a pseudo-element that selects the first formatted line of a block. When you attach it after p, the styles apply only to the first line of the paragraph.

In the test environment, I applied the CSS above to a paragraph that wrapped to four lines. Pink pixels appeared only in the range of the first line, and lines 2 to 4 had zero. Only the 19 characters of the first line became bold pink, and the rest stayed in the normal style.

According to the compatibility data, ::first-line is supported from Chrome 1, Edge 12, Firefox 1, and Safari 1. According to MDN, browsers also accept the single-colon :first-line form used in CSS2.

The first-line range changes with screen width

::first-line does not select a fixed set of characters in the HTML. It selects the characters that fall on the first line when the page is displayed.

When I narrowed the same paragraph from 320px to 240px, the first line dropped from 19 characters to 15. The bold pink range changed to match those 15 characters.

Three paragraphs stacked vertically. The text in the image is in Japanese; the English meaning is given here. Top: a 320px-wide paragraph whose first line, "The morning station area is always crowded with commuters" (cut off partway), is bold pink. Middle: the same paragraph at 240px wide, where the shorter first line is bold pink. Bottom: a paragraph with a background color on ::first-line, where the pink background stops at the end of the first line, "A short first line.", and does not extend to the right edge of the paragraph.
From top: a 320px paragraph, the same paragraph at 240px, and a paragraph with a background color on ::first-line

On a narrow screen such as a smartphone, the first line gets shorter. If you want the first line of a lead to carry the main point, keep in mind that where the first line ends depends on the reader's screen.

If a <br> appears inside the paragraph, the first line ends at that <br>. In a paragraph with a <br> after the sentence "The station square in the morning.", only the five characters of that sentence turned pink.

Larger text means fewer characters on the first line

::first-line also accepts font-size.

css
p::first-line {
  font-size: 20px;
}

In the test environment, the first line's characters grew from 24px to 29px tall, and the spacing between characters widened from 16px to 19.98px. As a result, the first line fit 16 characters instead of 19, and the remaining characters moved to line 2. Lines 2 and later stayed at 16px. Which characters fit on the first line is decided by the character size after the ::first-line rule is applied.

Properties that work: text and background

The CSS specification says ::first-line can take the following kinds of properties:

  • Font properties (font-weight, font-size, etc.)
  • color and opacity
  • Background properties (background-color, etc.)
  • Text decoration properties (text-decoration, etc.)
  • Text layout properties that apply to inline elements (letter-spacing, word-spacing, etc.)

Margins and borders are not in this list. In the test environment, setting padding-left: 2em, margin-left: 2em, or border: 2px solid #0000ff on ::first-line left the first line starting at the left edge of the paragraph (0px). The whole screenshot had zero blue pixels, so no border was drawn. For comparison, adding the same border to a span produced 656 blue pixels.

To indent the first line, set the property on p, not on ::first-line. Setting text-indent: 2em on ::first-line still started the first line at 0px. Setting it on p started the first line at 32px (two characters), and the pink first line then held one character fewer, 18.

Background color is painted only up to the last character of the first line, not across the full width of the paragraph. The bottom image above uses a paragraph with background-color set, where the first line ends at a <br> after "A short first line." When I measured that paragraph, the pink background stopped 89px from the left edge of the paragraph. MDN's ::first-line page also notes that backgrounds are treated like those of inline elements and may not extend to the right edge.

Be careful when you use text-transform: uppercase to capitalize the first line of English text. The compatibility data notes that in Safari (including iOS), text-transform set on ::first-line does not take effect.

span does not work, and div applies to the first paragraph

According to the specification, ::first-line works when it is attached to a block container, such as p or div, that contains lines of text or blocks. Setting span::first-line on an inline <span> did not color any line in the test environment. When I added display: block to the same span, its 19 first-line characters turned pink.

If you set ::first-line on a div that wraps the whole article body, the specification selects the first line of the first block child inside the div. If that first child is a paragraph, the first line of that paragraph is selected.

html
<div class="article-body">
  <p>最初の段落…</p>
  <p>2つ目の段落…</p>
</div>
css
.article-body::first-line {
  color: #d85878;
}

In the test environment, only the first line of the first p turned pink, and every line of the second p kept its original color. If the first child of the div is a paragraph, you can style just the article's opening paragraph without adding a selector such as p:first-child. If the first child is a block such as a heading, the first line of that block is selected instead.

Links on the first line keep their own color

Styles from ::first-line are inherited by child elements that sit on the first line. The specification says that the parts of child elements that fall on the first line inherit values from ::first-line as if it were their parent.

Only properties that inherit normally, such as color and font properties, are passed down. Under the CSS inheritance rules, this happens only when the child has no declared value for the property. I put a <a> and a <span> on the first line and set p::first-line { color: #d85878; }.

html
<p><a href="#">リンク</a>と<span>spanの文字</span>が1行目にある段落です。…</p>

The characters in the <span> turned pink, but the characters in the <a> stayed blue. The HTML specification includes a default CSS rule that sets the color of unvisited links (:link) to #0000EE. The <a> in the test environment also computed to rgb(0, 0, 238) (#0000EE). A link has its own color, so it does not inherit the color from ::first-line.

If you want the link on the first line to match, set the a color to inherit.

css
p::first-line {
  color: #d85878;
}

a {
  color: inherit;
}

I tested a link that spanned the end of line 1 and the start of line 2. The part of the link on the first line, which ends in the middle of a Japanese word meaning "is crowded," turned pink, while the part on line 2, which continues with a phrase about leaving the ticket gate and reaching a bakery, had no pink pixels. Only the portion on the first line inherited the ::first-line color. However, inherit removes the browser's default blue for links. The a rule above applies to every link on the page, so in practice you should limit it, for example to the links inside the lead paragraph. If you have removed the underline, use some other visual cue so readers can still tell the text is a link.

When you check values with JavaScript, pay attention to which element you read. The <span> that appeared pink still reported its original color (rgb(31, 42, 48)) from getComputedStyle. To read the ::first-line values, pass the pseudo-element as the second argument, as in getComputedStyle(p, "::first-line"). That returned color as rgb(216, 88, 120) and font-weight as 700. The ineffective padding-left returned 0px, even though I had set it to 2em.

​Posted by 86