How to center align an absolutely positioned div with CSS?

Spread the love

To center align an absolutely positioned div with CSS, we set the left property.

For instance, we write

div#thing {
  position: absolute;
  top: 0px;
  z-index: 2;
  width: 400px;
  margin-left: -200px;
  left: 50%;
}

to set left to 50% to move the div to the center of the page horizontally.

Leave a Reply

Your email address will not be published.