How to horizontally center a fixed div with CSS?

Spread the love

Sometimes, we want to horizontally center a fixed div with CSS.

In this article, we’ll look at how to horizontally center a fixed div with CSS.

How to horizontally center a fixed div with CSS?

To horizontally center a fixed div with CSS, we use translate.

For instance, we write

div {
  //...
  left: 50%;
  transform: translateX(-50%);
}

to set the left position to 50%.

Then we make the div centered with transform: translateX(-50%);.

Conclusion

To horizontally center a fixed div with CSS, we use translate.

Leave a Reply

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