Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upscale an Img in CSS while, maintaining the aspect ratio?

Tags:

html

css

image

After googling around a bit, I found this

HTML

<div class="divContainer">
    <img src="image.png">
<div>

CSS

.divContainer{
   width :200px;
   height:200px;
   border:solid;
 }

img{
    width: 100%;
    height: 100%;
    object-fit: contain;
}

This works well but it can only scale until it reaches the native resolution of the original picture and stops scaling with the div container but, I want it to go ahead and upscale beyond its native resolution.

Any ideas?

like image 727
Nero296 Avatar asked Mar 05 '23 05:03

Nero296


1 Answers

Change object-fit: contain; to object-fit: cover;.

.divContainer {
    width: 700px; /* Bigger than image's width. */
    height: 500px; /* Bigger than image's height. */
    border: solid;
}

img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
<div class="divContainer">
    <img src="https://dotjpg.co/YfUB.jpg"> <!-- Image Dimensions: 640px x 426px -->
<div>
like image 113
N'Bayramberdiyev Avatar answered Apr 09 '23 18:04

N'Bayramberdiyev