Centering the outer div, "text-align: center" does not work as expected

I have a content section in a div, and currently it is in the center on the left. Fiddle

I tried adding a container div like:

<div style='text-align:center; width:100px;'>[content]</div> 

But no luck. Also tried this:

 <div style='margin: 0px auto; width:100px;'>[content]</div> 

Content is still left-aligned.

This works, but I would like to find the right way to do this using CSS:

 <div style='width:100px;' align='center'>[content]</div> 

Help was appreciated. I am tempted to do this with tables, but I need to break free from old habits!

+8
html css centering
source share
3 answers

If you try to get the external trust of the div to its parent ( body in this case), you should set its side fields to auto:

 body div{ margin: auto; } 

as you can see here

+9
source share

The tool to center the div is actually margin: 0 auto; .

+3
source share

Wrap the content with another div inside the rounded corners.

And write style="width: 100%; margin: 0 auto;" for this new div

0
source share

All Articles