2 Different Ways to Center A Div In CSS And HTML

David Baiye
1 min readApr 7, 2022

--

A perfectly centered div.

There is a thousand-year-old question of how to align a div thee center both horizontally and vertically. In this article, I will give solutions to this age-old question.

The Flexbox method

The flexbox method is one of the most straightforward ways to center a div.

Imagine that you want to center a div in the center of your body, here is the code:

Remember that you have to create a link to your CSS code at the top.

Your CSS code should look like this:

And there you have it, The flexbox method of centering a div.

The Grid Method

This method is the easiest method for centering a div, this method uses the grid display property.

And these are the 2 most efficient methods for centering divs in CSS.

Follow me on Instagram at thekideveleoper, and on Twitter at thekideveloper.

I hope you’ve learned something new from this article, see you in the next one.

--

--