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 7626129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:02+00:00 2026-05-31T05:17:02+00:00

I need to fit images to the viewport/window size. I use this code for

  • 0

I need to fit images to the viewport/window size.

I use this code for doing it:

$(".fittedImg").each(function () {

        // I want to limit the size, and show a scrollbar on very small viewport
        height  = ($(window).height() < 725) ? 725 : $(window).height();
        width   = ($(window).width() < 1150) ? 1150 : $(window).width();

        var newWidth, newHeight;
        $(this)
            .removeAttr("height")
            .removeAttr("width"); // reset img size

        // calculate dimension and keep it 
        if (($(this).width() / width) > ($(this).height() / height)) {
            newHeight = height;
            newWidth = height / ($(this).height() / $(this).width());

        } else {
            newHeight = width / ($(this).width() / $(this).height());
            newWidth = width;
        };
        $(this).stop(true, false).animate({
            "height": newHeight + "px",
            "width": newWidth + "px"
        });
});

I wrapped that code in a function which I call on resize, and on events such as $(‘.fittedImg’).load() and $(‘.fittedImg’).ready().

The result’s very weird:
In Chrome it normally works, but somtimes the images stretch on repeated refreshes. On IE there’s quite the same problem. Opera does not fail never and safari ignore the calculation at all (ever stretching).

What’s wrong with my calculation and/or events calling?

Thank you.

  • 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-31T05:17:03+00:00Added an answer on May 31, 2026 at 5:17 am
    1. Make sure the image is fully loaded before extracting width/height
    2. Don’t remove the width/height attributes before calculating

    Assuming you want to fit the image inside a containing element (in this case the window), here are some simpler calculations you might find useful. The concept is quite simple, based on the containing measures and the image measures, calculate a ratio. Then multiply it with the original measures:

    $(".fittedImg").load(function(){
    
      var imgWidth = this.width,
          imgHeight = this.height,
          winWidth = $(window).height(),
          winHeight = $(window).width(),
          ratio = Math.min(winWidth/imgWidth, winHeight/imgHeight);
    
       // if you don’t want it to upscale, use Math.min or Math.max on the ratio
    
       $(this).stop(true, false).animate({
           height: imgHeight*ratio,
           width: imgWidth*ratio
       });
    });
    

    Please note that some extensions such as AdBlock can mess up the extracted width/height from the image. You can console and text the imgWidth/imgHeight just to make sure that it is available.

    Also please note that chances are that your image is already loaded when this event is added, depending on how you place your code. In that case, you should check for the complete property on the IMG element:

    $('fittedImg').each(function() {
        $(this).load(function() {
            // the calculations goes here
        });
        if (this.complete) { // check if image is already loaded
            $(this).trigger('load');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fit images on my site to the size of their containing
I need to adjust my jqgrid height dynamically to fit in the browser window
I bring the code from google to correct it fit for my need The
I need to design a page with border images on each side. I need
I need to reduce the size of apk file which currently is 40M. This
Need to call a filter function on some options based on a radio selected
Need to use own imaged markers instead built-in pins. I have several questions. 1.
Need some help to solve this. I have a gridview and inside the gridview
I need to generate small images for certain parts of text. Those will have
What I need is to set the limit of an image's width to 100%

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.