exoslav
exoslav

Reputation: 2295

Unable to set negative margin in jQuery css method

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?

Upvotes: 1

Views: 6686

Answers (2)

Ohjay44
Ohjay44

Reputation: 897

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');

Upvotes: 2

ben_makes_stuff
ben_makes_stuff

Reputation: 518

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.

Upvotes: 5

Related Questions