Skip to main content

Pseudo-elements for Text

Parts of a text, such as its first line or first letter, can be styled on their own with text pseudo-elements.


The CSS ::first-line Pseudo-element

To give the first line of a text a special style, use ::first-line.

Note: Only block-level elements accept ::first-line.

Example

Every <p> element here gets a formatted first line of text.

Prompt
Demonstrate the ::first-line pseudo-element. Format the first line of text in all p elements with color: red, font-variant: small-caps, and font-size: 19px. Include a paragraph with enough text to span multiple lines.
Output: complete HTML page

The CSS ::first-letter Pseudo-element

To give the first letter of a text a special style, use ::first-letter.

Note: Only block-level elements accept ::first-letter.

Example

Every <p> element here gets a formatted first letter.

Prompt
Demonstrate the ::first-letter pseudo-element. Format the first letter of the text in all p elements with color: red and font-size: xx-large. Include a paragraph with some text.
Output: complete HTML page

Pseudo-elements and HTML Classes

Pseudo-elements pair easily with HTML classes: put the class in the selector alongside the pseudo-element.

Example

In <p> elements with class "intro", the first letter is red and in a larger size.

Prompt
Combine a pseudo-element with an HTML class. Display the first letter of p elements with class="intro" in red (#ff0000) and 200% font-size. Include both a paragraph with the "intro" class and one without, to show the difference.
Output: complete HTML page

Multiple Pseudo-elements

One element can also take several pseudo-elements at once.

Example

Each part of a <p> element looks different here. Its first letter is red and xx-large, the remainder of its first line is blue and in small-caps, and everything after that keeps the default font size and color.

Prompt
Combine multiple pseudo-elements on the same element. Style the first letter of p elements with color: red and font-size: xx-large. Style the first line with color: blue and font-variant: small-caps. Include a paragraph with enough text to show both effects.
Output: complete HTML page