0

I want to center a div container on a page, in the css I am using

margin-left:auto;

does not seem to work.

Anything goes here

Thanks

X10nD
  • 21,638
  • 45
  • 111
  • 152

3 Answers3

8

This should work:

margin:0px auto;

Or you can even try this if you want:

margin:auto;
position:relative;
width: 600px; /* or whatever width */
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
3

You can center a div horizontally with:

margin: 0 auto;

There's no need for the addition of "px" when you set it to 0.
But: this doesn't work unless you set a width for the div.

So the complete CSS for this would be for example:

width: 500px; 
margin: 0 auto;

This creates a 500px width div, centered horizontally in it's container.

MysticEarth
  • 2,646
  • 4
  • 33
  • 53
1
margin: 0 auto;
width: 100%;
text-align: center;

You never specified if you wanted to center the div vertically or horizontally but this is a good way to center both on different devices.

ECS
  • 19
  • 3