gte_ gte_ - 1 year ago 104
HTML Question

How to horizontally center table inside div?

I have a

inside a
, with the styling below, the table is not being centered, why?

Shouldn't everything within the
be formatted like I styled in the CSS?




border: 5px solid white;
border-radius: 5px;
margin: auto;
text-align: center;

Answer Source

You are setting margin: auto on the div, so the div will be centred … however you have also left the div as width: auto so it will be as wide its container (once you account for margins, padding and borders).

You are setting text-align: center on the div, so the inline children of the div will be centred, but the table is not inline, so it isn't affected (some of the table cells contents might be, depending on inheritance).

If you want to centre the table then you have to centre the table itself.

table { margin: auto; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download