Answer the question
In order to leave comments, you need to log in
How to center an image on the parent's width/height?
Hello, could you please tell me what methods of centering an image with different height/width in a parent with explicitly given dimensions do you know?
Do not offer plugins, volumetric functions too.
Thank you.
upd: An
image can have a width greater than the width of its parent, and it must not be cropped.
Answer the question
In order to leave comments, you need to log in
img{
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}
I take it your pictures are different sizes? and the block for pictures is fixed?!
And to keep proportions?
Make the image a background and give it background-size: cover;
He will do all the work for you.
And if you need to specifically center img - then it's not very simple, I wrote a small function in js.
Flexboxes , simple, effective, in css, no matter if you know the size of the box or not...
If I understand your question correctly, then just give the picture max-width: 100%, you can center it horizontally or text-align: center in the parent or margin: 0 auto, and if you need vertically, then look here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question