liveBook · Manning

::first-letter pseudo-element

As explained in Tiny CSS Projects

The CSS ::first-letter pseudo-element selects the first typographic character of an element's rendered text.

Use `::first-letter` to style the first letter of an element’s text without wrapping that letter in an HTML element. The double colon identifies it as a pseudo-element: it selects part of an element rather than the whole element. section 1.6.3

Accent the opening paragraph

Start with a paragraph immediately after a header. Add `header + p::first-letter { color: crimson; font-style: italic; }`. The adjacent sibling selector picks that paragraph; `::first-letter` makes only its opening letter crimson and italic. section 1.6.3

For a drop cap, `p:first-of-type::first-letter` combines a pseudo-class that picks the first paragraph with a pseudo-element that picks its opening letter. Increase its font size, adjust its line height, and float it left so the following text wraps beside it. An ornate font can make the letter stand out, but such fonts are better kept to short pieces of text. section 7.1.3

Listing 7.4 Styling and positioning the first letter of the first paragraph
@import url(
  'https:/ /fonts.googleapis.com/css2?
 family=Passions+Conflict&display=swap'                #1
);
 
p:first-of-type::first-letter {                          #2
  font-size: 6em;                                        #2
  float: left;                                           #2
  line-height: .5;                                       #2
  font-family: 'Passions Conflict', cursive;             #2
}                                                        #2

See the complete drop-cap CSS, including the imported font, font size, line height, and left float. section 7.1.3

How Tiny CSS Projects covers it

How deep it goes
Works through two selectors and their declarations, including font sizing, line height, and float for a drop cap.
Examples
Shows a crimson italic opening letter and an ornate, floated drop cap in the first paragraph.
What it assumes
Introduces the pseudo-element distinction while working from paragraph selectors and CSS declarations.
What you can do after
Build it from scratch by selecting an opening letter and styling it as an accent or drop cap.
How it connects
Connects pseudo-elements with adjacent sibling selectors, the `:first-of-type` pseudo-class, typography, and float.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.