How to center fixed div with dynamic width with CSS?

Spread the love

To center fixed div with dynamic width with CSS, we set the margin.

For instance, we write

#example {
  position: fixed;
  right: 0;
  left: 0;
  margin-right: auto;
  margin-left: auto;
  min-height: 10em;
  width: 90%;
}

to set the margin-left and margin-right properties to auto and set left and right to 0 to center the fixed position element.

We set the width and min-height to set the dimensions of it.

Leave a Reply

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