The ::first-letter and ::first-line pseudo-elements.

::first-letter
::first-line
::prefix
::suffix

Summary of characteristics of the ::first-letter selector

Quick description
A pseudo-element targeting the first letter of the paragraph.
Status
Standard
W3C Module
CSS Pseudo-Elements Module
 🡇  
 🡅  
Document status: WD (Working Draft)

Document status: REC (Recommendation)

Document status: REC (Recommendation)

Document status: DEPR (Old specification, declared obsolete or superseded)

Description of the ::first-letter and ::first-line pseudo-elements.

The pseudo-element ::first-letter targets the first letter of an element.
The pseudo-element ::first-line targets the first line of text of the element. The latter is dynamic, i.e. it is constantly re-evaluated for example if the width of the Browser window changes or if the font size is changed.

Not all properties can be applied to these two pseudo-elements. Generally speaking, let's say that properties that apply to inline elements are accepted, including:

  • Style of characters (font, size, bold, other attributes, etc.).
  • Color and opacity.
  • Background properties.

The two pseudo-elements ::first-letter and ::first-line are only for elements of the block type. They are not applicable on inline elements such as links (a tags).

Special case of certain characters with the pseudo-element ::first-letter.

If the text of the element begins with a br tag, ::first-letter and ::first-line are empty.

On the other hand, certain characters are not taken into account to determine what is the first letter: parentheses, punctuation marks, etc. These characters are formatted in the ::first-letter format, as well as the character that follows them.
But unfortunately not all browsers follow the same rules. The samples below will give you an idea of what your browser does.

  • Space. This makes sense since the space at the beginning of a text isn't even displayed by HTML. Note that the non-breaking space is considered a normal letter.
  • !Exclamation.
  • ?Question mark.
  • "Double quotation mark.
  • #Dièse.
  • $Dollar.
  • %Percentage.
  • &Commercial (ampersand).
  • 'Apostrophe.
  • (Opening or closing parenthesis, but the element's contents are unlikely to begin with a closing parenthesis.
  • [Opening or closing hook.
  • {Opening or closing brace.
  • *Asterisk, a sign of multiplication.
  • +Plus, sign of addition.
  • -Dash, sign of subtraction.
  • /Slash, slash, division sign.
  • ,Punctuation marks: comma, period, semicolon, colon.
  • =Lower, higher, or equal signs.
  • _Underscore (or underlined white).
  • ^Circumflex.
  • |Vertical bar (pipe).
  • #####Caractères repeated.

Type one or more characters here and see how they are considered by the ::first-letter pseudo-element.

?Test

The pseudo-elements ::prefix and ::suffix.

To format differently the punctuation characters that may precede the first letter or that may follow it, the standard provides the pseudo-elements ::prefix and ::suffix. These are children of ::first-letter: they are used in conjunction with ::first-letter but are still not widely recognized by browsers (2025).

...::first-letter::prefix { ... }

Examples.

Combination of selectors, attention to space.

This line is written directly to the #example1 block, with no intermediate tags.

This text is the content of a p tag included in the #example1 block.

By looking at the CSS code, you'll understand why not all the paragraphs in this #exemple1 block have all the same formatting. Note the space between #example1 and ::first-line in the selector of the first rule. What translates to "the first row of all child elements of #example1" are in red.
This space is not included in the second rule. The latter therefore concerns only the first line of the element #example1, which must be in blue.

Formatting initials.

The CSS code in this second example, as written, is for the first letter of each of the child elements of the #example2 block. As can be seen, each of the paragraphs in this block does indeed have a drop cap.

Usually, when drop caps are used, the line on which the drop cap is located is passed in small caps. This is what has been done here with the pseudo-element ::first-line. By changing the width of your browser window, you will be able to see that this selector is indeed dynamic.

Note: the buttons (input tags) are obviously not selected by ::first-letter, but are not against it with ::first-line.

Browser compatibility.

Both of these selectors are handled well by today's browsers.

Column 1
Browser support for the pseudo-element ::first-letter, which, as its name suggests, targets only the first letter of an element.
Column 2
Browser support for the pseudo-element ::first-line, which targets the first line of text of an element.
1
::first-letter
pseudo-element
2
::first-line
pseudo-element
Estimated overall support.
97%
97%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Chrome

Edge

Firefox

Opéra

Safari

Safari sur IOS

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Samsung Internet

Baidu Browser

QQ Browser

UC Browser pour Androïd

Opéra mini

History of the ::first-letter and ::first-line selectors.

Recall that in the very first versions of CSS, pseudo-elements were, like pseudo-classes, prefixed with a single colon character.

  • Cascading Style Sheets specification - Version 1

    Initial definition of the :first-line and :first-letter pseudo-elements (with a single colon character in this first version).
    WD
    November 17, 1995
    Working Draft.
    PR
    November 12, 1996
    Proposed for recommendation.
    REC
    December 17, 1996
    Recommendation .
    DEPR
    September 13, 2018
    Old specification, declared obsolete.
  • Cascading Style Sheets Level 2 - Revision 1 Specification

    No change in the definition of the :first-line and :first-letter pseudo-elements.
    WD
    November 04, 1997
    Working Draft.
    PR
    March 24, 1998
    Proposed for recommendation.
    REC
    May 11, 1998
    Recommendation .
  • Selectors Level 3

    Since this version, pseudo-elements start with a double colon, to distinguish them from pseudo-classes.
    WD
    August 03, 1999
    Working Draft.
    CR
    November 13, 2001
    Candidate Recommendation.
    PR
    December 15, 2009
    Proposed for recommendation.
    REC
    September 29, 2011
    Recommendation .
  • CSS Pseudo-Elements Module Level 4

    No change to the ::fist-letter and ::fist-line pseudo-elements.
    WD
    January 15, 2015
    Working Draft.
    CR
    PR
    REC

List of pseudo-elements.

These terms are defined in the W3C specification CSS Pseudo-Elements Module .

Selectors:

::after
A pseudo-element designating the location immediately after an element.
::before
A pseudo-element designating the location immediately before an element.
::details-content
Langue française
This pseudo-element targets the detailed content of a details tag.
::file-selector-button
Targets the button in an input box of type file. This button has a different appearance in each browser.
::first-line
A pseudo-element targeting the first line of the paragraph.
::grammar-error
Peudo-elements targeting texts with spelling mistakes (::spelling-error) or grammar errors (::grammar-error).
::highlight()
Langue française
Pseudo-element that targets the text selected after a search in the browser.
::inactive-selection
Langue française
Targets the selected element or text when the window it is on is not active.
::marker
Langue française
A pseudo-element designating the marker in a list (bullet or number).
::placeholder
A pseudo-element designating the placeholder text in an input box (placeholder attribute).
::prefix
Target the punctuation characters that can precede the first letter of an element. Allows for a different styling of the first letter and the characters that come before it.
::search-text
Langue française
Target the text searched by the internet user (browser search zone CTRL+F).
::selection
Langue française
A pseudo-element that targets user-selected content.
::spelling-error
A pseudo-element that targets words or text with orthograph misspellings.
::suffix
Target the punctuation characters that may follow the first letter of an element. Allows for different styling of the first letter and the punctuation characters that follow it.
::target-text
Langue française
Targets the text of the item designated by the url bookmark.