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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:47:22+00:00 2026-06-16T08:47:22+00:00

What is the best way to simulate upcoming CSS3 object-fit property using CSS2 &

  • 0

What is the best way to simulate upcoming CSS3 object-fit property using CSS2 & JS? I’m interested in contain & cover methods for fitting.

EDIT: In CSS3 there is the new object-fit mechanism described here: http://dev.opera.com/articles/view/css3-object-fit-object-position/ but object-fit is not yet supported outside the Opera browsers. To create a portable solution I need to make an img grow or shrink to the size of its parent div without loosing the original aspect ratio of the image. This can be achieved by either containing the whole image (technique also called letterboxing) or by enlarging & overflowing the image until the smallest dimension matches the div‘s dimension (technique also called as cover). There are a few jquery imagefit libraries out there by they are very buggy (perhaps they work only in a specific situation on a specific browser).

EDIT 2: This question has been understood by those taking the time to read it, a good answer has been suggested and an answer has been given. It is difficult to understand the decision of closing it.

  • 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-16T08:47:23+00:00Added an answer on June 16, 2026 at 8:47 am

    For anyone interested, here’s the actual working solution:

    JSFiddle Demo

    and the actual code:

    (function($) {
      $.fn.imagefit = function(contain) {
        this.each( function() {
          var $this           = $(this),
              $wrapper        = $this.parent(),
              wrapper_width   = $wrapper.width(),
              wrapper_height  = $wrapper.height(),
              wrapper_ratio,
              image_ratio;
    
    
            // ratios
          image_ratio   = $this.width() / $this.height();
          wrapper_ratio = wrapper_width / wrapper_height;
    
          var ratio_cond = wrapper_ratio > image_ratio;
            if(contain) {
                ratio_cond = !ratio_cond;
            }
    
          if ( ratio_cond ) {
              $wrapper.css({
                'background'  : 'url('+$this.get(0).src+')',
                'background-size'  : '100% auto',
                'background-position'  : '0px 50%',
                'background-repeat'  : 'no-repeat'
              });
          } else {
              $wrapper.css({
                'background'  : 'url('+$this.get(0).src+')',
                'background-size'  : 'auto 100%',
                'background-position'  : '50% 0px',
                'background-repeat'  : 'no-repeat'
              });
          }
    
          $this.remove();
    
        });
        return this;
      };
    
      $('div.bgthumb#cover > img').imagefit(false);               
      $('div.bgthumb#contain > img').imagefit(true);               
    
    }(jQuery));​
    

    @Hippocrates: Thanks a lot for pointing me in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how is the best way to be able to simulate ie6 in my machine
What the best way to simulate the user pressing enter? $(element).keypress() doesn't seem to
What is the best way to simulate a user entering text in a text
In R, what's the best way to simulate an arbitrary univariate random variate if
I’m looking for a way to simulate resistance using the jQueryUI draggable plugin (similar
What's the best way to simulate a troolean variable with an HTML form? Specifically,
What would be the best way to simulate bullets in bulletphysics? Small capsule bodies?
Best way to match 2 pages for Google analytics. Pages to Query: 1.) this_google.html
The best way I think to explain this is to tell you what my
The best way to explain my problem is by a example. I have a

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.