<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p><img width="400" height="400" src="http://i.stack.imgur.com/aEEkn.png">
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p><div class="parent"><img width="400" height="400" src="https://placehold.it/400x400"></div>
<p>This image is originally 640x220, but should get resized by the CSS:</p><div class="parent"><div class="container"><img width="640" height="220" src="https://placehold.it/640x220"></div></div>
$(".temp_image").attr("src","str.jpg" ).load(function() {// callback to get actual size of received image
// define to expand image in Heightif(($(".temp_image").height() / $(".temp_image").width()) > display_aspect_ratio ) {$(".image").css('height', max_height_of_box);$(".image").css('width',' auto');} else {// define to expand image in Width$(".image").css('width' ,max_width_of_box);$(".image").css('height','auto');}//Finally put the image to Completely Fill the display area while maintaining aspect ratio.$(".image").attr("src","str.jpg");});
img {max-width: 80px; /* Also works with percentage value like 100% */height: auto;}
<p>This image is originally 400x400 pixels, but should get resized by the CSS:</p><img width="400" height="400" src="https://i.stack.imgur.com/aEEkn.png">
<p>Let's say the author of the HTML deliberately wantsthe height to be half the value of the width,this CSS will ignore the HTML author's wishes, which may or may not be what you want:</p><img width="400" height="200" src="https://i.stack.imgur.com/aEEkn.png">
img{width: 200px;height: auto;object-fit: contain; /* Fit logo in the image size */-o-object-fit: contain; /* Fit logo fro opera browser */object-position: top; /* Set logo position */-o-object-position: top; /* Logo position for opera browser */}