CSS text-indent Property: Controlling Text Indentation

The CSS text-indent property specifies the indentation of the first line of text in a block-level element. It’s a useful tool for improving readability and creating visual hierarchy within your content. This guide provides a comprehensive overview of the text-indent property, including its syntax, values, and practical examples.

What is text-indent?

The text-indent property controls the amount of empty space inserted before the first line of text in an element. It only affects the first line of text within the element, and it’s commonly used to create a traditional paragraph indentation style or to visually offset text.

Syntax

The text-indent property can be specified using the following syntax:

selector {
  text-indent: value;
}

Values

The text-indent property accepts the following values:

Value Description
`length` Specifies the indentation as a fixed length (e.g., `20px`, `1em`). Negative values are allowed.
`%` Specifies the indentation as a percentage of the containing element’s width. Negative values are allowed.
`inherit` Inherits the `text-indent` value from the parent element.
`initial` Sets the property to its default value (usually `0`).
`unset` Resets the property to its inherited value if it inherits from its parent or to its initial value if not.

Examples

Let’s explore some practical examples of using the text-indent property to control text indentation.

Example 1: Basic Indentation with Pixels

This example demonstrates how to indent the first line of a paragraph by a fixed number of pixels.

<!DOCTYPE html>
<html>
<head>
<style>
#paragraph1 {
  text-indent: 50px;
}
</style>
</head>
<body>

<p id="paragraph1">This is a paragraph of text. The first line is indented by 50 pixels using the text-indent property. This property is useful for creating a traditional paragraph indentation style.</p>

</body>
</html>

This is a paragraph of text. The first line is indented by 50 pixels using the text-indent property. This property is useful for creating a traditional paragraph indentation style.

In this example, the first line of the paragraph with the ID paragraph1 is indented by 50 pixels.

Example 2: Indentation with EMs

This example demonstrates how to use the em unit for text indentation, which is relative to the font size of the element.

<!DOCTYPE html>
<html>
<head>
<style>
#paragraph2 {
  text-indent: 2em;
}
</style>
</head>
<body>

<p id="paragraph2">This is another paragraph of text. The first line is indented by 2em, which is twice the font size of the element. This provides a flexible indentation that scales with the text size.</p>

</body>
</html>

This is another paragraph of text. The first line is indented by 2em, which is twice the font size of the element. This provides a flexible indentation that scales with the text size.

Here, the first line of the paragraph with the ID paragraph2 is indented by 2em. This value is relative to the font size, so if the font size changes, the indentation will adjust accordingly.

Example 3: Indentation with Percentage

This example shows how to indent the first line of text by a percentage of the containing element’s width.

<!DOCTYPE html>
<html>
<head>
<style>
#paragraph3 {
  width: 50%; /* Set a width for demonstration */
  text-indent: 10%;
}
</style>
</head>
<body>

<div style="width: 400px;">
  <p id="paragraph3">This paragraph has a text-indent of 10%, which is relative to the width of its containing element. The width of the containing element is set to 400px for demonstration purposes.</p>
</div>

</body>
</html>

This paragraph has a text-indent of 10%, which is relative to the width of its containing element. The width of the containing element is set to 400px for demonstration purposes.

In this case, the first line of the paragraph with the ID paragraph3 is indented by 10% of the width of its containing div element (400px), resulting in a 40px indentation.

Example 4: Negative Indentation

Negative values for text-indent can be used to create a hanging indent effect, where the first line extends to the left of the rest of the paragraph.

<!DOCTYPE html>
<html>
<head>
<style>
#paragraph4 {
  text-indent: -30px;
  padding-left: 30px; /* Add padding to prevent the text from being cut off */
}
</style>
</head>
<body>

<p id="paragraph4">This paragraph has a negative text-indent of -30px, creating a hanging indent effect. The padding-left property is used to ensure that the text is not cut off by the edge of the container.</p>

</body>
</html>

This paragraph has a negative text-indent of -30px, creating a hanging indent effect. The padding-left property is used to ensure that the text is not cut off by the edge of the container.

Here, the first line of the paragraph with the ID paragraph4 is indented by -30px, creating a hanging indent. The padding-left property is added to ensure the text is not cut off.

Example 5: Using inherit

The inherit value allows an element to inherit the text-indent value from its parent element.

<!DOCTYPE html>
<html>
<head>
<style>
#parent {
  text-indent: 40px;
}

#child {
  text-indent: inherit;
}
</style>
</head>
<body>

<div id="parent">
  <p id="child">This paragraph inherits its text-indent from its parent div. The parent div has a text-indent of 40px, so this paragraph will also have a text-indent of 40px.</p>
</div>

</body>
</html>

This paragraph inherits its text-indent from its parent div. The parent div has a text-indent of 40px, so this paragraph will also have a text-indent of 40px.

In this example, the paragraph with the ID child inherits the text-indent value from its parent div element, which has a text-indent of 40px.

Practical Tips and Considerations 💡

  • Readability: Use text-indent to enhance the readability of long blocks of text by providing visual separation for each paragraph.
  • Units: Choose the appropriate unit (px, em, %) based on your design requirements and the desired responsiveness of the indentation.
  • Hanging Indents: Use negative text-indent values in combination with padding-left to create hanging indents for bibliographies or lists.
  • Inheritance: Be mindful of inheritance when setting text-indent, and use the inherit value intentionally to maintain consistency across your design.
  • Resetting: Use initial and unset to control how text-indent behaves within your stylesheets.

Browser Support

The text-indent property is supported by all modern web browsers, ensuring consistent rendering across different platforms.

Conclusion

The CSS text-indent property is a simple yet powerful tool for controlling the indentation of the first line of text in a block-level element. By understanding its syntax, values, and practical applications, you can effectively use text-indent to improve the readability and visual presentation of your web content.