HTML : Centering a div of unknown height and width

Rajesh Kumar Yadav - May 17 '21 - - Dev Community

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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .