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:
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).::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 meaning only in a block-container box, therefore the ::first-letter
pseudo-element has an effect only 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:
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
and font-family
.background
, background-color
, background-image
, background-clip
, background-origin
, background-position
, background-repeat
, background-size
, background-attachment
, and background-blend-mode
.margin
, margin-top
, margin-right
, margin-bottom
, margin-left
.padding
, padding-top
, padding-right
, padding-bottom
, padding-left
.border
, border-style
, border-color
, border-width
, border-radius
, border-image
, and the longhands properties.color
property.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 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.
In CSS 2, pseudo-elements were prefixed with a single colon character. As pseudo-classes were also following the same convention, they were indistinguishable. To solve this, CSS 2.1 changed the convention for pseudo-elements. Now a pseudo-element is prefixed with two colon characters, and a pseudo-class is still prefixed with a single colon.
As several browsers already implemented the CSS 2 version in a release version, all browsers supporting the two-colon syntax also support the old one-colon syntax.
If legacy browsers must be supported, :first-letter
is the only viable choice; if not, ::first-letter
is preferred.
/* CSS3 syntax */ ::first-letter { style properties } /* CSS2 syntax */ :first-letter { style properties }
Make the first letter of every paragraph red and big.
<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. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy amet.</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, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</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>
p::first-letter { /* Use :first-letter if support for IE 8 or earlier is needed */ color: red; font-size: 130%; }
Specification | Status | Comment |
---|---|---|
CSS Pseudo-Elements Level 4 The definition of '::first-letter' in that specification. | Working Draft | Generalized allowed properties to typesetting, text decoration and inline layout properties, opacity and box-shadow . |
CSS Text Decoration Level 3 The definition of 'text-shadow with ::first-letter' in that specification. | Candidate Recommendation | Allowed the usage of text-shadow with ::first-letter . |
Selectors Level 3 The definition of '::first-letter' in that specification. | Recommendation | Defined edge-case behavior like in list items, or with language specific behavior (like the Dutch digraph IJ ). The two-colon syntax for pseudo-elements has been introduced. |
CSS Level 2 (Revision 1) The definition of '::first-letter' in that specification. | Recommendation | No significant change, though CSS Level 2 still used the one-colon syntax. |
CSS Level 1 The definition of '::first-letter' in that specification. | Recommendation | The initial definition used the one-colon syntax. |
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-letter ) | 1.0 | 1.0 (1.7 or earlier) | 5.5 | 3.5 | 1.0 (85) |
Support for the Dutch digraph IJ
| No support | No support bug 92176 | No support | No support | No support |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | ? | 1.0 (1) | No support | ? | ? |
Old one-colon syntax (:first-letter ) | ? | 1.0 (1) | ? | ? | ? |
Support for the Dutch digraph IJ
| ? | No support | ? | ? | ? |
::first-line
pseudo-element.
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter