For this to work beautifully make sure that margin-top is exactly half of the height and same goes for margin-left which should be exactly half of the width. Also note that CSS does not support decimals.
This images helped me visualise what was going on…