Blog / Web
Center Anything: How to center a text with CSS
  • Aug 10, 2021
  • 96
  • 54

Centering HTML elements is an important aspect for anything involving designing with CSS. There are various methods of centering things, however, it also depends on the elements that you are working with. In this series, we will go over the matter of how to center different elements horizontally and vertically.


Advertisement
 


Here, we will start with a simple task of centering a text inside another HTML elements - a div. You can follow this series by the links to the next post provided at the end of this article.

1. How to center a text horizontally?

To center a text or a link horizontally, we need to use  text-align  property with the value  center :

HTML:

  <div class="outer-wrap">
    Hello, The World is Horizontally Centered!
  </div>

CSS:

  .outer-wrap {    
    text-align: center;
    border: 1px dashed black;
  }

Result:

Hello, The World is Horizontally Centered!


2. How to center a text vertically?

Center things vertically is more difficult than doing it horizontally. Below are 2 methods of centering a text or a paragraph vertically:

a. Using the line-height property

The important thing of this method is:

  • the height of an outer element must be a fixed height.
  • the line-height value has to be same as the height of the outer element.

HTML:

  <div class="outer-wrap">
    Hello, The World is Vertically Centered!
  </div>

CSS:

  .outer-wrap {
    height: 100px;
    line-height: 100px;
    border: 1px dashed black;
  }

Result:

Hello, The World is Vertically Centered!


Problem: Longer text causes multiple lines. Because the line-height is 100px which causes the total height of the text greater than the outer element. So, the second line is outside of the outer element.

Hello, The World is Vertically Centered! But there is a problem: This method only works for a single line of text. So if the text is too long and has to break the line, it doesn't work!



Solution: wrap the text inside another element (span or div or p) and add below css property:

HTML:

<div class="outer-wrap">
  <span class="inner-text">Hello, The World is Vertically Centered! The problem of a multiple-line text or a long text is solved in this solution! Let's see if it works with this loooooooooooong teeeeeexxxxttttttttt!
  </span>
</div>

CSS:

.outer-wrap {
  height: 100px;
  line-height: 100px;
  border: 1px dashed black;
}
.inner-text{
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
}

Result:

Hello, The World is Vertically Centered! The problem of a multiple-line text or a long text is solved in this solution! Let's see if it works with this loooooooooooong teeeeeexxxxttttttttt!


b. Using the flex box

HTML:

<div class="outer-wrap">
  <span>Hello, The World is Vertically Centered!</span>
</div>

CSS:

.outer-wrap {
  display: flex;
  align-items: center;
  height: 100px;
  border: 1px dashed black;
}

Result:

Hello, The World is Vertically Centered!


Advertisement
 


3. How to center a text horizontally and vertically?

So, we can combine the CSS properties above to make the text centered horizontally and vertically:

a. Using line-height property

HTML:

<div class="outer-wrap">
  <span class="inner-text">Hello, The World is Centered!
  </span>
</div>

CSS:

.outer-wrap {
  height: 100px;
  line-height: 100px;
  text-align: center;
  border: 1px dashed black;
}
.inner-text{
  display: inline-block;
  vertical-align: middle;
  line-height: normal;
}

Result

Hello, The World is Centered!  


b. Using flex box

HTML:

<div class="outer-wrap">
  <span>Hello, The World is Centered!</span>
</div>

CSS:

.outer-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100px;
  border: 1px dashed black;
}

Result:

Hello, The World is Centered!


Read more in this series:

  • Web:
    1. You are reading: Center Anything: How to center a text with CSS

If you have a Website or a Web API developed by using .Net Core and looking for a way to publish your applications, this post will explain how to do it using GoDaddy Windows Hosting.Note: at this mome ...

Search text in Stored Procedure in SQL SELECT DISTINCT o.name AS Object_Name, o.type_desc FROM sys.sql_modules m INNER JOIN sys.objects o ON m.object_id = o ...

Using cherry-pick to select specific commits for your Pull Request.1. Create a new branch based on the target of the Pull Requestgit branch cherry-branch origin/master2. Switch to a new branchgit chec ...

After deployment Angular and API on IIS, it's working fine unless I refresh the page. Once refreshed, the web encountered 404 error. In this article, I will explain how to resolve this.Since Angular i ...

There are some benefits of keeping both UI and API parts in the same place for small projects. In this article, I will explain how I did to deploy Angular Web and ASP .Net Core API in the same folder ...

I got CORS error after publishing my API and Angular app to IIS even though CORS is enabled and the origins of the Angular app is added. Below is how I resolved this issue.Just simple, make sure you s ...

In Object-Oriented Programming, S.O.L.I.D refers to the first five design principle for the purpose of making software designs more understandable, flexible, and maintainable. The principles was first ...

1. The Situation:Error Message:&nbsp;Pulse Secure Application failed to load Java. Please install correct JRE version.Description: This issue happens when I'm using a M1 Mac with a correct version of ...

Accelerated Mobile Pages (AMP)&nbsp;focuses on delivering static content from publishers as quickly as possible and possibly rewards early adopters with a boost in rank. Let's see how to implement it ...