Old question, new answer:
When the image is larger than the wrapping div, center alignment: center or margin: auto will not work. But if the image is smaller, then solutions with absolute positioning or a negative left margin will create strange effects.
So, when the size of the image is not really known in advance (for example, on CSM), and it can be larger or smaller than the div for wrapping, there is an elegant CSS3 solution that serves all purposes:
div { display: flex; justify-content: center; height: 400px; overflow: hidden; } img { flex: none; }
Note that depending on other rules in the stylesheet, you may need to add the width: auto and / or max-width: none to img.
RavanH Oct. 14 '15 at 20:11 2015-10-14 20:11
source share