How to center text in a div

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: To center text within a div, you can use CSS. The most common and flexible methods involve `text-align: center;` for horizontal centering or using Flexbox properties like `display: flex;`, `justify-content: center;`, and `align-items: center;` for both horizontal and vertical centering.

Key Facts

Overview

Centering text within a div is a fundamental task in web development, crucial for creating visually appealing and user-friendly layouts. Whether you need to center a single word, a paragraph, or even an entire block of content both horizontally and vertically, CSS provides several effective methods. Understanding these techniques allows developers to precisely control the alignment of text elements, enhancing the overall design and readability of web pages.

Common Methods for Centering Text

1. Using `text-align: center;` (Horizontal Centering)

The most straightforward way to horizontally center text within a div is by applying the `text-align` property to the parent div. This property affects inline or inline-block level children, including text nodes.

How it works: When you set `text-align: center;` on a div, all inline content within that div, such as text, images, or inline-block elements, will be centered relative to the div's boundaries.

Example:

.center-text {text-align: center;}

<div class="center-text">This text will be centered horizontally.</div>

Limitations: This method only centers content horizontally. It does not affect block-level elements themselves or provide vertical centering.

2. Using Flexbox (Horizontal and Vertical Centering)

Flexbox is a powerful layout module in CSS that offers a more comprehensive solution for alignment, including both horizontal and vertical centering. It's ideal for centering a single item or a group of items within a container.

How it works: To use Flexbox for centering, you first set the parent div to `display: flex;`. Then, you use `justify-content: center;` to align items along the main axis (horizontally by default) and `align-items: center;` to align items along the cross axis (vertically by default).

Example:

.center-flex {display: flex;justify-content: center; /* Horizontally center */align-items: center; /* Vertically center */height: 200px; /* Required for vertical centering */border: 1px solid black;}

<div class="center-flex">This text is centered both ways.</div>

Note: For `align-items: center;` to work effectively for vertical centering, the parent container must have a defined height (or a height that allows it to expand vertically).

3. Using CSS Grid (Horizontal and Vertical Centering)

CSS Grid Layout is another modern and powerful layout system that excels at creating complex grid-based layouts. It also provides simple ways to center content.

How it works: Similar to Flexbox, you set the parent div to `display: grid;`. Then, you can use `place-items: center;` as a shorthand for both `justify-items` and `align-items`, effectively centering the content within the grid cell(s).

Example:

.center-grid {display: grid;place-items: center; /* Centers both horizontally and vertically */height: 200px; /* Required for vertical centering */border: 1px solid blue;}

<div class="center-grid">Centered with Grid.</div>

Alternative Grid Method: You can also use `justify-content: center;` and `align-items: center;` if you are dealing with the alignment of the grid *items* within the grid container itself, rather than the alignment of content within a grid *item*.

4. Using `line-height` (Vertical Centering for Single Lines)

For single lines of text, a common technique is to set the `line-height` property of the text equal to the `height` of its container. This pushes the text down or up so it appears vertically centered.

How it works: When the line height matches the container height, the text naturally sits in the middle. This is a simple trick but works best for single lines of text.

Example:

.center-single-line {height: 50px;line-height: 50px; /* Equal to the height */text-align: center; /* For horizontal centering */border: 1px solid green;}

<div class="center-single-line">Single line centered.</div>

Limitations: This method breaks down with multiple lines of text, as the `line-height` will be applied to each line, causing them to spread out unnaturally.

5. Using Absolute Positioning and Transform

This is a more traditional method that can be used for centering, especially when Flexbox or Grid might not be suitable or available.

How it works: Set the parent div to `position: relative;`. Then, set the child element (or the text content itself if treated as a block) to `position: absolute;`, `top: 50%;`, `left: 50%;`, and `transform: translate(-50%, -50%);`. The `translate` function pulls the element back by half of its own width and height, centering it precisely.

Example:

.parent-relative {position: relative;height: 200px;border: 1px solid red;}.child-absolute-center {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);text-align: center; /* Centers text within the absolute element */}

<div class="parent-relative"><div class="child-absolute-center">Centered via absolute positioning.</div></div>

Considerations: This method takes the element out of the normal document flow, which might affect the layout of other elements.

Choosing the Right Method

The best method for centering text depends on your specific needs:

Modern CSS practices strongly favor Flexbox and Grid for layout and alignment tasks due to their flexibility, power, and semantic clarity.

Sources

  1. text-align - CSS: Cascading Style Sheets | MDNCC-BY-SA-2.5
  2. Aligning Items in a Flex Container - CSS: Cascading Style Sheets | MDNCC-BY-SA-2.5
  3. place-items - CSS: Cascading Style Sheets | MDNCC-BY-SA-2.5

Missing an answer?

Suggest a question and we'll generate an answer for it.