<div id="wrapper">
<div id="child"></div>
</div>
If the position is #childnot absolute, you can set the left and right margins to auto:
#child {
margin: 0 auto;
}
Or, if the position is absolute, you can try the following:
$("#child").css("left", function(){
return ($("#wrapper").width() - $(this).width()) / 2;
});
source
share