0
I thought the problem was simple, but I can’t in any way center an image that’s inside a div
. I have images of various sizes and would like to center them horizontally, but I can’t use the property margin: 0 auto;
. My code:
#wrapper {
width:90%;
height:300px;
margin:0 auto;
}
#wrapper div {
width:100%;
height:100%;
overflow:hidden;
}
img {
display:block;
margin:0 auto;
}
<div id="wrapper">
<div>
<img src="http://placehold.it/900x300"/>
</div>
</div>
Yes, the div "wrapper" is required. I’ve been searching around and remembered that I can use the property transform
, but would like a less "radical" solution. I also tried to put text-align:center;
in the common div and display:inline-block;
in the image, but without positive results also.
The image needs to stay at the original width
– Rafael Almeida
@Rafaelalmeida as I said in the reply, just use
min-width
and/ormax-width
in the image.– Renan Gomes
The images have different widths, it would not be possible to stimulate a minimum or maximum width, otherwise the quality of them would be modified.
– Rafael Almeida
@At some point you will have to set a limit for the image. Anyway, there is the suggestion.
– Renan Gomes