【问题标题】:Table Thead be full size of browser with centered tbodyTable Thead 是浏览器的全尺寸,tbody 居中
【发布时间】:2014-05-01 02:50:22
【问题描述】:

您好,我正在尝试实现如下所示:

因此,thead 是屏幕宽度的 100%,但 tbody 居中。

我有以下标记:

<table>
  <thead>
    <tr>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
    </tr>
  </tbody>
</table>

我还做了一个JSFiddle:http://jsfiddle.net/3KYWL/

编辑:对不起,我应该提到,因为我使用的是数据表 jQuery 插件,所以我必须:

  1. 只有一张桌子
  2. thead 和 tbody 中的列数相同

【问题讨论】:

  • 好像你只需要在左右两边多加一列。
  • @j08691,对不起,我忘了说我需要让 thead 和 tbody 中的列数量相同。

标签: html css


【解决方案1】:

请看下面的jsfiddle。 http://jsfiddle.net/3KYWL/13/

HTML:

<table class="centered">
  <thead>
    <tr>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
      <th>Test</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
      <td>Test</td>
    </tr>
  </tbody>
</table>

CSS:

table { 
    border-spacing: 0;
    border-collapse: collapse;
}

.centered{
    margin:auto;
    width:100%;
}

thead th, tbody td {
    padding:15px;
    text-align:left;
}

thead {
    background:green;
    display: table;
    width: 100%;
}

thead tr {
     width: 75%;
     display: table;
     margin: 0 auto;
}

tbody {
    width: 75%;
    display: table;
    background:aqua;
    margin: 0 auto;
}

结果:

【讨论】:

  • 这不是把列排列起来吗?另外,我应该在问题中提到我需要让它成为一个表,因为我在渲染它之前在表上使用了 datatables jQuery 插件。
  • @the_ 好的。这应该有效。 jsfiddle.net/3KYWL/12 我刚刚将 display: table 属性添加到您的一些元素中。 :)
  • 感谢您的帮助,但标题不再是全宽了吗?
  • @the_ 哎呀。只需将 centered 类的宽度改回 100%(您最初的宽度为 700px)。现在可以了。 jsfiddle.net/3KYWL/13
猜你喜欢
  • 2012-04-04
  • 2021-07-19
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
  • 2012-08-04
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
相关资源
最近更新 更多