How to center a div in CSS

To center a div in CSS you can use the following snippet.

Sample CSS

#divID {
	width: 300px; /* width you need */
	height: 150x; /* height you need */
	position:absolute;
	left:50%;
	top:50%;
	margin-left:-150px; /* half the width */
	margin-top:-75px; /* half the height */
}

Please Share us:
Please rate this snippet

2 thoughts on “How to center a div in CSS

Leave a Reply