S
S
Sasha2015-09-23 19:44:01
css
Sasha, 2015-09-23 19:44:01

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

4 answer(s)
A
Andrey Goncharov, 2015-09-23
@agmegadeth

img{
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
margin: auto;
}

X
Xandrio, 2015-09-23
@Xandrio

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.

S
Sergey, 2015-09-23
Protko @Fesor

Flexboxes , simple, effective, in css, no matter if you know the size of the box or not...

I
Ivan Tokar, 2015-09-26
@Jonathan

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 question

Ask a Question

731 491 924 answers to any question