I am trying to center imginside the containing divwhere it imgfills (minimum) 100% of the width and height of the containing div, which means that the image is automatically scaled to maintain the image. It’s easy for me to compare this value imgwith the top, bottom, left, or right, but I hope to center imgboth vertically and horizontally. At the moment, I could not find a solution, so any help was highly appreciated.
HTML
<section id="hero-image">
<img src="https://s-media-cache-ak0.pinimg.com/originals/ae/1d/6e/ae1d6ef744320d237a95fc1e7d6ee98b.jpg">
</section>
CSS
#hero-image {
position: relative;
width: 100%;
height: 400px;
overflow: hidden;
background: red;
}
#hero-image img {
position: absolute;
min-height: 100%;
height: auto;
min-width: 100%;
width: auto;
margin: auto;
right: 0;
left: 0;
top: 0;
z-index: 0;
}
Fiddle
source
share