代码之家  ›  专栏  ›  技术社区  ›  Johnrad

HTML-垂直对齐表格[重复]

  •  4
  • Johnrad  · 技术社区  · 15 年前

    可能重复:
    How to Vertically Align a Table in CSS

    我想把我的桌子放在一页的中间。它很容易水平对齐,只需使用

    <table align="center"></table>
    

    但它不允许这样:

    <table align="center" valign="center">
    

    我的想法是让一个单元格占据整个页面,然后使用:

    <Table align="center" width="100%">
    <tr><td valign="middle" height="100%"></td></tr>
    </table>
    

    但这也不起作用,因为我的 <td>

    有什么想法吗?CSS代码也会很有帮助。

    2 回复  |  直到 9 年前
        1
  •  3
  •   rownage    15 年前

    http://www.pmob.co.uk/pob/vertical-center1.htm

    跟随该链接后,右键单击打开的页面,然后查看源代码。

    对于其他浏览器,应该很清楚它们是如何做到的…使用position、valign和text align的组合来达到预期的效果。

    希望这有帮助!

        2
  •  1
  •   Martin Maciaszek    15 年前

    如果将对象的位置设置为绝对位置,则它会起作用。

    下面是一个例子(我只在Safari上试过):

    <html>
      <head>
        <title>Dead Center</title>
        <style type="text/css">
          .dead_center {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            width: 30%;
            height: 30%;
            margin: auto;
            background-color: green;
          }
        </style>
      </head>
      <body>
        <table class="dead_center">
          <tr>
            <td>My centered table</td>
          </tr>
        </table>
      </body>
    </html>
    

    不需要任何不推荐使用的属性,如align。