The ::first-letter
CSS pseudo-element applies styles to the first letter of the first line of a block-level element, but only when not preceded by other content (such as images or inline tables).
The first letter of an element is not always trivial to identify:
- Punctuation that precedes or immediately follows the first letter is included in the match. Punctuation includes any Unicode character defined in the open (Ps), close (Pe), initial quote (Pi), final quote (Pf), and other punctuation (Po) classes.
- Some languages have digraphs that are always capitalized together, like the
IJ
in Dutch. In these cases, both letters of the digraph should be matched by the::first-letter
pseudo-element. (This is poorly supported by browsers; see the browser compatibility table below.) - A combination of the
::before
pseudo-element and thecontent
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.
Only a small subset of CSS properties can be used with the ::first-letter
pseudo-element:
- All font properties :
font
,font-style
,font-feature-settings
,font-kerning
,font-language-override
,font-stretch
,font-synthesis
,font-variant
,font-variant-alternates
,font-variant-caps
,font-variant-east-asian
,font-variant-ligatures
,font-variant-numeric
,font-variant-position
,font-weight
,font-size
,font-size-adjust
,line-height
andfont-family
- All background properties :
background
,background-color
,background-image
,background-clip
,background-origin
,background-position
,background-repeat
,background-size
,background-attachment
, andbackground-blend-mode
- 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
,border-radius
,border-image
, and the longhands properties - The
color
property - The
text-decoration
,text-shadow
,text-transform
,letter-spacing
,word-spacing
(when appropriate),line-height
,text-decoration-color
,text-decoration-line
,text-decoration-style
,box-shadow
,float
,vertical-align
(only iffloat
isnone
) CSS properties
CSS3 introduced the ::first-letter
notation (with two colons) to distinguish pseudo-classes from pseudo-elements. Browsers also accept :first-letter
, introduced in CSS2.
Syntax
/* CSS3 syntax */ ::first-letter /* CSS2 syntax */ :first-letter
Example
Make the first letter of every paragraph red and big.
HTML
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est.</p> <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat.</p> <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat.</p> <p>-The beginning of a special punctuation mark.</p> <p>_The beginning of a special punctuation mark.</p> <p>"The beginning of a special punctuation mark.</p> <p>'The beginning of a special punctuation mark.</p> <p>*The beginning of a special punctuation mark.</p> <p>#The beginning of a special punctuation mark.</p> <p>「特殊的汉字标点符号开头。</p> <p>《特殊的汉字标点符号开头。</p> <p>“特殊的汉字标点符号开头。</p>
CSS
p::first-letter { color: red; font-size: 130%; }
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Pseudo-Elements Level 4 The definition of '::first-letter' in that specification. |
Working Draft | Generalizes allowed properties to typesetting, text decoration, inline layout properties, opacity , and box-shadow . |
CSS Text Decoration Module Level 3 The definition of 'text-shadow with ::first-letter' in that specification. |
Candidate Recommendation | Allows the use of text-shadow with ::first-letter . |
Selectors Level 3 The definition of '::first-letter' in that specification. |
Recommendation | Introduction of the two-colon syntax. Definition of edge-case behavior, such as in list items or with specific languages (e.g., the Dutch digraph IJ ). |
CSS Level 2 (Revision 1) The definition of '::first-letter' in that specification. |
Recommendation | No change. |
CSS Level 1 The definition of '::first-letter' in that specification. |
Recommendation | Initial definition, using the one-colon syntax. |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1.0 (1.7 or earlier) | 9.0 | 7.0 | 1.0 (85) |
Old one-colon syntax (:first-letter ) |
1.0 | (Yes) | 1.0 (1.7 or earlier) | 5.5 | 3.5 | 1.0 (85) |
Support for the Dutch digraph IJ |
No support | No support | No support bug 92176 | No support | No support | No support |
Feature | Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | (Yes) | 1.0 (1) | No support | ? | ? |
Old one-colon syntax (:first-letter ) |
? | (Yes) | 1.0 (1) | ? | ? | ? |
Support for the Dutch digraph IJ |
? | No support | No support | ? | ? | ? |
See also
Document Tags and Contributors
Tags:
Contributors to this page:
mfluehr,
erikadoyle,
Sebastianz,
yisibl,
HTMLValidator,
floEdelmann,
LouisLazaris,
bradleyflood,
MusikAnimal,
teoli,
NosCandidats,
kscarfone,
awendland,
Matt_Coughlin,
Sheppy,
syoichi,
FredB,
brianloveswords,
Jürgen Jeka,
BijuGC
Last updated by:
mfluehr,