3

I need to set the CSS property of width as the same size as the browser's current width. To do this, I did var setWidth = $(window).width(), but I don't know how to apply this to my current code, which is something like this:

$(document).ready(function(){
    $("#backgroundImg").css("width", ""); 
});

Is there any way to do this? I'm new to JQuery so I might just be really amateur.

4 Answers 4

7

Try this

$(document).ready(function() {
    var setWidth = $(window).width();
    $("#backgroundImg").css("width", setWidth + 'px'); 
});

or you can use $.width

$(document).ready(function() {
    $('#backgroundImg').width( $(window).width() )
});
Sign up to request clarification or add additional context in comments.

Comments

0

update code to following

$("#backgroundImg").css("width", setWidth + "px");

Comments

0

One option: $("#backgroundImg").css("width","100%");

with your variable option do: var setWidth = $(window).width() +'px';

so your code would be

$(document).ready(function(){
$("#backgroundImg").css("width", setWidth); 

});

1 Comment

The first option is incorrect. Second option is correct with a small error - just add "px" as well
0

This should make the background match the browser's current width:

$( "#backgroundImg" ).width( setWidth )

Source

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.