Keywords: CSS horizontal line | centered text | pseudo-elements | Flexbox | web design
Abstract: This article provides an in-depth exploration of various CSS methods for creating horizontal lines with centered text. It focuses on traditional approaches using border-bottom and background colors, while also covering modern techniques employing pseudo-elements and Flexbox. The content includes detailed explanations of each method's principles, advantages, disadvantages, and browser compatibility, complete with comprehensive code examples and implementation details to help developers choose the most suitable solution for their specific needs.
Introduction
In web design, creating horizontal lines with centered text is a common visual requirement. This design element is typically used for title separation, section division, or highlighting important content. Traditional implementation methods rely on character repetition (such as hyphens), but this approach lacks flexibility and aesthetic appeal. Modern CSS techniques provide more elegant and controllable solutions.
Traditional Implementation Using Border-Bottom
This method utilizes element border properties and background color overlay techniques, representing one of the earliest widely adopted solutions. The core concept involves setting a bottom border on the container element, then using the background color of inline elements to "break" this line.
Implementation Principles
First, set the title element's width to 100% to ensure it occupies the entire available space. Create the horizontal line effect using border-bottom: 1px solid #000;. The line-height: 0.1em; setting is crucial as it compresses the element's height, bringing the border line closer to the text.
In the nested span element, set background: #fff; to match the page background color, combined with padding: 0 10px; to create blank areas on both sides of the text, thereby achieving the visual effect of text "breaking" the horizontal line.
Code Implementation
<style>
h2 {
width: 100%;
text-align: center;
border-bottom: 1px solid #000;
line-height: 0.1em;
margin: 10px 0 20px;
}
h2 span {
background: #fff;
padding: 0 10px;
}
</style>
<h2><span>THIS IS A TEST</span></h2>
<p>this is some content other</p>
Advantages and Disadvantages Analysis
Advantages:
- Simple implementation with minimal code
- Excellent compatibility with all modern browsers and most legacy versions
- No additional HTML markup required
Disadvantages:
- Dependent on fixed background colors, performs poorly with complex or gradient backgrounds
line-heightadjustments may affect text vertical alignment- Poor adaptability in responsive design scenarios
Modern Implementation Using Pseudo-Elements
With the advancement of CSS pseudo-element technology, more flexible implementation methods have emerged. This approach utilizes ::before and ::after pseudo-elements to create horizontal lines without relying on background color overlays.
Implementation Principles
Set the title element with overflow: hidden and text-align: center to ensure proper content alignment. Use ::before and ::after pseudo-elements to create horizontal lines on the left and right sides respectively.
Key property settings include:
display: inline-blockenables proper display of pseudo-elementswidth: 50%ensures horizontal lines occupy appropriate widthposition: relativeandvertical-align: middleguarantee vertical alignment- Adjust horizontal line position using
margin-left: -50%andmargin-right: -50%
Code Implementation
<style>
h1 {
overflow: hidden;
text-align: center;
}
h1::before,
h1::after {
background-color: #000;
content: "";
display: inline-block;
height: 1px;
position: relative;
vertical-align: middle;
width: 50%;
}
h1::before {
right: 0.5em;
margin-left: -50%;
}
h1::after {
left: 0.5em;
margin-right: -50%;
}
</style>
<h1>Heading</h1>
<h1>This is a longer heading</h1>
Responsive Solution Using Flexbox
The Flexbox layout model provides a more modern and responsive solution for creating horizontal lines with centered text. This method is particularly suitable for modern web design that needs to adapt to different screen sizes.
Implementation Principles
Set the title element as a Flex container, ensuring child elements are arranged horizontally through flex-direction: row. The ::before and ::after pseudo-elements use the flex: 1 1 property to automatically fill available space, creating flexible horizontal line effects.
border-bottom: 1px solid; creates the actual line on pseudo-elements, while margin: auto ensures the line is properly centered within the container. Adjusting margin-left and margin-right controls the spacing between text and lines.
Code Implementation
<style>
h1 {
display: flex;
flex-direction: row;
}
h1:before, h1:after {
content: "";
flex: 1 1;
border-bottom: 1px solid;
margin: auto;
}
h1:before {
margin-right: 10px;
}
h1:after {
margin-left: 10px;
}
</style>
<h1>Today</h1>
Technical Comparison and Selection Recommendations
Browser Compatibility Analysis
Traditional border-bottom method: Best compatibility, supports IE6+ and all modern browsers
Pseudo-element method: Supports IE8+ and all modern browsers, requires single-colon syntax in IE8
Flexbox method: Supports IE10+ and all modern browsers, represents the most modern solution
Performance Considerations
The three methods show minimal performance differences, but the Flexbox method may offer better rendering performance in complex layouts. The traditional method, using background color overlays, may have slight advantages in repaint and reflow scenarios.
Recommended Application Scenarios
- Simple projects: Recommend traditional border-bottom method for concise code and good compatibility
- Complex backgrounds: Recommend pseudo-element method, independent of background color overlays
- Responsive design: Recommend Flexbox method for strong adaptability and easy maintenance
- Legacy browser support: Must use traditional border-bottom method
Advanced Applications and Extensions
Custom Line Styles
All methods support custom line styles including color, thickness, and dashed lines:
/* Dashed line style */
border-bottom: 1px dashed #ccc;
/* Gradient line */
border-bottom: 2px solid;
border-image: linear-gradient(to right, transparent, #000, transparent) 1;
/* Double line */
border-bottom: 3px double #000;
Animation Effects
Combine with CSS animations to add dynamic effects to horizontal lines:
@keyframes lineExpand {
from { width: 0%; }
to { width: 50%; }
}
h1::before,
h1::after {
animation: lineExpand 1s ease-out;
}
Best Practices Summary
When implementing horizontal lines with centered text, follow these best practices:
- Semantic HTML: Use appropriate heading tags (
<h1>to<h6>) rather than generic<div>elements - Progressive enhancement: Provide fallback solutions for browsers that don't support modern CSS features
- Accessibility: Ensure horizontal lines don't interfere with screen reader usage
- Responsive design: Test effects across different screen sizes, use media queries when necessary
- Performance optimization: Avoid unnecessary repaints and reflows, use CSS properties judiciously
By deeply understanding the principles and application scenarios of these CSS techniques, developers can choose the most appropriate implementation method for their project requirements, creating both aesthetically pleasing and functionally complete horizontal line effects with centered text.