::first-letter
Styles the first letter of the first line in a block of text. The classic way to make a drop cap at the start of an article.
Example
#.article p:first-of-type::first-letter {
float: left;
margin: 4px 8px 0 0;
font-family: Georgia, serif;
font-size: 3.4em;
line-height: 0.8;
color: #c4b5fd;
}<article class="article">
<p style="margin: 0 0 8px;">Every world in CodeWorlds begins with a short story. The drop cap signals that this is the start of a tale, not an ordinary paragraph of instructions.</p>
<p style="margin: 0; color: #a1a1aa;">The second paragraph has no drop cap, because the rule applies only to the first paragraph in the article.</p>
</article>Definition and usage
#The ::first-letter pseudo-element selects the first letter (or digit) of the first line of a block element. It is most often used for a drop cap: a large, decorative letter at the start of an article, familiar from books and newspapers.
The browser also includes any punctuation right before the letter, for example an opening quotation mark: in text starting with “Ala the whole fragment “A becomes the initial. The pseudo-element works only on block-level elements (block, inline-block, list-item, table cells), so on a plain span it changes nothing.
It supports text and box properties: font, colour, background, margins, padding, border, and float. A multi-line drop cap is made with float: left, a large font-size, and a matching line-height.
Syntax
#selector::first-letter { property: value; }Common styles
#- A larger, coloured first letter, still in the text line.
- A drop cap that the text wraps around over several lines.
- The first letter in a coloured tile.
::first-lettersupports background, padding, and margins.
Common styles in practice
#font-size
.demo::first-letter { font-size: 2em; color: #a78bfa; }float: left
.demo::first-letter {
float: left;
margin: 4px 8px 0 0;
font-family: Georgia, serif;
font-size: 3.2em;
line-height: 0.8;
color: #a78bfa;
}background
::first-letter supports background, padding, and margins..demo::first-letter {
margin-right: 4px;
padding: 2px 8px;
border-radius: 6px;
background: #7c3aed;
color: #fff;
font-weight: 700;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Combine
::first-letterwith:first-of-typeso that only the first paragraph of an article gets the drop cap, not every one. - A drop cap is pure decoration. Don’t split the first letter into a separate HTML element, because screen readers may then read the word in pieces.
Related entries
#- ::first-lineStyles the first line of text in a block element, regardless of how many words fit in it.
- ::beforeCreates a virtual element at the start of an element’s content. Used for icons, decorations, and labels without extra HTML.
- :nth-of-type()Selects elements by position among siblings with the same tag, for example every other image or the third paragraph.
- floatMoves an element to the left or right edge, and text wraps around it on the other side.
- font-sizeSets the font size.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.