Skip to content

Text styling

  • Text color and alignment
  • Text decoration and transformation
  • Line height and letter spacing

Text styling is a key aspect of web design that involves adjusting the appearance of text to enhance readability, aesthetics, and overall user experience. CSS provides a variety of properties to style text, including text color, alignment, decoration, transformation, line height, and letter spacing.

The color property is used to set the color of the text. It can be specified using color names, hexadecimal values, RGB, RGBA, HSL, or HSLA values.

Examples:

/* Using a color name */
.text-example {
color: red;
}
/* Using a hexadecimal value */
.text-example {
color: #ff5733;
}
/* Using RGB */
.text-example {
color: rgb(255, 87, 51);
}
/* Using RGBA */
.text-example {
color: rgba(255, 87, 51, 0.8);
}
/* Using HSL */
.text-example {
color: hsl(9, 100%, 60%);
}
/* Using HSLA */
.text-example {
color: hsla(9, 100%, 60%, 0.8);
}

The text-align property is used to set the horizontal alignment of text within an element. It can take the following values:

  • left: Aligns the text to the left.
  • right: Aligns the text to the right.
  • center: Centers the text.
  • justify: Stretches the lines so that each line has equal width, and the left and right edges are aligned.

Examples:

.text-example {
text-align: left;
}
.text-example {
text-align: right;
}
.text-example {
text-align: center;
}
.text-example {
text-align: justify;
}

The text-decoration property is used to set the decoration of text, such as underlines, overlines, line-throughs, and color of the decoration.

Values:

  • none: No decoration.
  • underline: Underlines the text.
  • overline: Draws a line above the text.
  • line-through: Draws a line through the text.
  • underline overline: Underlines and overlines the text.

Examples:

.text-example {
text-decoration: underline;
}
.text-example {
text-decoration: overline;
}
.text-example {
text-decoration: line-through;
}
.text-example {
text-decoration: underline overline;
}
/* Text-decoration color */
.text-example {
text-decoration: underline;
text-decoration-color: red;
}

The text-transform property is used to control the capitalization of text.

Values:

  • none: No transformation.
  • capitalize: Capitalizes the first letter of each word.
  • uppercase: Converts all characters to uppercase.
  • lowercase: Converts all characters to lowercase.

Examples:

.text-example {
text-transform: capitalize;
}
.text-example {
text-transform: uppercase;
}
.text-example {
text-transform: lowercase;
}

The line-height property is used to set the amount of space between lines of text. It can take numeric values, percentages, lengths, or the keyword normal.

Examples:

/* Numeric value (relative to the font size) */
.text-example {
line-height: 1.5; /* 1.5 times the font size */
}
/* Length value */
.text-example {
line-height: 24px;
}
/* Percentage */
.text-example {
line-height: 150%;
}
/* Keyword */
.text-example {
line-height: normal;
}

The letter-spacing property is used to control the amount of space between characters in a text.

Examples:

/* Positive value */
.text-example {
letter-spacing: 2px;
}
/* Negative value */
.text-example {
letter-spacing: -1px;
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text Styling Example</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<p class="text-color">This is a text with color.</p>
<p class="text-align">This text is centered.</p>
</body>
</html>

CSS:

.text-color {
color: #ff5733;
}
.text-align {
text-align: center;
}

Example: Text Decoration and Transformation

Section titled “Example: Text Decoration and Transformation”

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text Styling Example</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<p class="text-decoration">This text is underlined.</p>
<p class="text-transformation">this text is transformed to uppercase.</p>
</body>
</html>

CSS:

.text-decoration {
text-decoration: underline;
}
.text-transformation {
text-transform: uppercase;
}

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text Styling Example</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<p class="line-height">This text has increased line height.</p>
<p class="letter-spacing">This text has increased letter spacing.</p>
</body>
</html>

CSS:

.line-height {
line-height: 1.8;
}
.letter-spacing {
letter-spacing: 3px;
}

By understanding and utilizing these text styling properties, you can greatly enhance the readability and visual appeal of your web content.