demosthenes.info

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

featured articles

popular favourites

Classic Typography Effects in CSS: Hanging Indent

Hanging Indent screenshotA hanging indent, or hanging paragraph, is a paragraph in which the opening line is left of the margin. In other words, the first line has a negative text indent, and that is exactly how it is created in CSS. First, our HTML markup:

  1. <h3>The Objectification of My Monkey</h3>
  2. <p id=”firstpara”>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  3. Suspendisse ac ligula a turpis placerat pharetra. Fusce est erat, hendrerit sit
  4. amet ultricies et, tincidunt a sapien. Vivamus quis mi vel neque commodo condimentum.
  5. Mauris vel turpis quam, sed fringilla ipsum. Suspendisse potenti. Suspendisse a
  6. sodales arcu. In quis augue nisl, vitae scelerisque nibh. Duis aliquam consequat
  7. velit, at mattis augue rhoncus at.</p>

Then the CSS code, written embedded or linked:

  1. p { text-indent: -4em; }

Our immediate problem is that this style would apply itself to every paragraph. We need to restrict the effective range of the style to the first paragraph. So long as we’re ignoring IE6 (which would require a referenced id on the paragraph to work similarly) and are consistent in our markup, the easiest way is with an adjacent selector:

  1. h3 + p { text-indent: -4em; }

This creates a problem of its own: the first line of our first paragraph is now likely to go off the left edge of the browser window. To compensate, push the paragraph to the right with margin-left:

  1. h3 + p { text-indent: -4em; margin-left: 6em; }

We probably want all the paragraphs to share the same left edge, so rather than applying the margin-left to just the first paragraph after the h3, apply it to all paragraphs. Typically, we would also balance the margin on the right-hand side and justify all paragraphs:

  1. p { margin-left: 6em; margin-right: 6em; text-align: justify; }
  2. h3 + p { text-indent: -4em; }

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.