CSS text-indent property
The text-indent
Property in CSS
The text-indent
property in CSS is used to control the indentation of the first line of text within an element. This property is often used to create visual separation for paragraphs, improve readability, or adhere to specific formatting styles.
Values of text-indent
Length Values (e.g.,
px
,em
,rem
, etc.)- Description: Specifies a fixed or relative amount of indentation for the first line of text using units such as pixels, ems, or rems. Positive values indent the text to the right, while negative values indent it to the left.
- Examples:
.indent-positive { text-indent: 20px; /* Indents the first line of text by 20 pixels */ } .indent-negative { text-indent: -10px; /* Indents the first line of text to the left by 10 pixels */ } .indent-relative { text-indent: 2em; /* Indents the first line of text by 2em, relative to the font size */ }
Percentage
- Description: Specifies the indentation as a percentage of the width of the containing block. This can be useful for responsive designs.
- Example:
.indent-percentage { text-indent: 5%; /* Indents the first line of text by 5% of the width of the containing block */ }
inherit
- Description: Inherits the
text-indent
value from the parent element. This allows for consistent indentation across nested elements. - Example:
.inherit-indent { text-indent: inherit; /* Inherits the text-indent value from the parent element */ }
- Description: Inherits the
Examples
Positive Indentation
.positive-indent { text-indent: 30px; /* Indents the first line of text by 30 pixels */ }
Negative Indentation
.negative-indent { text-indent: -15px; /* Moves the first line of text to the left by 15 pixels */ }
Relative Indentation
.relative-indent { text-indent: 1.5em; /* Indents the first line of text by 1.5em */ }
Percentage Indentation
.percentage-indent { text-indent: 10%; /* Indents the first line of text by 10% of the containing block's width */ }
Explanation
- Length Values: Allow you to specify a fixed or relative amount of indentation for the first line of text. Positive values move the text to the right, while negative values move it to the left.
- Percentage: Provides a way to set indentation relative to the width of the containing block, which can be useful for responsive designs.
inherit
: Ensures that the indentation style is consistent with its parent element, maintaining uniformity in text presentation.
Use Cases
- Paragraph Formatting: Commonly used in paragraphs to create a visual separation of the first line, enhancing readability and following traditional formatting styles.
- Design Layouts: Useful in web design to create specific visual effects or to align text consistently across different sections of a webpage.
- Indentation for Lists: Can be used in conjunction with list styles to control the indentation of list items.