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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:00:25+00:00 2026-06-01T11:00:25+00:00

Is there a css-only solution to scale an image into a bounding box (keeping

  • 0

Is there a css-only solution to scale an image into a bounding box (keeping aspect-ratio)? This works if the image is bigger than the container:

img {
  max-width: 100%;
  max-height: 100%;
}

Example:

  • Use case 1 (works): http://jsfiddle.net/Jp5AQ/2/
  • Use case 2 (works): http://jsfiddle.net/Jp5AQ/3/

But I want to scale up the image until a dimension is 100% of the container.

  • Use case 3 (doesn’t work): http://jsfiddle.net/Jp5AQ/4/
  • 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-01T11:00:27+00:00Added an answer on June 1, 2026 at 11:00 am

    Note: Even though this is the accepted answer, this other answer is more accurate and is currently supported in all browsers if you have the option of using a background image.

    Edit 2: In the modern age, using object-fit might be an even better solution: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

    No, there is no CSS only way to do this in both directions. You could add

    .fillwidth {
        min-width: 100%;
        height: auto;
    }
    

    To the an element to always have it 100% width and automatically scale the height to the aspect ratio, or the inverse:

    .fillheight {
        min-height: 100%; 
        width: auto;
    }
    

    to always scale to max height and relative width. To do both, you will need to determine if the aspect ratio is higher or lower than it’s container, and CSS can’t do this.

    The reason is that CSS does not know what the page looks like. It sets rules beforehand, but only after that it is that the elements get rendered and you know exactly what sizes and ratios you’re dealing with. The only way to detect that is with JavaScript.


    Although you’re not looking for a JS solution I’ll add one anyway if someone might need it. The easiest way to handle this with JavaScript is to add a class based on the difference in ratio. If the width-to-height ratio of the box is greater than that of the image, add the class "fillwidth", else add the class "fillheight".

    $('div').each(function() {
      var fillClass = ($(this).height() > $(this).width()) 
        ? 'fillheight'
        : 'fillwidth';
      $(this).find('img').addClass(fillClass);
    });
    .fillwidth { 
      width: 100%; 
      height: auto; 
    }
    .fillheight { 
      height: 100%; 
      width: auto; 
    }
    
    div {
      border: 1px solid black;
      overflow: hidden;
    }
    
    .tower {
      width: 100px;
      height: 200px;
    }
    
    .trailer {
      width: 200px;
      height: 100px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="tower">
      <img src="http://placekitten.com/150/150" />
    </div>
    <div class="trailer">
      <img src="http://placekitten.com/150/150" />
    </div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make some CSS rules visible only for Opera?
Is there a CSS code that changes this text This is it to this
Possible Duplicate: Is there a CSS parent selector? Is there some kind of .this
The only solution i have found is ($(this).innerHeight() - $(this).height()) / 2 But /
There has to be a easy solution to this. I have been trying to
Modifying a SnapPages theme, meaning I only have access to the CSS stylesheet. I
Is there CSS Meta Framefork for ASP.NET? Something like LESS or xCSS.
Is there any CSS grid system that supports full viewport width? Most Grid System
Isn't there a CSS way of having the page to get a border around
Are there any html/css checkers that are javascript based like JSLINT?

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.