Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - resizing image to fit div [duplicate]

I have divs varying in height and width and wish for my images to be automatically resized to fill these divs 100%, and then of course centred.

At the moment my images are set to width 100% and then using the jQuery below centred, but this only works for images where the height are more than the div once resized.. how would I make it 100% for both height and width and center also.. completely filling the div (even if this means stretching the image)!

Thanks.

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var top_margin = -(img_height / 2);
    $(item).css({
        'top': '50%',
        'margin-top': top_margin
    });
});
like image 565
Jake Hills Avatar asked Oct 14 '13 13:10

Jake Hills


People also ask

How do I autofit an image in a div?

To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container. object-fit property: This property is used to specify how an image or video resize and fit the container.

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 do I fit a div image 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.

How do I make an image fit in HTML size?

One of the simplest ways to resize an image in the HTML is using the height and width attributes on the img tag. These values specify the height and width of the image element. The values are set in px i.e. CSS pixels.


2 Answers

Use CSS to set both the Width and Height of the image to 100% and the image will be automatically stretched to fill the containing div, without the need for jquery.

Also, you will not need to center the image as it will already be stretched to fill the div (centered with zero margins).

HTML:

<div id="containingDiv">
    <img src="">
</div>

CSS:

#containingDiv{
    width: 200px;
    height: 100px;
}
#containingDiv img{
    width: 100%;
    height: 100%;
}

That way, if your users have javascript disabled, the image will still be stretched to fill the entire div width/height.

OR

The JQuery way (SHRINK/STRETCH TO FIT - INCLUDES WHITESPACE):

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var div_height = $(item).parent().height();
    if(img_height<div_height){
        //IMAGE IS SHORTER THAN CONTAINER HEIGHT - CENTER IT VERTICALLY
        var newMargin = (div_height-img_height)/2+'px';
        $(item).css({'margin-top': newMargin });
    }else if(img_height>div_height){
        //IMAGE IS GREATER THAN CONTAINER HEIGHT - REDUCE HEIGHT TO CONTAINER MAX - SET WIDTH TO AUTO  
        $(item).css({'width': 'auto', 'height': '100%'});
        //CENTER IT HORIZONTALLY
        var img_width = $(item).width();
        var div_width = $(item).parent().width();
        var newMargin = (div_width-img_width)/2+'px';
        $(item).css({'margin-left': newMargin});
    }
});

The JQuery way - CROP TO FIT (NO WHITESPACE):

$('img.shelf-img').each(function(i, item) {
    var img_height = $(item).height();
    var div_height = $(item).parent().height();
    if(img_height<div_height){
        //INCREASE HEIGHT OF IMAGE TO MATCH CONTAINER
        $(item).css({'width': 'auto', 'height': div_height });
        //GET THE NEW WIDTH AFTER RESIZE
        var img_width = $(item).width();
        //GET THE PARENT WIDTH
        var div_width = $(item).parent().width();
        //GET THE NEW HORIZONTAL MARGIN
        var newMargin = (div_width-img_width)/2+'px';
        //SET THE NEW HORIZONTAL MARGIN (EXCESS IMAGE WIDTH IS CROPPED)
        $(item).css({'margin-left': newMargin });
    }else{
        //CENTER IT VERTICALLY (EXCESS IMAGE HEIGHT IS CROPPED)
        var newMargin = (div_height-img_height)/2+'px';
        $(item).css({'margin-top': newMargin});
    }
});
like image 99
IIIOXIII Avatar answered Sep 19 '22 17:09

IIIOXIII


If you want to keep the image ratio, I would set max-height and max-width to 100%. Here's a sample to show how that works. That will effectively shrink images that are larger than the div, but it will keep the aspect ratio.

For images that are smaller than the div, you will have to scale up with JavaScript. The basic algorithm is like so:

  1. Find the aspect ratio of the image (width / height)
  2. Find the aspect ratio of the div (width / height)
  3. If the image's aspect ratio is less than the div's, set the image's height to 100%
  4. If the image's aspect ratio is greater than the div's, set the image's width to 100%
  5. Whichever dimension is not set, set it to auto

Obviously, you could use this algorithm for scaling up or down, but if you can be guaranteed that your div will always be smaller than your image, you can use the simpler CSS solution.

It looks like you've got code to do centering, so I'll leave that to you.

like image 37
RustyTheBoyRobot Avatar answered Sep 16 '22 17:09

RustyTheBoyRobot