Home > Web Front-end > CSS Tutorial > How Can I Center Text Between Horizontal Rules?

How Can I Center Text Between Horizontal Rules?

Barbara Streisand
Release: 2024-12-22 14:28:09
Original
893 people have browsed it

How Can I Center Text Between Horizontal Rules?

Centering Text Within Horizontal Rules

To create horizontal lines that flank centered text, various solutions have been proposed, each with its own limitations.

One common approach involves using multiple <div> elements and floating them:

<div>
Copy after login

However, this approach can produce alignment issues. Similarly, using a table can result in misalignment:

<table><tr>
  <td>
Copy after login

A cleaner solution emerged with the introduction of Flexbox:

.separator {
  display: flex;
  align-items: center;
  text-align: center;
}

.separator::before,
.separator::after {
  content: '';
  flex: 1;
  border-bottom: 1px solid #000;
}

.separator:not(:empty)::before {
  margin-right: .25em;
}

.separator:not(:empty)::after {
  margin-left: .25em;
}
Copy after login
<div class="separator">Next section</div>
Copy after login

This approach provides precise alignment and eliminates the need for complex markup or "fudgy" solutions.

The above is the detailed content of How Can I Center Text Between Horizontal Rules?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template