Auto resize image to fill container div

Hi all,

I’m working on a fluid-layout website with two main divs side-by-side at 50% width each. One of these divs is split horizontally with 2 divs at 50% height each. Inside these divs are images or videos which need to fill the container div.

Please see this link for a working example: Click here

The problem I have is that when the window is resized, the images do not fill the container. Is there some javascript that is needed to automatically resize the images to fill the container divs?

Can anyone please tell me what I need to do to achieve this?

Thank you in advance,



You can just do this with css - you just need to tell the img or vid width to equal 100% its container width. This would work for example:

#div {width: 50%}
#div img {width:100%}

If you post your code we may be able to help you further.


You can do it like this:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Dynamically resize image</title>
    <script src="" type="text/javascript"></script>
      html, body {
        margin: 0;
        padding: 0;
        height: 100%;
        height: 100%;
    <div id="container"><img src="contact.jpg" id="image"></div>
      $(window).load(function() {
        function resizeImage(){
          img.css('height', el.height());
        $(window).resize(function() {
        var el = $("#container");
        var img = $("#image");

Here’s an example of what it looks like:

Whatever the viewport dimensions, the image will always scale to take up 100% of the available height (try resizing the window)


Edit: Oops! Ryan was faster and has a better solution. Note to self: Just because you can do it in jQuery, doesn’t necessarily make it a good idea :slight_smile:

Thanks for your replies guys. The solutions above are good but they do not keep the proportions of the image. The image needs to fill both the height and the width at 100% without stretching or squashing the image. Please see this website as an example:

Unless you’ve defined a height it should maintain the proportions - or you could try height:100% but it wont always fill the container like you’re after. You could try background-size:cover - there’s some jquery there they you might be able to use on videos as well.

To maintain image proportion and still fill whole container.
We must set container’s overflow: hidden
And set shortest side to 100% and make another side to auto. (Some portion will be clipped out)


The page you link to uses JavaScript to automatically resize the image.
However, this doesn’t always work flawlessly and by manually resizing your browser, it is quite easy to create a gap between the image on the left and the content on the right.
Also, parts of the image get cut off if you resize your browser below certain dimensions.
This is logical as the aspect ratio must be preserved, so that the image doesn’t get squashed or stretched.

If I was you, I’d opt for Ryan’s CSS solution, so that the image always fills the height of the viewport and live with the fact that 100% height, 100% width, correct aspect ratio and no cropping will not be possible.

If you’re interested this is the code the other site uses to do the resizing:

function resizeImage(){
  //Resize Bottom Right Image
  var image = jQuery("#bottomRightImage");
  var imageParentElement = jQuery('#hp-rhs-bottom');
  var widthRatio = imageParentElement.outerWidth() / imageRatio.width;
  var heightRatio = imageParentElement.outerHeight() / imageRatio.height;
  var ratio = Math.max(widthRatio, heightRatio);
    width: Math.ceil(ratio*imageRatio.width) ,
    height: Math.ceil(ratio*imageRatio.height)