Text Properties (text-align, text-decoration, etc.)
In web design, text serves not only as a carrier of information but also as a critical component of visual rhythm. The text-align property acts as a core tool for controlling horizontal text alignment, significantly enhancing both readability and aesthetics. It applies not only to paragraph text but can also precisely target content within list items, code blocks, and table cells. When we need to break away from default left or center alignment modes—to achieve right alignment, justified alignment, or content-based flexible layouts—a deep understanding of this property's behavior mechanisms is crucial.
The text-align property accepts values such as left, right, center, and justify. Notably, for list elements (such as <ul> and <ol>), this property allows the use of match-parent or auto to inherit the alignment style from parent elements, enabling complex typographic effects. In CSS3, we also introduced the text-align-last property, which is specifically designed to control the alignment behavior of the last line; this is particularly effective for handling line breaks at the end of long text paragraphs.
In practical development, the most common requirement is adjusting the alignment of specific blocks. For instance, in a news list, we might want titles to be centered while body text is right-aligned to emphasize importance; or in form design, aligning placeholder text within input fields to center it for better visual balance.
.news-item {
display: flex;
align-items: baseline;
}
.news-item h2 {
text-align: center;
margin-bottom: 0.5rem;
}
.news-item p {
text-align: right;
font-size: 0.9rem;
color: #666;
}
The code above demonstrates how combining text-align with Flexbox layout can achieve centered titles and right-aligned body text. This fine-grained control often yields unexpected visual impact, making information hierarchy more distinct.
Text Decoration and Underline Styles
Beyond positioning, the appearance of text cannot be overlooked. The text-decoration property is the standard solution for handling underlines, strikethroughs, and other decorative effects, completely replacing the non-standard usage of the <u> tag in early HTML. This property supports values such as none, underline, overline, line-through, and blink (now deprecated), allowing developers to flexibly choose based on semantic needs.
text-decoration-style provides finer-grained control, permitting specification of line thickness types such as solid, dotted, double, or wavy. Paired with text-decoration-color and text-decoration-width, we can achieve highly customized decorative effects—for example, adding a red wavy underline to links or applying double underlines to specific text to denote quotes.
.link-highlight {
text-decoration: underline;
text-decoration-style: double;
text-decoration-color: #007bff;
text-underline-offset: 3px;
}
.deleted-text {
text-decoration: line-through;
text-decoration-color: #999;
opacity: 0.8;
}
By introducing the text-underline-offset property, we can fine-tune the distance between the underline and the text baseline, preventing the underline from obscuring text content. This is highly practical for scenarios requiring emphasis without compromising the reading experience. Furthermore, modern browsers have increasingly mature support for text-decoration-thickness, allowing us to directly define line width in pixels, further eliminating the limitation of relying on font weight to simulate line thickness.
Text Spacing and Line Height Optimization
Good typography relies not only on alignment but also on reasonable spatial distribution. The line-height property is a key parameter for controlling line height; it determines the vertical distance between adjacent baselines. By default, browsers automatically calculate an appropriate line height based on font size. However, when precise control over the reading experience is required, manually setting this value becomes particularly important.
It is generally recommended to set line-height to a multiple of 1.2 to 1.6 times the font size to balance compactness with readability. For long text content, a larger line height effectively reduces visual fatigue; for short code snippets or titles, a smaller line height saves vertical space. Besides numerical settings, using keywords like normal or tighter can also quickly adjust the overall style.
.readable-paragraph {
font-size: 16px;
line-height: 1.5; /* Equivalent to 24px */
}
.compact-code {
font-family: 'Courier New', monospace;
line-height: 1.2;
white-space: pre-wrap;
}
Besides line height, letter-spacing and word-spacing also affect the horizontal breathing room of text. The former controls character spacing, while the latter controls word spacing. In English typesetting, appropriately increasing word-spacing can prevent words from sticking together; in Chinese typesetting, since there are no spaces between characters, line-height is primarily relied upon to adjust density, but using letter-spacing remains effective for processing special symbols or number combinations.
Comprehensive Application Scenarios
In actual projects, we often need to combine the aforementioned properties to achieve the best visual effect. Below is a complete card layout example demonstrating how unified text style specifications can enhance the overall quality of the page.
.card-content {
padding: 1.5rem;
}
.card-title {
text-align: center;
font-size: 1.25rem;
margin-bottom: 0.75rem;
letter-spacing: 0.05em;
}
.card-body {
line-height: 1.6;
color: #333;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 1rem;
padding-top: 0.5rem;
border-top: 1px solid #eee;
}
.action-link {
text-decoration: underline;
text-decoration-color: #0066cc;
text-underline-offset: 2px;
font-size: 0.875rem;
}
.tag {
display: inline-block;
padding: 0.125rem 0.5rem;
background-color: #f0f0f0;
border-radius: 4px;
font-size: 0.75rem;
text-decoration: none;
}
In this example, we not only unified the title alignment and letter spacing but also optimized the reading experience of the body text by setting the line height. The bottom action area utilizes Flexbox layout combined with the localized application of text-align to achieve perfect layout for links and tags. This systematic approach to style management helps teams maintain visual consistency in large projects, reducing cognitive load caused by inconsistent element styles.
Mastering fine-grained control over text properties is a fundamental skill for improving frontend page quality. Whether it's simple alignment adjustments or complex decorative effects, the reasonable application of these CSS characteristics can make web content more vivid, professional, and easy to read.