html - 为什么 text-align :center not centering all the elements within my DIV?

标签 html css center text-alignment

我在一个 DIV 中有两个 block 元素,另一个 DIV 和一个表。我想将它们都放在容器 DIV 中,所以我尝试了

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

这是我正在使用的 HTML

<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>

这是说明问题的 fiddle — https://jsfiddle.net/0nrcfkr5/1/ .如何让表格在较大的 DIV 中居中(使用 CSS)?

最佳答案

text-align 只会对行内元素进行操作。您可以通过将左右边距设置为自动来使您的 div 居中:

#subscriptions-table {
  margin: 0 auto;
}

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

关于html - 为什么 text-align :center not centering all the elements within my DIV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41130501/

相关文章:

html - 为什么不能识别父 CSS 类?

html - 使 SVG 适合父容器宽度

jquery - IE 7 中的 CSS 菜单中断

text - 如何在 PostScript 中使页面上的旋转文本居中

html - 使用 CSS 垂直居中 3 个段落

php - HTML 表格到图像

jquery - 爆炸效果jquery不工作

javascript - 防止应用程序拖动但允许在 ios 中滚动内部 div 元素

javascript - 嵌套的 Bootstrap 选项卡无法正确加载内容

css - 在 OnsenUI 中将一个按钮居中放置在另外两个按钮之间