Cascading Style Sheets (CSS) provide powerful formatting capabilities for web content. As a web developer, mastering text formatting in CSS unlocks new levels of typographic control and visual appeal for your sites and applications. One lesser-known but extremely useful text formatting technique is indenting the second line of text while leaving the first line flush left.
Let‘s dive into the nitty-gritty details of indenting the second line in CSS.
Why Indent the Second Line?
Before we look at the CSS properties and syntax needed, you may be wondering—why indent the second line rather than the first? Here are some great reasons to indent the second line of text:
- Creates a distinctive visual style that grabs the user‘s attention
- Highlights the start of new paragraphs by making the first line stand out
- Improves aesthetics and typography for wider columns of text
- Provides hierarchical emphasis after headings and subheadings
- Directs the user‘s eyes down the page in a left-aligned flow
Indented second lines create movement down the page while maintaining a clean left edge that is easy to scan visually. This fits with best practices for readable web typography.
The text-indent Property Explained
The key property that enables indenting the second line is text-indent in CSS. This property specifies the indentation, or empty space to leave before the first character, of a text block.
Here is the syntax:
text-indent: <length> | <percentage> | inherit;
The value defines the indentation space:
- length: Indents the text by a fixed pixel, em, rem or other length unit amount
- percentage: Indents the text by a percentage of the parent element‘s width
- inherit: Inherits the indent value from the parent element
Negative values are allowed for text-indent. This pulls the text to the left rather than indenting right.
By default, text-indent indents the first line of text. But by using a negative indent, we can indent the second line instead!
Indenting the Second Line in Practice
Let‘s walk through a full example to see second line indents in action.
We‘ll use HTML and CSS like this:
<p class="indent">
This paragraph will have the second line indented. Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
.indent {
text-indent: -1.5em;
padding-left: 1.5em;
}
Breaking this down:
- The
<p>element contains our text - We add a
indentclass to apply the indenting - In our CSS, we target
.indentto affect any paragraphs with that class - A negative
text-indentof-1.5empulls the first line 1.5 ems left padding-left: 1.5emindents the second line by the same amount
This approach works by balancing the negative text indent on the first line with equal padding on the container. The result is the second line and subsequent lines indent while the first remains flush.
And here is the output:
The effect is subtle yet eye-catching. The first line grabs the user‘s attention while the indented second line leads into the paragraph body.
Indenting Multiple Lines
We can build on this technique to indent additional lines too.
For example, to indent the 2nd and 3rd lines, we chain multiple box shadows like this:
.indent-multiple {
text-indent: -1.5em;
padding-left: 1.5em;
box-shadow:
inset 1.5em 0 0 0 white,
inset 3em 0 0 0 white;
}
Here‘s what‘s happening:
text-indentandpadding-leftindent the second line as before- Two
box-shadowsadd additional indents for the 3rd and 4th lines - The
insetkeyword makes the shadows display inside the element - The shadows "mask" portions of the background white to displace the text
Chaining box shadows like this can indent numerous lines with minimal code.
Browser Support
Indented second lines work well across all modern browsers. Safari, Chrome, Firefox, Edge all support the techniques shown.
For IE11 and other legacy browsers, avoid using relative em units. Use fixed px values instead as older IE versions miscalculate indent sizes with relative units.
You also need to account for the padding space when setting fixed indent widths. For example:
.indent-legacy {
padding-left: 50px; /* match fixed indent size */
text-indent: -50px;
}
This ensures consistent indentation width across browsers.
Potential Drawbacks
Indenting lines with CSS does come with a few drawbacks to note:
- More HTML markup: Requires extra
classattributes and CSS compared to default indentation - Fluid layout limitations: Percentage-based indents may not adapt well to fully fluid layouts
- First line visibility: For very wide columns, a negative indent could pull the first line offscreen
Test your designs thoroughly across various viewport widths. And favor length units like em for most flexible results.
Creative Uses for Indented Lines
With the essential techniques covered, let‘s explore some creative examples and advanced use cases for indented lines using CSS.
Indented Drop Caps
We can indent lines to create a prominent, styled first letter like this:
p:first-letter {
float: left;
font-size: 4em;
line-height: 1;
padding-top: 0.1em;
padding-right: 0.1em;
}
p {
text-indent: -2em;
padding-left: 3em;
}
This styles a large, capital drop cap letter while indenting the second line neatly below. The effect looks gorgeous for articles and blog content.
Staircase Indents
Alternating indented lines create a "staircase" text effect. This adds intrigue while leading the eye down and across the text.
The effect uses two rules, one to indent even lines, the other for odd:
p:nth-of-type(2n) {
text-indent: -1em;
padding-left: 1em;
}
p:nth-of-type(2n+1) {
text-indent: 1em;
padding-left: -1em;
}
Pro Tip: For extra styling, add a background color to the indents only:
p:nth-of-type(2n) {
box-shadow: inset 2em 0 0 #fee991;
}
The possibilities here get creative!
Indented Image Captions
Text indents apply just as readily to image captions as paragraphs:
img + em {
display: block;
text-indent: -2em;
padding: 0 0 0 2em;
}
This indents the italicized <em> caption following each image. Photos get a distinctive caption style.
Again, the flexibility of CSS text-indent opens up some stylish options.
Code Block Indents
Code blocks are another use case for indents that greatly enhances readability:
Say we have a <pre> container to display monospaced code. We could then indent all inner lines except the first like so:
pre {
text-indent: -1.3em;
padding-left: 1.3em;
}
pre span:first-of-type {
text-indent: 0;
display: inline-block;
}
This leaves the opening <span> tag flush left while subsequent lines indent. The result beautifully showcases code samples.
Indenting Lines Responsively
One final technique brings extra dimension to text indents—responsive values.
Using viewport units, we can scale indent widths seamlessly across device sizes.
p {
text-indent: -3vw;
padding-left: 3vw;
}
Now the indent depth adapts 3% of the viewport width. Narrow mobiles show subtle indents while wide monitors get deeper indentation. Content flexes beautifully.
Responsively sized indents take text formatting to the next level.
Conclusion
Text indent offers creative new formatting approaches for web content. By indenting the second line and beyond, web designers gain typographic styles that attract users and enhance UX design.
Yet many developers overlook CSS indents limited to first lines only. By understanding properties like negative text-indent and box shadows, web crafters can step outside conventions with indented lines.
So try out indented lines for sleek paragraphs, highlighted intros, stylized images and code samples. The examples here only start to tap the potential. With thoughtful styling choices and the fundamentals covered here, indented lines stand ready to unlock new dimensions for your content.


