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?
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'.
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.
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.
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.
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”); } 
                        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>  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');
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With