A
A
Alexander Wolf2016-12-07 11:38:27
css
Alexander Wolf, 2016-12-07 11:38:27

How to make a responsive image?

Good afternoon! Can you please tell me how to make the image have a certain width and height, but not more than 100%?
It is not difficult to do this, however, you need to somehow report the width / height, otherwise the picture becomes stretched.
Example: https://jsfiddle.net/5fs3dkqj/
How can I decide? Preferably without JS, but you can use it too :) The size of the original image is there.
PS: This question arose due to the fact that when the site is loaded (as well as when changing the image), everything jumps, since the picture does not know its height. And if you set the height, then it becomes stretched.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kovalsky, 2016-12-07
@mannaro

img {
  max-width: 100%;
}

M
Maxim, 2016-12-07
@maxfarseer

Сделайте обертку, туда передавайте нужные размеры (https://jsfiddle.net/5fs3dkqj/2/)

G
GreatRash, 2016-12-07
@GreatRash

object-fit
пример для нормальных браузеров
замазка

Алексей Сироткин, 2016-12-07
@alexeisirotkin

img {width: 100%; max-width: 100%; height: auto;}
если не нужно на всю ширину, то width: 100% уберите

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question