How to Vertically Align Text in a Div with CSS

Avatar

By squashlabs, Last Updated: Oct. 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 Vertically Align Text Within A Div Using CSS

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

Related Article: How to Horizontally Center a Div in CSS

- 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.

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

How To Change SVG Element Color

Learn how to change the color of an SVG element using CSS in this simple tutorial. Discover two methods: using the CSS fill property or applying a CS… read more

CSS Position Relative: A Complete Guide

This tutorial is a comprehensive resource that dives deep into the fundamentals, practical examples, real-world applications, and advanced techniques… 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 g… read more

How to Use the CSS Parent Selector

Using the CSS parent selector allows you to target specific elements based on their parent's properties, offering more control over your styling. Thi… read more

How to Implement a Beating Heart Loader in Pure CSS

The code below generates a beating heart that can be used as a CSS loader. Use it in web pages and web apps to add a visually appealing loading anima… read more

How to Set the Transparent CSS Background Color

Setting a transparent background color in CSS is a simple process that can enhance the visual appeal of your website. This article provides a guide o… read more

How to Use CSS Hex Code Colors with Alpha Values

Learn how to incorporate transparency in CSS hex code colors for stunning visual effects. This article provides a comprehensive guide on using alpha … read more

How to Use Media Queries for Desktop, Tablet, and Mobile

Guide on constructing CSS media queries to target various devices. This article provides step-by-step instructions on understanding media queries and… read more

CSS Padding: An Advanced Guide

Dive into advanced techniques for perfect spacing and visually appealing layouts with CSS padding. This comprehensive guide covers lesser-known topic… read more

How to Style CSS Scrollbars

Learn how to customize scrollbars using CSS in this tutorial. Discover different use cases and best practices for styling scrollbars, ensuring consis… read more