• Home
    Blog
    Poetry
    About
    Contact
  • Get Started

How to Center a Div in HTML: A Beginner's Guide

avatar

Gautam Patoliya

@mr_gattu_07

8 min Read

July 15, 2024


4 Claps

Frontend Development
Web Development

Learn all the methods to center a div in HTML with this easy-to-understand guide. Perfect for beginners, this tutorial covers various techniques to achieve perfect centering of div elements using CSS.

Centering a div in HTML is a common task, but it can be tricky if you're just starting out. There are multiple ways to achieve this, each suitable for different situations. Let's explore the most common methods to center a div both horizontally and vertically.


1. Centering with text-align (Horizontal Centering Only)


This method works when the div is an inline-block or inline element.


CSS:

.parent {
  text-align: center;
}
.child {
  display: inline-block;
  width: 50%;
}


HTML:

<div class="parent">
  <div class="child">Centered div</div>
</div>


2. Centering with margin: auto (Horizontal Centering Only)


This technique centers block-level elements.


CSS:

.child {
  width: 50%;
  margin: 0 auto;
}


HTML:

<div class="child">Centered div</div>


3. Centering with flexbox (Horizontal and Vertical Centering)


Flexbox is a modern and powerful layout module.


CSS:

.parent {
  display: flex;
  justify-content: center; /* Horizontal centering */
  align-items: center; /* Vertical centering */
  height: 100vh; /* Full viewport height */
}
.child {
  width: 50%;
}


HTML:

<div class="parent">
  <div class="child">Centered div</div>
</div>


4. Centering with grid (Horizontal and Vertical Centering)


CSS Grid is another modern layout system that can center elements easily.


CSS:

.parent {
  display: grid;
  place-items: center; /* Centers both horizontally and vertically */
  height: 100vh;
}
.child {
  width: 50%;
}


HTML:

<div class="parent">
  <div class="child">Centered div</div>
</div>


5. Centering with position and transform (Horizontal and Vertical Centering)


This method uses absolute positioning and CSS transforms.


CSS:

.parent {
  position: relative;
  height: 100vh;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 50%;
}


HTML:

<div class="parent">
  <div class="child">Centered div</div>
</div>


Each method has its own use cases and advantages. Whether you're building a simple webpage or a complex web application, knowing these techniques will help you create well-centered layouts with ease.


Tags:
HTML
CSS
Solution
How to
Question
center div in HTML
CSS centering techniques
HTML layout
flexbox centering
grid centering
beginner HTML guide
web design tips
responsive design

Comments

(1)


LINKS

  • Home
  • Blog
  • Pricing
  • About
  • Contact

SOCIAL LINKS

  • Email
  • Instagram
  • LinkedIn

LEGAL

  • Terms and Conditions
  • Privacy Policy

Copyright © 2025 iThoughtSpace. All rights reserved.

avatar

jay harpal

・ 10mo

HII GAUTAM 👏🏻✨❤️