XML technologies / CSS / Advanced websites with CSS / Text effects and cascading / Indenting lines

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

In our last example, we enhanced the layout in a somewhat unusual way by centering the text below the subheadings. Indenting the first lines is a much more common and advisable typographical method of creating more white space with paragraphs.

With text-indent, you can specify a line’s indentation precisely. This can be done in two ways: using absolute units such as pixels and centimeters, or relative values in ems or percentages in relation to the font size.

We replace the paragraph style rule in our last example with an indentation of two ems:

p {
  text-indent: 2em;
}

The following figure shows our example page with an indentation at the start of each paragraph.

Page layout with indented first paragraph lines

Figure: Page layout with indented first paragraph lines.

Another variation of text-indent is the opposite of indentation, known as a hanging indent. The first line is positioned closer to the left edge of the page than the remaining text. The following figure shows this effect at the beginning of the first paragraph.

Hanging indent in the first line of the first paragraph

Figure: A hanging indent in the first line of the first paragraph.

The following CSS rule defines a hanging indent for all paragraphs of class ausgerueckt:

p.ausgerueckt {
  padding-left: 2em;
  text-indent: -2em;
}

To create a hanging indent for a paragraph, first set its padding toward the page’s left edge with 2em. This provides room to pull the first line in front of the rest of the paragraph: setting text-indent to -2em overrides the padding for the first line and pulls it to the page’s left edge.

In the HTML, simply assign the paragraph with the hanging indent to the CSS class ausgerueckt:

<h1>Die zehn Gebote für eine optimale Performance</h1>
<p class="ausgerueckt">Das sorgfältige Studium und die Analyse von mehr als 35.000 Seiten an Selbsthilfeliteratur führt zu der Schlussfolgerung, dass es letztendlich nur zehn Basisregeln gibt, die zu optimalen Ergebnissen und Erfolg führen.</p>

Negative values for text-indent should always stay within the available space up to the page edge. If there is insufficient space, create some, as in our example using padding-left.

  

 

 

 

back to top