You can preserve the aspect ratio by specifying only width and setting height to auto using CSS property. This will render a 400px wide image. The height is adjusted accordingly to preserve the aspect ratio of the original image.
The aspect ratio of an image or video is the proportional relationship of the width to the height. You'll recognize it as two numbers separated by a colon in an x:y format. For instance, a 6×4 inch image has an aspect ratio of 3:2, whereas a 1920×1080 pixel video has an aspect ratio of 16:9.
Don't set height AND width. Use one or the other and the correct aspect ratio will be maintained.
.widthSet {
max-width: 64px;
}
.heightSet {
max-height: 64px;
}
<img src="http://placehold.it/200x250" />
<img src="http://placehold.it/200x250" width="64" />
<img src="http://placehold.it/200x250" height="64" />
<img src="http://placehold.it/200x250" class="widthSet" />
<img src="http://placehold.it/200x250" class="heightSet" />
here is the sample one
div{
width: 200px;
height:200px;
border:solid
}
img{
width: 100%;
height: 100%;
object-fit: contain;
}
<div>
<img src="https://upload.wikimedia.org/wikipedia/meta/0/08/Wikipedia-logo-v2_1x.png">
</div>
Set width
and height
of the images to auto
, but limit both max-width
and max-height
:
img {
max-width:64px;
max-height:64px;
width:auto;
height:auto;
}
Fiddle
If you want to display images of arbitrary size in the 64x64px "frames", you can use inline-block wrappers and positioning for them, like in this fiddle.
<img src="Runtime Path to photo"
style="border: 1px solid #000; max-width:64px; max-height:64px;">
Use object-fit: contain
in css of html element img
.
ex:
img {
...
object-fit: contain
...
}
None of the methods listed scale the image to the largest possible size that fits in a box while retaining the desired aspect ratio.
This cannot be done with the IMG tag (at least not without a bit of JavaScript), but it can be done as follows:
<div style="background:center no-repeat url(...);background-size:contain;width:...;height:..."></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With