2

Hey guys, quick question, all I want to do is resize an image to fit inside a small container when I run this function. Right now, only a portion of the image is shown inside the div. If anyone has any ideas, I would appreciate any advice.

$(this)
       .css({
           backgroundImage    : 'url(' + src + ')',   // set background image
           backgroundPosition : 'center center',      // position background image
           backgroundRepeat   : 'no-repeat'           // don't repeat image
       });

2 Answers 2

4

Only very modern browsers (meaning Webkit) can scale background images. So I'd recommend inserting a new DOM element as the image and then changing it's height and width should scale it down. Than use CSS (things like position and z-index should help you) to position it where you need it.

Sign up to request clarification or add additional context in comments.

2 Comments

Agreed. You should do this if you want a good browsing experience for all. You'll need to do some aspect ratio calculations if your images are going to be different sizes as well.
yeah thats what I thought. Thanks guys.
1

Well this is css:

#container{
    background-size: cover;
    -moz-background-size: cover;
    background-repeat:no-repeat;
    background-attachment:fixed;
    background-position:center;
    }

Jquery:

var url = "your_url";
$('#container').css("background-image", url);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.