How to center a div in CSS

Centering a div can be of crucial importance to position various HTML elements. By means of a handy CSS trick you can easily and quickly center a div making sure this div is displayed as a centered div in all browsers. To center a div a combined CSS property width and margin needs to be assigned. It is of crucial importance to first determine the width of the div after which you can indicate by means of the margin that you would like the left and right margin to be automated (to set them on auto) in order to get them equalized.  Please find below a practical example involving a class container with a 960px width and a vertical centration:

.container{
width: 960px;
margin: 0 auto;
}


Was this useful?