KC Here KC Here - 4 months ago 10
CSS Question

When my div is not getting horizontally centered?

Why is the image not centered here?



#image {
margin: 0 auto;
}

<div id="image">
<img src="https://placekitten.com/200/300">
</div>





But it works perfect with a
<center>
tag.

My fiddle

Answer

Or you could just use flexbox

#image {
  height: 90vh;
  width: 90vw;
  display: flex;
  justify-content: center;
  align-items: center;
}

You don't need the 90vh or 90vw but you do need to give the height and width some value. Whether it's in px or % is up to you.