Reputation: 2751
I would like to find the width of a div tag
then set it's margin-left to half of that width
using jquery.
Thanks!!
Upvotes: 1
Views: 1792
Reputation: 13461
var elem = $('#divID');
elem.css('margin-left',elem.width() / 2);
Upvotes: 0
Reputation: 79830
Try like below,
$('#divID').css('marginLeft', function() {
return $(this).width()/2;
});
Upvotes: 13
Reputation: 35309
$('div').css({'marginLeft' : $('div').width()/2});
Pure JS
var div = document.getElementsByTagName('div')[0];
div.style.marginLeft = div.offsetWidth/2 +'px';
Upvotes: 3