demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

Philip, after the defeat of Onomarchus, had marched toward the pass of Thermopylae…

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. Alternatively, if you were using an font, you could emply true small caps.

Note that the effect of :first-line is dynamic: as the number of words changes (due to rescaling content or altering the browser window size), only the text that remains 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.

You must be signed up in order to leave comments.

Alternatively, you could just the b or i elements. I think you can argue the case for either:

The b element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood, such as key words in a document abstract, product names in a review, actionable words in interactive text-driven software, or an article lede.

The i element represents a span of text in an alternate voice or mood, or otherwise offset from the normal prose in a manner indicating a different quality of text, such as a taxonomic designation, a technical term, an idiomatic phrase from another language, a thought, or a ship name in Western texts.

posted by leonp

Dudley StoreyA good point, Leon. I'd only point out to readers that your suggestions are an alternative to using <span> for a fixed length of text; using b or i has the same disadvantage in the sense that it's not dynamic or responsive, unlike ::first-line.

posted by Dudley Storey

Hey Dudley, A couple things, relevant one first: If the content is more dynamic or ids/classes couldn't be added, you could use first/nth child, first/nth of type or if desperate enough something like ".content > h2 + p". Less relevant: 1. Your blog is a great resource. Thanks and keep it up. 2. Didn't bother to duplicate it but chrome autofilled my email as my username when I attempted to login. Submitting the login with that in-correct auto-fill failed (of course) but I was not given a visual cue that the login had failed so maybe look into that if it's the case.

posted by vvmike

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

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.