Search code examples
htmlcsscentertext-alignment

Why is text-align:center not centering all the elements within my DIV?


I have two block elements within a DIV, another DIV and a table. I would like to center them both within the container DIV so I tried

#container-content {
    text-align: center;
}

Here’s the HTML I’m using

<div id="container-content" style="background-color:orange;">
    <h3>My Subscriptions</h3> 

    <table id="subscriptions-table">
        <thead>
            <tr>
                <th>Subscription</th>
                <th>Download</th>
            </tr>
        </thead>
        <tbody>
            <tr class="even subscription-row header">
                    <td class="ig-header-title ellipsis">
                    <img src="/assets/s-icon-0d60471f901d65172728d3df0e793b2ee4493a529c1a1dca73409fdae56ad362.png" alt="S icon">
                    <a class="name ellipsis" href="/scenarios/18">My Scenario #1</a>
                </td>  
                <td align="center"><a href="/scenarios/18/download"><img src="/assets/zip_icon-c2a0694959db12a0939d264d4283478c1f59a4b118df839d7020aca929a1df61.png" alt="Zip icon"></a></td>
            </tr> 
        </tbody>
    </table>
</div>

And here’s the Fiddle that illustrates the problem — https://jsfiddle.net/0nrcfkr5/1/ . How do I center the table within the larger DIV (using CSS)?


Solution

  • text-align will just operate on inline elements. You can center your divs by setting the left and right margins to auto:

    #subscriptions-table {
      margin: 0 auto;
    }
    

    https://jsfiddle.net/0nrcfkr5/2/