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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:27:09+00:00 2026-06-17T14:27:09+00:00

I have problems using jQuery .css(‘background-size’,’contain’); On Google Chrome sometimes it works and sometimes

  • 0

I have problems using jQuery .css('background-size','contain');

On Google Chrome sometimes it works and sometimes it doesn’t.. (90% of cases it doesn’t work)

Here is the code I’m using:

function scaleImg(){    

    var img = $('#img_src_div img');
    if(img.height() >= 600 || img.width() >= 570){
        $('#img_src_div').css('background-size','contain');
    }

}

Here is a JSFiddle example, and it is working. But inside my page it is not.
And it’s pretty much the same, there is some php generated content, like urls and some text. and the code it’s inside a parent div.

I tried removing all javascripts that may interfer with it, but it’s the same. In firefox it’s all ok.

EDIT:

images.php – This it’s included on another php page.

//Removed - Was not usefull.

2nd EDIT: By calling scaleImg(); manually after the page is loaded, the background get scaled. So there is a issue on the page load..

This is the exact code i’m using right now.

    <script type="text/javascript">
    function scaleImg() {
    var container = document.getElementById(\'img_src_div\');
    var img = container.children[0]; // assuming image is first child
    // var img = container.getElementsByTagName(\'img\')[0]; // use this one if not
    if( img.width >= 570 || img.height >= 600) container.style.backgroundSize = "contain";
}
    $(document).ready(function(){
    scaleImg();
    });
    </script>

3rd and last (hopefully) EDIT:

I’ve solved, the problem was that $(document).ready(...); it’s executed before the image has been fully downloaded. So instead to call scaleImg(); on document ready function, i call it inside onload="" of the image tag.

<img alt="" onload="scaleImg(this.width,this.height);" id="img_src" src="image_url" />
  • 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-17T14:27:11+00:00Added an answer on June 17, 2026 at 2:27 pm

    Try plain JavaScript:

    function scaleImg() {
        var container = document.getElementById('img_src_div');
        var img = container.children[0]; // assuming image is first child
        // var img = container.getElementsByTagName('img')[0]; // use this one if not
        if( img.width >= 570 || img.height >= 600) container.style.backgroundSize = "contain";
    }
    

    It’s possible that jQuery is doing some validation on your CSS call and is failing for some reason. Plain JS is more reliable 😉

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

Sidebar

Related Questions

I have some strange scrolling problems with my PhoneGap (using jQuery Mobile) app: I'm
is there anyone who have experienced problems using the latests jQuery version with one
I'm using backstretch for the background image so that I don't have problems with
I am using qTip2 on my website but have some problems with the css.
I'm having problems using ajax with jQuery... I have the following jscript: $(document).ready(function() {
I 've created a small photo tour using HTML, CSS and jQuery. It works
I have problem while using jquery maskedinput with asp.net textbox. I have a check
I have a little problem about using jQuery (I really do not know jQuery
Greetings, I have a problem using jqgrid and jquery tab (I am coding in
I have some problem to get the utf-8 string from PHP using jQuery $.load()

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.