Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • HTML Tutorial
  • HTML Exercises
  • HTML Tags
  • HTML Attributes
  • Global Attributes
  • Event Attributes
  • HTML Interview Questions
  • HTML DOM
  • DOM Audio/Video
  • HTML 5
  • HTML Examples
  • Color Picker
  • A to Z Guide
  • HTML Formatter
Open In App
Next Article:
How to Center Text in a Div Vertically and Horizontally?
Next article icon

How to Center Text in a Div Vertically and Horizontally?

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

Table of Content

  • Using Flexbox
  • Using Grid
  • Using Text Align
  • Using Transform Property

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:

Using Transform Property

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:


Next Article
How to Center Text in a Div Vertically and Horizontally?

A

ankitjangidx
Improve
Article Tags :
  • HTML
  • CSS-Questions

Similar Reads

    How to Center an Image Vertically and Horizontally ?
    To center an image along X-axis i.e. Horizontally and along Y-axis i.e. vertically we can use different CSS properties. There are different ways to center an Image Vertically and Horizontally including CSS flexbox properties, absolute positioning, and CSS Grid System. Table of Content Using FlexboxU
    3 min read
    How to Center an Element Horizontally and Vertically ?
    We will learn how to center an element horizontally and vertically. Centering an element both horizontally and vertically is a common task in web development. It ensures that the element is always displayed at the center of the webpage, regardless of the screen size. Whether you’re aligning text, im
    5 min read
    How to make an image center-aligned (vertically & horizontally) inside a bigger div using CSS ?
    We will know how to center-align the image in a div tag using CSS & will also understand its implementation through the example. Given an image, we need to set the image that align to the center (vertically and horizontally) inside a bigger div. But first let's create a basic structure, in which
    2 min read
    How to Center a Button Element Vertically and Horizontally with CSS?
    Centering a button element both vertically and horizontally on a webpage is a common requirement in web design. It ensures that the button is always displayed at the centre of the webpage, regardless of the screen size. In this article, we will cover different approaches to centre a button element v
    2 min read
    How to Vertically Align Text Within a Div in CSS?
    Aligning text vertically within a div can enhance the visual appeal of your web design. There are various approaches to achieving vertical alignment using CSS.1. Using line-height PropertyThe line-height property is the easiest and most commonly used approach to align text within a div vertically. T
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences