How to center text horizontally and vertically inside a div block with CSS?

Spread the love

Sometimes, we want to center text horizontally and vertically inside a div block with CSS.

In this article, we’ll look at how to center text horizontally and vertically inside a div block with CSS.

How to center text horizontally and vertically inside a div block with CSS?

To center text horizontally and vertically inside a div block with CSS, we use flexbox.

For instance, we write

#container {
  display: flex;
  justify-content: center;
  align-items: center;
}

to make the element with ID container a flex container with display: flex;.

Then we center the items inside horizontally with justify-content: center;.

And then we center the items inside vertically with align-items: center;.

Conclusion

To center text horizontally and vertically inside a div block with CSS, we use flexbox.

Leave a Reply

Your email address will not be published. Required fields are marked *