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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:41:56+00:00 2026-06-17T11:41:56+00:00

I am making an image gallery. This is the HTML for the image: <div

  • 0

I am making an image gallery. This is the HTML for the image:

<div style="width: 84%; height: 100%; float: left; text-align: center;"><img
id="mainimage" style="height: 100%;" 
src="http://www.gymheroapp.com/workouts/img/spinner.gif"/></div>
<!-- Loading spinner is temp image, will be replaced by Javascript later -->

It works fine when the image is square, or the width is not too much more than the height. It breaks when the width is too great, though. Example of image overflowing:

image overflowing

Is there a way I could check whether there is overflow within my Javascript? Something like this:

image.setAttribute('height', '100%')
image.removeAttribute('width')
if (image.isOverflowing()) {
    image.setAttribute('width', '100%')
    image.removeAttribute('height')
}

Even better, is there any way to scale the image properly but fit it withing the containing 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-06-17T11:41:57+00:00Added an answer on June 17, 2026 at 11:41 am

    jquery example

    Best fit:

    $('img').on('load',function(){
        var css;
        var ratio=$(this).width() / $(this).height();
        var pratio=$(this).parent().width() / $(this).parent().height();
        if (ratio<pratio) css={width:'auto', height:'100%'};
        else css={width:'100%', height:'auto'};
        $(this).css(css);
    });
    

    Edit to account for caveat where image is in cache

    $('img').on('bestfit',function(){
        var css;
        var ratio=$(this).width() / $(this).height();
        var pratio=$(this).parent().width() / $(this).parent().height();
        if (ratio<pratio) css={width:'auto', height:'100%'};
        else css={width:'100%', height:'auto'};
        $(this).css(css);
    }).on('load', function(){
        $(this).trigger('bestfit');
    }).trigger('bestfit');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making div with image and text. User can hover on this div
I currently have this on a webpage I'm making: HTML <div id=pageHeader> <nav id=siteNav>
im making an image button like this: <input type=image src=red.jpeg width=150px> but it is
I need help with making an image float onto the canvas in HTML5 and
Basicly I'm making an image gallery that downloads from the web. So - when
I'm making a C# image gallery for a website (I know there's many free
I was making a light box gallery but this code doesn't hide the lightbox
I'm making an image gallery and I want to have a bunch of thumbnails
I am making an image gallery in XNA and XAML. I need to have
I'm making a sliding image gallery where there are 3 pages with 3 images

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.