How to center align a fixed position div with CSS?

Spread the love

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

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

How to center align a fixed position div with CSS?

To center align a fixed position div with CSS, we set the transform property.

For instance, we write

.centered {
  position: fixed;
  left: 50%;
  transform: translate(-50%, 0);
}

to add left: 50%; and transform: translate(-50%, 0); to center the element with class centered horizontally.

Conclusion

To center align a fixed position div with CSS, we set the transform property.

Leave a Reply

Your email address will not be published.