E
E
Emptyform2015-12-18 15:25:11
css
Emptyform, 2015-12-18 15:25:11

How to style an image with unknown proportions?

If, say, you need to arrange an arbitrary number of pictures with proportions unknown in advance, and moreover, the proportions will be guaranteed to be different. And the grid is naturally with fixed cell sizes.
How can I make the images display without distorting the proportions? And at the same time, observe the rule that the tag must have the attributes of width and height.
I myself suffered here for a couple of days, but it only works fine if the width and height attributes are not specified, but set in CSS max-width , max-height. But here it turns out that the page is not valid.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Victor Victor, 2015-12-18
@Emptyform

attributes-attributes, but no one bothers to set the width and height in styles

.img {
    width: 100%;
    max-width: 100%;
    height: auto;
}

so it will stretch to the full width of the container, but not distorted in height
Or should it also be adjusted in height?

M
MetaDone, 2015-12-18
@MetaDone

put the pictures in a fixed width div
and the pictures

img{
max-width:100%;
}

M
mr_dev1l, 2015-12-18
@mr_dev1l

object-fit:cover will solve all your problems

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question