Search code examples
htmlcssdeprecated

CSS replacement for <div align="center">


I know this question has been asked many times, but I never saw a satisfactory answer. I mean, an answer that actually works.

So, here we go again. Take this jsFiddle: http://jsfiddle.net/KFMyn/3/

If you remove the align="center" from the HTML, what CSS do you need to use to make the result look the same as the original?

The answers I found usually amount to margin:0 auto and/or text-align:center but neither of those have the desired effect of making the result look the same as the original.


Solution

  • text-align:center covers most of the text elements, but a little extra is needed to centre align a table:

    div {
        width: 400px;
        text-align: center;
    }
    table {
        margin: 0 auto;
    }
    table td {
        text-align: left;
    }
    

    http://jsfiddle.net/NYuv8/4/