demosthenes.info

A blog by Dudley Storey on , , , , , , and anything else that strikes his fancy.

featured articles

popular favourites

Classic Typography Effects in CSS: first-line run-in

Another typographic convention is to emphasize the first line or first few words of an opening paragraph, known as a run-in. Typically these words are transformed into small-caps. CSS makes this easy. Returning to the scenario used in the earlier drop-cap exercise, in which the paragraph has an id, the associated CSS for this effect would be:

  1. p#firstpara::first-line { font-variant: small-caps; }

Not every font face has a true small-caps variant; in that case, the browser will fake the equivalent. Other fonts are nothing but small caps: Trajan Pro, for example.

Note that this effect is dynamic: as the number of words in the first line changes (due to rescaling content or altering the browser window size), only the words that remain in the first line of the id firstpara paragraph are affected by this rule.

Affecting just the first few words is somewhat more complicated. There is, as of this writing, no pseudo-selector for :first-word, :second-word or :nth-word. The only solution for the time being would be to wrap a <span> tag around the words you wish to affect, and write a CSS rule for the span in context. Our HTML code would be:

  1. <p id=firstpara">P<span>hilip, after the defeat</span>
  2. of Onomarchus, had marched toward the pass of Thermopylae, which, however, he
  3. found occupied by the Athenians, who had sent a force for the purpose of
  4. preventing his advance. Being baffled there,he directed his march into Thrace,
  5. and alarmed the Athenians for the safety of their dominions in the Chersonese.
  6. </p>

And the added CSS:

  1. p#firstpara span { font-variant: small-caps; }

<span> is a tag that is used when there are absolutely no other alternatives for markup. <span> is an inline tag that is non-semantic: it provides no more information than "here is a set of content". For that reason, you should wrack your brain for any alternative elements to <span> before using it.

web developer guide

featured comment

by JoelB in Goodbye, JQuery Validation: HTML5 Form Errors With CSS3

what i'm reading

A Storm of Swords: A Song of Ice and Fire: Book Three
A Storm of Swords: A Song of Ice and Fire: Book Three

what i'm watching

Californication: The Third Season
Californication: The Third Season

what i'm playing

Mass Effect 3 Collector's Edition
Mass Effect 3 Collector's Edition

what i'm hearing

Dub FX
Dub FX

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.