How to Vertically Align Text in a Div with CSS

Avatar

By squashlabs, Last Updated: October 6, 2023

How to Vertically Align Text in a Div with CSS

There are several ways to vertically align text in a div using CSS. Here are two possible approaches:

Approach 1: Using Flexbox

Flexbox is a useful CSS layout module that provides a simple and flexible way to vertically align elements. To vertically align text in a div using flexbox, follow these steps:

1. Create a container div that will hold the text and apply the following CSS properties to it:

.container {
  display: flex;
  align-items: center;
  justify-content: center;
}

2. Place the text inside the container div:

<div class="container">
  <p>Vertically aligned text</p>
</div>

Related Article: How to Center an Absolutely Positioned Element in a Div

Approach 2: Using Table Display

Another approach to vertically align text in a div is by using table display properties. Here’s how you can do it:

1. Create a container div and apply the following CSS properties to it:

.container {
  display: table;
}

2. Place the text inside a child div and apply the following CSS properties to it:

.child {
  display: table-cell;
  vertical-align: middle;
}

3. Place the child div inside the container div:

<div class="container">
  <div class="child">
    <p>Vertically aligned text</p>
  </div>
</div>

Best Practices

– Use flexbox when you need more flexibility in your layout, such as aligning multiple elements vertically.
– Use table display when you have a simple layout and need to vertically align a single element.

Related Article: How to Horizontally Center a Div Element in CSS

More Articles from the Cascading Style Sheets (CSS) Guide series:

How to Center a Div Vertically and Horizontally on a Page

Learn the simplest method to center a div both vertically and horizontally using CSS. This article covers different techniques such as Flexbox, Absolute Positioning, and... read more

How to Vertically Center Text with CSS

This article provides a guide on vertically centering text using CSS properties and techniques. You'll learn how to achieve this using flexbox, CSS grid, and alternative... read more

How to Center a Position Absolute Element in CSS

Centering an element with position absolute in CSS can be achieved using different techniques. Two commonly used methods are using the Transform Property and Flexbox. In... read more

How to Vertically Align Text Within A Div Using CSS

This guide provides steps on how to use CSS to vertically align text within a Div. The two chapters, "Flexbox" and "CSS Table Display," offer different methods to... read more

How to Right Align Div Elements in CSS

Setting Div elements to align right in CSS can be achieved using two different properties. The first method involves using the "float" property, while the second method... read more

How to Make a Div Vertically Scrollable Using CSS

Making a div vertically scrollable in CSS can be achieved through different methods. This article provides a guide on two solutions: using the overflow property and... read more