Centering text using Transform Property

  • Set the parent container to position: relative. This enables the use of absolute positioning for the child element.
  • Set the child element to position: absolute. This enables the use of absolute positioning for the child element.
  • Set the child element’s top and left properties to 50%. This positions the child element at the center of the parent container.
  • Use transform: translate(-50%, -50%) to center the child element both horizontally and vertically.

Example: This example shows how to center text inside a div using the transform property of CSS.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        Center text horizontally and
        vertically inside a div block
    </title>
    <style>
        .container {
            height: 300px;
            width: 645px;
            position: relative;
            border: 2px solid black;
        }

        h1 {
            position: absolute;
            top: 50%;
            color: green;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>GeekforGeeks</h1>
    </div>
</body>

</html>

Output:

How to Center Text in a Div Vertically and Horizontally

Centering text within a <div> block is essential for creating visually attractive layouts. Various methods, such as flexbox, grid, and CSS transforms, offer solutions with distinct benefits. In this article, we’ll explore various methods to center text both horizontally and vertically using CSS. From classic approaches to modern flexbox solutions, 

Table of Content

  • 1. Centering text using Flexbox
  • 2. Centering text using Grid
  • 3. Centering text using Text Align
  • 4. Centering text using Table Cell
  • 5. Centering text using Transform Property

Similar Reads

1. Centering text using Flexbox

Flexbox provides a powerful and flexible way to center elements/text. It’s widely supported by modern browsers....

2. Centering text using Grid

CSS grid is another popular layout tool that enables you to create complex and flexible grid-based layouts....

3. Centering text using Text Align

The text-align property is a simple and straightforward way to center text horizontally within a div block. For single-line text or images, you can use the following CSS properties:...

4. Centering text using Table Cell

Set the parent container to display: table. This emulates the behavior of a table.Set the child element to display: table-cell. This emulates the behavior of a table cell.Use vertical-align: middle to center the child element vertically within the parent container.Use text-align: center to center the child element horizontally within the parent container....

5. Centering text using Transform Property

Set the parent container to position: relative. This enables the use of absolute positioning for the child element.Set the child element to position: absolute. This enables the use of absolute positioning for the child element.Set the child element’s top and left properties to 50%. This positions the child element at the center of the parent container.Use transform: translate(-50%, -50%) to center the child element both horizontally and vertically....

Choosing the Right Method

The best method for you depends on your project’s specific needs and complexity. Here’s a quick guide:...