
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 0.69 KB | hits: 12 | expires: Never
Centering div using jQuery
// Center content when width changes
if($("#organizer_listings_container").css("position") != 'absolute') {
$("#organizer_listings_container").css("position","absolute");
var left = ( $(window).width() - $("#organizer_listings_container").width() ) / 2 + "px";
$("#organizer_listings_container").css('left', left); // set initial 'left'
}
left = ( $(window).width() - ( 852 ) ) / 2 + "px";
$("#organizer_listings_container").animate({'left': left}); // animate to final 'left'
<div class="container" style="background:blue; width:100%;">
<div class="box" style="background:green; width:50px; height:40px; margin: 0 auto; margin-top:20px;"></div>
</div>