jsBin demo
If you are already using calc (CSS3), then you will have nothing against using it in CSS:
Using CSS3 transition and calc()
#line{ transition: 1s; }
and in your jQuery (instead of .animate() )
var outW2 = $("#line").outerWidth() / 2; $("#line").css({ left : "calc(50% - "+ outW2 +"px)", top : 70 });
Using .animate() and negative margin (for centering)
For all old browsers, you can simply use .animate() , as you did ,
moving the element up to 50%, but also in the field - half-widths with half width (to center it):
var outW2 = $("#line").outerWidth() / 2; $("#line").animate(){ left: "50%", marginLeft : -outW2 }, 1000);
jsBin demo (solution for crossbrowsers)
source share