Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7659947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:24:00+00:00 2026-05-31T13:24:00+00:00

This is what I need: The image must completely fill 100% the area the

  • 0

This is what I need:

  • The image must completely fill 100% the area the div covers – left to
    right and top to bottom.
  • the image must not be squashed or streched – just be cropped or
    must overflow.
  • The image must be kept as small as possible, so whatever the resize – you
    can still see either the very sides OR the very top and bottom.

The div itself will be adjusting in height and width as both are a percentage of the main window.

I have found a little bit of JavaScript here that is manipulating the image just how I want when the window is resized, but is displaying it in the whole window.

<html> 
<head> 
<title>test</title> 

<script type="text/javascript"> 

function resizeImage() 
{ 
    var window_height = document.body.clientHeight 
    var window_width  = document.body.clientWidth 
    var image_width   = document.images[0].width 
    var image_height  = document.images[0].height 
    var height_ratio  = image_height / window_height 
    var width_ratio   = image_width / window_width 
    if (height_ratio > width_ratio) 
    { 
        document.images[0].style.width  = "100%" 
        document.images[0].style.height = "auto" 
    } 
    else 
    { 
        document.images[0].style.width  = "auto" 
        document.images[0].style.height = "100%" 
    } 
} 
</script> 

</head> 

<body onresize="resizeImage()"> 

<img onload="resizeImage()" src="f/a.jpg">   

</body> 
</html> 

Here is a demo

Please don’t just answer that all I need is:

<img style="width : 100%;">

This is so much more than that.

It’s not too easy to explain but check the demo and drag the corner of the window around and that’ll be worth 1000 words…!

Can it (or something like it) be made to work the same way within a % sized div?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:24:01+00:00Added an answer on May 31, 2026 at 1:24 pm

    I wrote a jQuery plugin that does exactly this. Check out my blog post here and the demo here

    jQuery.fn.resizeToParent = function(options) {
      var defaults = {
       parent: 'div'
      }
    
      var options = jQuery.extend(defaults, options);
    
      return this.each(function() {
        var o = options;
        var obj = jQuery(this);
    
        // bind to load of image
        obj.load(function() {
          // dimensions of the parent
          var parentWidth = obj.parents(o.parent).width();
          var parentHeight = obj.parents(o.parent).height();
    
          // dimensions of the image
          var imageWidth = obj.width();
          var imageHeight = obj.height();
    
          // step 1 - calculate the percentage difference between image width and container width
          var diff = imageWidth / parentWidth;
    
          // step 2 - if height divided by difference is smaller than container height, resize by height. otherwise resize by width
          if ((imageHeight / diff) < parentHeight) {
           obj.css({'width': 'auto', 'height': parentHeight});
    
           // set image variables to new dimensions
           imageWidth = imageWidth / (imageHeight / parentHeight);
           imageHeight = parentHeight;
          }
          else {
           obj.css({'height': 'auto', 'width': parentWidth});
    
           // set image variables to new dimensions
           imageWidth = parentWidth;
           imageHeight = imageHeight / diff;
          }
    
          // step 3 - center image in container
          var leftOffset = (imageWidth - parentWidth) / -2;
          var topOffset = (imageHeight - parentHeight) / -2;
    
          obj.css({'left': leftOffset, 'top': topOffset});
        });
    
        // force ie to run the load function if the image is cached
        if (this.complete) {
          obj.trigger('load');
        }
      });
    }
    

    And if you want the image to resize when the window is resized, just bind a resize handler to the window:

    $(window).resize(function() {
        $('img').resizeToParent();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here in this html i need to get the image name arrow_down using jquery
I need to match a image url like this: http://site.com/site.com/files/images/img (5).jpg Something like this
I need to upload a given image using Amazon S3 I have this PHP:
I need to encrypt an image of size 151*15 with RSA. This is the
I have this method that changes an larger image source on click. I need
According to this question I succeeded to create upload image, but now I need
On my MVC3 _Layout.cshtml page I show a banner image like this <body> <div
This problem must have been solved a million times, but Google has not been
I use this method to compress an image if(bitmapObject.compress(Bitmap.CompressFormat.PNG, 100, fOut)) { ... }
I've been googling this for long time! I need to show images on some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.