Controlling Line Height and Letter Spacing

Tutorial 3 of 5

Introduction

This tutorial aims to teach you how to control line height and letter spacing in your HTML and CSS content. By the end of this guide, you will be able to tweak these properties to enhance the readability and design aesthetics of your webpage.

You will learn:
- How to control line height in CSS
- How to control letter spacing in CSS

Prerequisites:
- Basic understanding of HTML and CSS

Step-by-Step Guide

Line height and letter spacing are two critical aspects of typography in web design. They help in setting the visual rhythm and readability of the text.

Line Height

Line height, also known as leading, is the vertical distance between lines of text. In CSS, the line-height property is used to control it.

The line-height property can accept:

  • Unitless numbers: These are multiplied with the current font size to set the line height.
  • Length values (px, em, etc.): These are fixed values regardless of the current font size.
  • Percentage values: These are relative to the current font size.
  • Normal: This is roughly equivalent to 1.2, depending on the browser.

Letter Spacing

Letter spacing, also known as tracking, is the horizontal spacing between letters in a piece of text. In CSS, the letter-spacing property is used to control it.

The letter-spacing property accepts:

  • Length values (px, em, etc.): These are fixed values that are added to the default spacing.
  • Normal: This resets to the default spacing.

Code Examples

Let's see some practical examples:

Example 1: Line Height

<!-- This is your HTML content -->
<p class="leading">The quick brown fox jumps over the lazy dog.</p>
/* This is your CSS content */
.leading {
    line-height: 1.5;
    /* This sets the line height to 1.5 times the current font size */
}

Example 2: Letter Spacing

<!-- This is your HTML content -->
<p class="tracking">The quick brown fox jumps over the lazy dog.</p>
/* This is your CSS content */
.tracking {
    letter-spacing: 2px;
    /* This adds 2px of spacing between each letter */
}

Summary

In this tutorial, we've learned about controlling line height and letter spacing in CSS. Next, you might want to explore other typographical properties like font size, font weight, and text alignment.

Practice Exercises

  1. Exercise 1: Create a paragraph of text with a line height of 2 and a letter spacing of 1px.
  2. Exercise 2: Create a paragraph of text with a line height of 1.5em and a letter spacing of normal.
  3. Exercise 3: Create a paragraph with a line height of 120% and a letter spacing of -1px.

Solutions:

  1. Solution 1:
    html <p style="line-height: 2; letter-spacing: 1px;">Lorem ipsum dolor sit amet.</p>
  2. Solution 2:
    html <p style="line-height: 1.5em; letter-spacing: normal;">Lorem ipsum dolor sit amet.</p>
  3. Solution 3:
    html <p style="line-height: 120%; letter-spacing: -1px;">Lorem ipsum dolor sit amet.</p>

Remember, learning comes with practice, so play around with different values of line height and letter spacing to get a feel for how they affect the presentation of text. Happy coding!