Home > Web Front-end > CSS Tutorial > How to Center Text within a CSS-Created Circle?

How to Center Text within a CSS-Created Circle?

Mary-Kate Olsen
Release: 2024-12-04 06:48:14
Original
591 people have browsed it

How to Center Text within a CSS-Created Circle?

Modifying CSS Circle to Include Centered Text

You found an example using CSS alone to draw a circle. However, you now seek to modify it to include text in the center.

First, attempt this solution:

.circle {
  width: 500px;
  height: 500px;
  line-height: 500px;
  border-radius: 50%;
  font-size: 50px;
  color: #fff;
  text-align: center;
  background: #000;
}
Copy after login
<div class="circle">Hello I am A Circle</div>
Copy after login

Unfortunately, this may result in an oval shape instead of a circle.

The Solution

To vertically center the text, set the line-height to be the same value as the height of the div. In the example above, the height and line-height are both set to 500px.

.circle {
  width: 500px;
  height: 500px;
  line-height: 500px; /* Vertically center the text */
  border-radius: 50%;
  font-size: 50px;
  color: #fff;
  text-align: center;
  background: #000;
}
Copy after login

This modification ensures that the text is displayed centered within the circle.

The above is the detailed content of How to Center Text within a CSS-Created Circle?. 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