How to Center Text in a Div Vertically and Horizontally?
Last Updated : 07 Oct, 2024
Centering text inside a <div> both vertically and horizontally is a common requirement. This can be done using various CSS techniques. Here, we’ll explore different approaches: using Flexbox, CSS Grid, Text Align, and the Transform property.
Below are the approaches to center the text vertically or horizontally:
Using Flexbox
Flexbox is a widely supported CSS layout module that allows easy alignment and distribution of space within a container. It provides a simple and effective way to center content.
- Set the parent container to display: flex; (This enables the use of a flexbox and turns the parent container into a flex container.)
- Use justify-content: center (to center the child element horizontally within the parent container.)
- Use align-items: center (to center the child element vertically within the parent container.)
Example: This example shows how to center text inside a div using the flexbox property of CSS.
HTML <!DOCTYPE html> <html lang="en"> <head> <title> Center text horizontally and vertically inside a div block </title> <style> body { text-align: center; } .container { height: 300px; width: 645px; display: flex; justify-content: center; align-items: center; border: 2px solid black; } h1 { color: green; } </style> </head> <body> <div class="container"> <h1>GeekforGeeks</h1> </div> </body> </html>
Output:

Using Grid
CSS grid is another layout system that makes it simple to create complex layouts. You can center content using the place-items property, which is a shorthand for justify-items (horizontal alignment) and align-items (vertical alignment).
- Set the parent container to display: grid. (This enables the use of a CSS grid and turns the parent container into a grid container.)
- Use the place-items: center property (to center the child element both horizontally and vertically within the grid cell. This property is a shorthand for both justify-items and align-items.)
Example: This example shows how to center text inside a div using the grid 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; display: grid; place-items: center; border: 2px solid black; } h1 { color: green; } </style> </head> <body> <div class="container"> <h1>GeekforGeeks</h1> </div> </body> </html>
Output:

Using Text Align
The text-align property can be used to horizontally center text inside a div. For vertical centering, we can use the line-height property, especially for single-line text, by setting it to the same height as the container.
- Set the parent container to text-align: center. This centers the child element horizontally within the parent container.
- Use line-height: <height> to center the child element vertically within the parent container. The value of <height> should be equal to the height of the parent container.
Example: This example shows how to center text inside a div using the text align 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; text-align: center; line-height: 400px; border: 2px solid black; } h1 { color: green; } </style> </head> <body> <div class="container"> <h1>GeekforGeeks</h1> </div> </body> </html>
Output:

The transform property offers another method for centering elements by using translate(-50%, -50%). This approach is particularly effective for absolute positioning.
- 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: