What You'll Learn
You'll learn how to indent the first line of a paragraph and control what happens when text is too long to fit in its container.
Text Indent: Creating Print-Style Indentation
The text-indent property pushes the first line of a block of text to the right, just like the indent you'd see at the start of a book paragraph. It only affects the first line, leaving the rest of the text aligned normally.
p {
text-indent: 2em; /* Indents first line by 2em */
}
.article {
text-indent: 30px; /* Or use absolute units */
}
.negative {
text-indent: -20px; /* Creates a hanging indent */
}
You can use any length unit you've learned (px, em, rem, %) and even negative values to create hanging indents where the first line sticks out to the left.
Text Overflow: Handling Long Text
When text overflows its container, text-overflow controls how it's displayed—but only works with three conditions met: the element must have overflow: hidden, white-space: nowrap, and a fixed width.
.truncate {
width: 200px;
white-space: nowrap; /* Prevents wrapping */
overflow: hidden; /* Hides overflow */
text-overflow: ellipsis; /* Shows ... */
}
The most common value is ellipsis, which adds "..." to indicate there's more text. Without these three properties working together, text-overflow won't do anything visible.
Think of it like a bookshelf with limited space: text-indent is like starting each book a few inches from the edge, while text-overflow: ellipsis is like putting "..." on the spine when the title is too long to fit.
Key Takeaway: Use text-indent to indent first lines of paragraphs, and combine text-overflow: ellipsis with overflow: hidden and white-space: nowrap to gracefully truncate long text with an ellipsis.