Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stretch image to fill div, but not skew

Tags:

jquery

css

I'm trying to make an image stretch to fit inside a div. However I want it to overflow so that the entire div is filled. The div resizes with the page. I want an effect similar to the jQuery bgstretcher plugin (http://www.ajaxblender.com/bgstretcher-2-jquery-stretch-background-plugin-updated.html) , but I cannot use that plugin because I'm already using it for the background image on the page, and it seems to not work when you try and activate multiple instances of it.

So what I'm asking is for some sort of simple jQuery function that resizes an image to completely fill a div with overflow hidden, so that there are no gaps, but without skewing the image.

Edit:

http://jsfiddle.net/R7UCZ/

That's sort of what I'm after, but I want the image to fill the entire div without skewing. If the image goes out of the div a bit, that's fine, but I need it to resize with the div, which resizes with the page, both height and width.

like image 628
r0tterz Avatar asked Jun 30 '12 01:06

r0tterz


People also ask

How do you fill a box with an image without distorting it?

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 stretch an image to fit in a div?

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 can I fix an image in a div without stretching it?

Another way is to simply use background-image on the container instead, but resizing it (if you want to stretch smaller images) will be difficult unless you use background-size which isn't fully supported. Otherwise, it's a great easy solution. Save this answer. Show activity on this post.


1 Answers

First you need to get the image properties and see which one is larger, the height or the width, then resize the image based on the div size after the window resizes, like this:

  //this would load up initially, you will need this data for future processing


   div = $("div");
   imgsrc = div.find('img').attr('src');

   var img = new Image()
   img.onload = function(){
        imgw = img.width;
        imgh = img.height;

        //after its loaded and you got the size..
        //you need to call it the first time of course here and make it visible:

        resizeMyImg(imgw,imgh);
        div.find('img').show();

        //now you can use your resize function
        $(window).resize(function(){ resizeMyImg(imgw,imgh) });

        }
   img.src = imgsrc


   function resizeMyImg(w,h){     
      //the width is larger
      if (w > h) {
        //resize the image to the div
        div.find('img').width(div.innerWidth() + 'px').height('auto');
      }        
      else {
        // the height is larger or the same
        //resize the image to the div
        div.find('img').height(div.innerHeight() + 'px').width('auto');
      }

     }

You can actually find a complete solution here: http://jsfiddle.net/CDywS/1

like image 138
Control Freak Avatar answered Oct 23 '22 05:10

Control Freak