Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS image scaling to fit within area not distort

Is there a way with CSS or otherwise of making an image fit within an area. Lets say I have multiple images of different sizes and I want them all to fit within a div of 150px by 100px. I don't want to scale the images though as some may be tall and others narrow I simply want them to fit within this area with the rest hidden.

I thought about using overflow:hidden but it appears to not be hidden in IE6.

Any ideas?

like image 803
RIK Avatar asked Mar 20 '11 15:03

RIK


People also ask

How do I resize an image without distorting it CSS?

There are a few ways to make an image smaller without distortion in CSS. One way is to use the CSS property 'width'. You can set the width to a percentage or a pixel value. Another way is to use the CSS property 'max-width'.

How do I resize an image without distorting it in HTML?

Using object-fit When you add an image to a page using the HTML <img> element, the image will maintain the size and aspect ratio of the image file, or that of any HTML width or height attributes.

How do I make an image fit perfectly in CSS?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

How do you scale an image proportionally in CSS?

A common use is to set max-width: 100%; height: auto; so large images don't exceed their containers width. Another way is the use of object-fit property, this will fit image, without changing the proportionally.


3 Answers

You should try using this:

img{   width: auto;   max-width: 150px;   height: auto;   max-height: 100px; } 

Edit: Looks like IE6 doesn't support max-width and max-height properties. However, you can implement the workaround given here: max-width, max-height for IE6

Excerpt (in case linked article stops working):

img {   max-height: 100px;   max-width: 100px;   width: expression(document.body.clientWidth > 150? “150px”: “auto”);   height: expression(document.body.clientHeight > 100? “100px”: “auto”); } 
like image 118
Sang Suantak Avatar answered Sep 29 '22 05:09

Sang Suantak


When you say "fit within this area" with the rest hidden I feel like you want the image to not be scaled down at all and basically crop off any excess.

I might be interpreting you're question wrong, but try this and see if it produces the effect you're looking for.

.img-holder {    width: 150px;    height: 150px;    position: relative;    overflow: hidden;  }  .img-holder img {    position: absolute;    display: block;    top: 0;    left: 0;  }
<div class="img-holder">    <img src="http://img.playit.pk/vi/dH6NIe7wm4I/mqdefault.jpg" />  </div>
like image 38
Jimmy Cleveland Avatar answered Sep 29 '22 06:09

Jimmy Cleveland


This won't work in IE6 (as required by the OP), but for completeness you can achieve the required effect on newer browsers using CSS3's background-size:cover and setting the image as a centered background image. Like so:

div { 
  width:150px;
  height:100px;
  background-size:cover; 
  background-position:center center; 
  background-repeat:no-repeat; 
  background-image:url('somepic.jpg');
}
like image 35
Dan Avatar answered Sep 29 '22 05:09

Dan