margin-left:50% ; margin-right:50%;
would imply that the item you are centering has no width.
For a block level element like a div
, the best way to center it is:
div.centered { margin: 0 auto; }
The 0 before the auto refers to top and bottom so that can be set to whatever you like. The important part is the auto
which will cause the left and right margins to be automatically set and they will be equal regardless of how wide your element is.
Update: As pointed out by Phelios, a width should be specified for the div for this approach to work