如何对齐表格单元格中心左侧?

时间:2017-11-07 12:39:37

标签: html css html-table

我有一个简单的表结构。

table > tbody > tr > td{
  text-align:center;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet"/>
<table class="table table-hover">
      <thead>
        <tr>
          <td>Client</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td class="col-md-4">
            C 1
          </td>
        </tr>
        <tr>
          <td class="col-md-4">
            Client 2
          </td>
        </tr>
      </tbody>
    </table>

问题是:我如何align td -s内容leftcenter。 ?

输出应该是这样的: enter image description here

4 个答案:

答案 0 :(得分:2)

如果您希望从中心左下方对齐下方细胞,那么只需将50%的填充物添加到您的身体细胞中

&#13;
&#13;
.table tbody td {
  padding-left: 50%;
  text-align;
  left;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet" />
<table class="table table-hover">
  <thead>
    <tr>
      <td>Client</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="col-md-4">
        C 1
      </td>
    </tr>
    <tr>
      <td class="col-md-4">
        Client 2
      </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

如果你希望它们以最大的单词为中心,然后左对齐,你不能为tbody分别设置行,你需要将所有单词包装在内联块div中:

&#13;
&#13;
.col-md-4 {
  text-align: center;
}

.inline-block {
  display: inline-block;
  text-align: left;
}
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet" />
<table class="table table-hover">
  <thead>
    <tr>
      <td>Client</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="col-md-4">
        <div class="inline-block">
          C 1<br> Client 2
        </div>
      </td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个:

td {
  width: 50%;
}
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet"/>
    <table class="table table-hover">
      <thead>
        <tr>
          <td>Client</td>
          <td></td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
          </td>
          <td>
            C 1
          </td>
        </tr>
        <tr>
          <td>
          </td>
          <td>
            Client 2
          </td>
        </tr>
      </tbody>
    </table>

如果您无法添加额外的table-cells,则可以使用padding

.table tbody td {
  padding-left: 50%;
}
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet"/>
    <table class="table table-hover">
      <thead>
        <tr>
          <td>Client</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
            C 1
          </td>
        </tr>
        <tr>
          <td>
            Client 2
          </td>
        </tr>
      </tbody>
    </table>

答案 2 :(得分:1)

td {
  width: 50%;
}
td:last-child {
  border-left:1px solid #333;
}
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet"/>
    <table class="table table-hover">
      <thead>
        <tr>
          <td>Client</td>
          <td></td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>
          </td>
          <td>
            C 1
          </td>
        </tr>
        <tr>
          <td>
          </td>
          <td>
            Client 2
          </td>
        </tr>
      </tbody>
    </table>

答案 3 :(得分:0)

尝试colspan="<number of columns to merge>"rowspan="<number of rows to merge>"合并行或列:

<table>
    <thead>
    <tr>
        <td colspan="2" align="center">Client</td>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td rowspan="2" width="50%"></td>
        <td>
            C 1
        </td>
    </tr>
    <tr>
        <td>
            Client 2
        </td>
    </tr>
    </tbody>
</table>