Dmitriy Demir Dmitriy Demir - 2 months ago 7
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

You need to do like this:

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

Or

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