Resize image proportionally to CSS, is this possible?

Is there a way to resize images while maintaining their aspect ratio using CSS?

The container has a fixed width and height

<div class="container"> <img class="theimage" src="something" /> </div> 

and the reason I ask is because the layout can change (from the list to the icons through the class) and the images should be changed (40% less) in proportion.

I know how to do this using JavaScript, as well as how to resize using CSS, but you really don't believe that this can be done in proportion to CSS, unless there is a reasonable way.

+4
source share
2 answers
 .theimage{ width:100%; height:auto; } 

or

 .theimage{ width:auto; height:100%; } 

Depending on how you want to assign scale preference ... :) :)

That's all.

+6
source

To preserve image ratio when scaling, you can use object-fit CSS3 propperty.

Useful article: Controlling image proportions with CSS3

 img { width: 100%; /* or any custom size */ height: 100%; object-fit: contain; } 
+5
source

Source: https://habr.com/ru/post/1342619/


All Articles