Favorites

Alignment and Indentation - HTML Tutorial

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

You've learned how to add space around boxes with margins and padding. This section is about arranging the text inside a box: aligning it, indenting it and keeping it comfortable to read.

Text Alignment

The text-align property sets the horizontal alignment of text and other inline content inside a block:

  • left: lines up with the left edge (the default for English).
  • right: lines up with the right edge.
  • center: centers each line.
  • justify: stretches lines so both edges are straight.
  • start and end: like left and right, but they flip automatically for right-to-left languages such as Arabic and Hebrew.
Example: Text Alignment
<style>
  .box     { border: 2px solid #999; padding: 0.5rem; margin-bottom: 0.5rem; }
  .left    { text-align: left; }
  .center  { text-align: center; }
  .right   { text-align: right; }
  .justify { text-align: justify; }
</style>

<p class="box left">This text is aligned to the left.</p>
<p class="box center">This text is centered.</p>
<p class="box right">This text is aligned to the right.</p>
<p class="box justify">This text is justified. When a paragraph is long enough
to wrap over several lines, the browser adds space between the words so that
every line except the last one reaches both edges of the box.</p>

Left-aligned text is the easiest to read for long passages. Centered text works for short headings and captions, but long centered paragraphs are hard to follow. Justified text can leave uneven gaps between words, especially in narrow columns.

Indenting the First Line

By default, paragraphs on the web aren't indented. To indent the first line of each paragraph, as in many printed books, use text-indent:

Example: Indented Paragraphs
.story p {
  text-indent: 2em;
  margin: 0;
}

Books usually use either indents or space between paragraphs, not both, which is why this example also removes the paragraph margins.

Hanging Indents

A negative text-indent pulls the first line out to the left, while the rest of the lines stay in place. This is called a hanging indent, and it's often used for bibliographies and references. Add the same amount of left padding so the first line doesn't hang outside the box:

Example: A Hanging Indent
<style>
  .reference {
    padding-left: 2em;
    text-indent: -2em;
  }
</style>

<p class="reference">Baker, Ann. <cite>Pancakes for Beginners</cite>.
Corner Cafe Press. A simple guide to making light, fluffy pancakes at home,
with recipes for every season.</p>

Centering Blocks

text-align: center centers the content inside a box, but it doesn't move the box itself. To center a whole block, such as a column of text, give it a maximum width and automatic side margins, as you saw in Margins and Padding:

.container {
  max-width: 40rem;
  margin-inline: auto;
}

To center something vertically, or to center it in both directions at once, use Flexbox or Grid. You'll learn how in Flexbox Basics.

Comfortable Line Length

Very long lines of text are tiring to read, because your eyes have to travel a long way to find the start of the next line. Most people read comfortably at around 50 to 75 characters per line.

The ch unit is roughly the width of one character, which makes this easy to set:

Example: A Readable Column of Text
article {
  max-width: 65ch;
  margin-inline: auto;
  line-height: 1.6;
}

With these three lines, your text sits in a centered column that's a comfortable width on large screens and fills the screen on small ones.

That's the end of Styling Basics! In the next chapter, you'll put your new CSS skills to work on tables.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee