Mark
Mark

Reputation: 33

Can I specify styling for specific lines in css?

Is there any way to apply css formatting to just the first (or nth) line of text in CSS? Suppose I have:

<h2>This is a line of text on my web page</h2>

That gets displayed as:

This is a line of text
on my web page

Is there a way to specify, a priori, separate formatting for the first and second lines without knowing where the line break will occur?

Upvotes: 3

Views: 2535

Answers (2)

Shi
Shi

Reputation: 4258

Use the :first-line selector (CSS2) or ::first-line selector (CSS3).

h2:first-line {
  font-weight: bold;
}

Upvotes: 1

BoltClock
BoltClock

Reputation: 724342

There is only the :first-line pseudo-element; there isn't any for the second, third, fourth, ... nth lines.

h2 {
    font-size: 2em;
    color: blue;
}

h2:first-line {
    color: red;
}

Upvotes: 4

Related Questions