Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Centering div using jQuery
  2. // Center content when width changes
  3. if($("#organizer_listings_container").css("position") != 'absolute') {
  4.     $("#organizer_listings_container").css("position","absolute");
  5.     var left = ( $(window).width() - $("#organizer_listings_container").width() ) / 2 + "px";
  6.     $("#organizer_listings_container").css('left', left);       // set initial 'left'
  7. }
  8. left = ( $(window).width() - ( 852 ) ) / 2  + "px";
  9. $("#organizer_listings_container").animate({'left': left}); // animate to final 'left'
  10.        
  11. <div class="container" style="background:blue; width:100%;">
  12. <div class="box" style="background:green; width:50px; height:40px; margin: 0 auto; margin-top:20px;"></div>
  13. </div>