Dmitriy Demir Dmitriy Demir - 1 year ago 80
jQuery Question

How to set background-size with jQuery with both height and width

I'm trying to set div's background-size with jQuery (it should depend on other elements). The problem is that while I can set it with one value, like this:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', contentHeight);

It doesn't do anything if I do it like this:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', 'auto ' + contentHeight + 'px !important');

I have a feeling that I made a silly mistake, but I fail to see it...

Answer Source

You need to do like this:

contentHeight = $('#content').outerHeight(true);
$('#background').css('background-size', "auto " + contentHeight + "px", "important");


contentHeight = $('#content').outerHeight(true);
$('#background').css({'background-size': "auto " + contentHeight + "px !important"});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download