HTML : Centering a div of unknown height and width

rajeshkumaryadavdotcom

Rajesh Kumar Yadav

Posted on May 17, 2021

HTML : Centering a div of unknown height and width

Let's create a div with some dummy content as -

<div class="content">This works with any content</div>
Enter fullscreen mode Exit fullscreen mode

Now let's apply some css to allow this content to be in center:

.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
Enter fullscreen mode Exit fullscreen mode

I know approx 10-15 ways to center html content, if you're interest then please comment below, I'll write an article on this topic as well.

Buy Me A Coffee

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.

💖 💪 🙅 🚩
rajeshkumaryadavdotcom
Rajesh Kumar Yadav

Posted on May 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related