Question

may I ask you? How do I set negative margin (or anything else) in .css() method in jQuery? I am adding the size by variable, but it doesn´t works

var width = $('.gallery div img').outerWidth(true);
$('.gallery').css('margin-left', '- ' + width + 'px');

Or is there any way how to convert var width into negative number?

Was it helpful?

Solution

The following worked for me on this StackOverflow page:

$(".bottom-notice").css("margin-left", "-"+500+"px");

Notice there's no space between the - and the closing ". I think that might be what is causing your issue. Otherwise make sure width is actually a number.

OTHER TIPS

Check width first to make sure its a number, and then take the space out and the true.

var mywidth = $('.gallery div img').outerWidth();
alert(mywidth);
$('.gallery').css('margin-left', '-' + mywidth + 'px');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top