Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize an image in proportion with CSS, is it possible?

Is there a way to resize images retaining their proportions with CSS?

The container has a fixed width and height

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

and the reason I'm asking is because the layout can change (from list to icons via a class) and the images need to be resized (about 40% less) in proportion.

I know how to do it with JavaScript and also how to resize via CSS, but don't really believe it can be done in proportion with CSS, unless there is some clever way.

like image 921
jackJoe Avatar asked Mar 07 '11 10:03

jackJoe


2 Answers

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

or

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

Depending on how you wanna give the scale preference... :) :)

Thats all.

like image 147
Arindam Paul Avatar answered Oct 22 '22 22:10

Arindam Paul


To save the Image ratio while scaling you can use object-fit CSS3 propperty.

Useful article: Control image aspect ratios with CSS3

img {
    width: 100%; /* or any custom size */
    height: 100%; 
    object-fit: contain;
}
like image 30
Andrii Verbytskyi Avatar answered Oct 23 '22 00:10

Andrii Verbytskyi