Definition and Usage
The ::first-letter CSS pseudo-element selects the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line.
The first letter of an element is not necessarily trivial to identify:
- Punctuation, that is any characters defined in Unicode in the open (Ps), close (Pe), initial quote (Pi), final quote (Pf) and other punctuation (Po) classes, preceding or immediately following the first letter is also matched by this pseudo-element.
- Similarly some languages have digraphs that are always capitalized together, like the IJ in Dutch. In these rare cases, both letters of the digraph should be matched by the ::first-letter pseudo-element. (This is poorly supported by browsers, check the browser compatibility table).
- Finally, a combination of the ::before pseudo-element and the content property may inject some text at the beginning of the element. In that case, ::first-letter will match the first letter of this generated content.
A first line has only meaning in a block-container box, therefore the ::first-letter pseudo-element has only an effect on elements with a display value of block, inline-block, table-cell, list-item or table-caption. In all other cases, ::first-letter has no effect.
Only a small subset of all CSS properties can be used inside a declaration block of a CSS ruleset containing a selector using the ::first-letter pseudo-element:
- All font properties : font, font-style, font-variant, font-weight, font-size, line-height and font-family.
- All background properties : background-color, background-image, background-position, background-repeat, background-size, and background-attachment.
- All margin properties: margin, margin-top, margin-right, margin-bottom, margin-left.
- All padding properties: padding, padding-top, padding-right, padding-bottom, padding-left.
- All border properties: the shorthands border, border-style, border-color, border-width and the longhands properties.
- The color property.
- The text-decoration, text-shadow, text-transform, letter-spacing, word-spacing (when appropriate), line-height, float, vertical-align (only if float is none) CSS properties.
As this list will be extended in the future, it is recommended that you not use any other properties inside the declaration block, in order to keep the CSS future-proof.
Examples
/* make the first letter of every paragraph red and big */ p::first-letter { /* Use :first-letter if support for IE 8 or earlier is needed */ color: red; font-size: 130%; }
Compatibility
Desktop browsers
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 1.0 | 1.0 (1.7 or earlier) | 9.0 | 7.0 | 1.0 (85) |
Old one-colon syntax (:first-line ) | 1.0 | 1.0 (1.7 or earlier) | 5.5 | 3.5 | 1.0 (85) |
Support for the Dutch digraph IJ | Not supported | Not supported | Not supported | Not supported | Not supported |
Mobile browsers
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | NA | 1.0 (1) | Not supported | NA | NA |
Old one-colon syntax (:first-line ) | NA | 1.0 (1) | NA | NA | NA |
Support for the Dutch digraph IJ | NA | Not supported | NA | NA | NA |