Skip to content

How to Center a Div in HTML: A Quick Guide

Updated: at 01:22 PM

We’ve all been there – feeling frustrated trying to center a div in HTML. It sounds easy, but the way you do it can be tricky, making us search on Google for answers. If you’re tired of that struggle, don’t worry! In this blog post, we’ll look at three easy ways to center a div in HTML, with examples for those using TailwindCSS. This trick is simpler than figuring out what went wrong in your relationship, and it will definitely work – unlike your relationship that isn’t working! 😂✌️✌️

Centering a Div in HTML

1. Using Transform

One straightforward method is utilizing the transform property in CSS. Here’s a simple example:

HTML

<div class="center-transform">
  <!-- Copy-paste contents here. I know! You are copy-pasting some code; I've been there 😂 -->
</div>

CSS

.center-transform {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

This method involves setting the position to absolute and then translating the div by 50% in both the horizontal and vertical directions.

More information about transform and translate:
https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function/translate

2. Using Flexbox

Flexbox is a powerful layout tool in CSS that makes centering a breeze. Check out this example:

HTML

<div class="center-flexbox">
  <!-- Copy paste contents here. -->
</div>

CSS

.center-flexbox {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;  /* This will be on full height of the screen you can change it according to your needs */
}

With display: flex;, you can easily center content both horizontally and vertically by setting justify-content and align-items to center.

More information about flex box layout:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flexible_box_layout/Basic_concepts_of_flexbox

3. Using Grid

CSS Grid is another excellent option for centering. Here’s a basic example:

HTML

<div class="center-grid">
  <!-- Copy paste contents here. -->
</div>

CSS

  .center-grid {
  display: grid;
  place-items: center;
  height: 100vh;  /* This will be on full height of the screen you can change it according to your needs */
}

The place-items: center; property does the trick, centering the content both horizontally and vertically.

More information about grid layout:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_grid_layout

Tailwind CSS Examples

Oh! I’m a fan of tailwindCSS and it was easy to implement. Here are some basic examples:

1. Using flex box layout

<div class="flex items-center justify-center h-screen">
  <!-- Copy paste contents here. -->
</div>

Much like the classic CSS approach in number 2, the flex class sets up a flex container, while items-center and justify-center ensure both vertical and horizontal alignment of content. The h-screen class maintains full-screen height for the container.

2. Using grid layout

<div class="grid place-items-center h-screen">
  <!-- Copy paste contents here. -->
</div>

Similar to the classic CSS approach in the number 3, the grid class sets up a grid container, and the place-items-center class places the content at the center of both the rows and columns. The h-screen class ensures the container takes up the full height of the screen.

If you want to learn more about Tailwind CSS, I don’t have content about that yet 😂😂😂, but later, if I’m not busy, I will add the basic steps on how to set up TailwindCSS. Is that okay with you? ✌️

For the meantime, you can follow this steps: https://tailwindcss.com/docs/installation

Final Thoughts

Centering div in HTML doesn’t have to be a puzzle. Whether you like the classic CSS moves or the coolness of TailwindCSS, these tricks will make it super easy. No more getting lost in Google! Now, here’s a tip: if you found this post because you were searching again, 😂😂😂 save it! Bookmark this page, so you always have it when you need it. Easy peasy!