【问题标题】:How to position a table at the center of div horizontally & vertically如何将表格水平和垂直放置在div的中心
【发布时间】:2011-10-26 22:15:17
【问题描述】:

我们可以将图片设置为<div>的背景图片,比如:

<style>
    #test { 

        background-image: url(./images/grad.gif); 
        background-repeat: no-repeat;
        background-position: center center; 

        width:80%; 
        margin-left:10%; 
        height:200px; 
        background-color:blue;

    }

</style>

<div id="test"></div>

我需要在&lt;div&gt; 的中心水平和垂直设置一个表格。有没有使用CSS的跨浏览器解决方案?

【问题讨论】:

    标签: css


    【解决方案1】:

    居中是 CSS 中最大的问题之一。但是,存在一些技巧:

    要将表格水平居中,您可以将左右边距设置为自动:

    <style>
      #test {
        width:100%;
        height:100%;
      }
      table {
        margin: 0 auto; /* or margin: 0 auto 0 auto */
      }
    </style>
    

    要垂直居中,唯一的方法是使用javascript:

    var tableMarginTop = Math.round( (testHeight - tableHeight) / 2 );
    $('table').css('margin-top', tableMarginTop) # with jQuery
    $$('table')[0].setStyle('margin-top', tableMarginTop) # with Mootools
    

    vertical-align:middle 是不可能的,因为表格是块而不是内联元素。

    编辑

    这里是一个总结 CSS 居中解决方案的网站:http://howtocenterincss.com/

    【讨论】:

    • 傻表。谢谢,这(边距:0 自动)也帮助了我。
    • 这里的testHeight 是什么?
    • @AjayKulkarni #test 元素的高度。很抱歉造成混乱。
    • 是的,我想通了......请详细说明javascriptjQueryAJAX等,以便其他人理解
    • 正是我需要的!几个小时以来,我一直在摆弄填充、边距!
    【解决方案2】:

    这对我有用:

    #div {
      display: flex;
      justify-content: center;
    }
    
    #table {
      align-self: center;
    }
    

    这使它垂直和水平对齐。

    【讨论】:

    • 这一定是最佳答案!
    【解决方案3】:

    要水平居中,您可以说width: 50%; margin: auto;。据我所知,这是跨浏览器。对于垂直对齐,您可以尝试vertical-align:middle;,但它可能仅适用于文本。不过值得一试。

    【讨论】:

      【解决方案4】:

      只需将margin: 0 auto; 添加到您的table。无需为div添加任何属性

      <div style="background-color:lightgrey">
       <table width="80%" style="margin: 0 auto; border:1px solid;text-align:center">
          <tr>
            <th>Name </th>
            <th>Country</th>
          </tr>
          <tr>
            <td>John</td>
            <td>US </td>
          </tr>
          <tr>
            <td>Bob</td>
            <td>India </td>
          </tr>
       </table>
      <div>

      注意:为 div 添加背景颜色以可视化表格与其中心的对齐方式

      【讨论】:

        【解决方案5】:

        此外,如果您想水平和垂直居中 -而不是采用流式设计(在这种情况下,适用之前的解决方案)- 您可以这样做:

        1. 将主div声明为absoluterelative定位(我称之为content)。
        2. 声明一个内部 div,它将实际保存表格(我称之为wrapper)。
        3. wrapper div 中声明表本身。
        4. 应用 CSS 转换将包装对象的“注册点”更改为其中心。
        5. 将包装对象移动到父对象的中心。

        #content {
          width: 5em;
          height: 5em;
          border: 1px solid;
          border-color: red;
          position: relative;
          }
        
        #wrapper {
          width: 4em;
          height: 4em;
          border: 3px solid;
          border-color: black;
          position: absolute;
          left: 50%; top: 50%; /*move the object to the center of the parent object*/
          -webkit-transform: translate(-50%, -50%);
          -moz-transform: translate(-50%, -50%);
          -ms-transform: translate(-50%, -50%);
          -o-transform: translate(-50%, -50%);
          transform: translate(-50%, -50%);
          /*these 5 settings change the base (or registration) point of the wrapper object to it's own center - so we align child center with parent center*/
          }
        
        table {
          width: 100%;
          height: 100%;
          border: 1px solid;
          border-color: yellow;
          display: inline-block;
          }
        <div id="content">
            <div id="wrapper">
                <table>
                </table>
            </div>
        </div>

        注意:你不能去掉包装器 div,因为这种样式不能直接在表格上工作,所以我使用 div 来包装它并定位它,而表格在 div 内流动。

        【讨论】:

          【解决方案6】:

          您可以使用以下技术将框垂直和水平居中:

          外部容器:

          • 应该有display: table;

          内部容器:

          • 应该有display: table-cell;
          • 应该有vertical-align: middle;
          • 应该有text-align: center;

          内容框:

          • 应该有display: inline-block;

          如果您使用这种技术,只需将您的表格(以及您想要使用的任何其他内容)添加到内容框中。

          演示:

          body {
              margin : 0;
          }
          
          .outer-container {
              position : absolute;
              display: table;
              width: 100%;
              height: 100%;
              background: #ccc;
          }
          
          .inner-container {
              display: table-cell;
              vertical-align: middle;
              text-align: center;
          }
          
          .centered-content {
              display: inline-block;
              background: #fff;
              padding : 20px;
              border : 1px solid #000;
          }
          <div class="outer-container">
             <div class="inner-container">
               <div class="centered-content">
                  <em>Data :</em>
                  <table>
                      <tr>
                          <th>Name</th>
                          <th>Age</th>
                      </tr>
                      <tr>
                          <td>Tom</td>
                          <td>15</td>
                      </tr>
                      <tr>
                          <td>Anne</td>
                          <td>15</td>
                      </tr>
                      <tr>
                          <td>Gina</td>
                          <td>34</td>
                      </tr>
                  </table>
               </div>
             </div>
          </div>

          另见this Fiddle

          【讨论】:

            【解决方案7】:

            我发现我必须包含

            body { width:100%; }
            

            “margin: 0 auto”适用于表格。

            【讨论】:

              【解决方案8】:

              由于我还不能发表评论,我将在此处将我的评论发布到jdlin。如果您想在不设置表格本身样式的情况下完成此操作,您可以这样做:

              #div 
              {
                  display: flex;
                  justify-content: center;
                  align-items: center;
              }
              

              【讨论】: