【问题标题】:Html table: last column width autoHtml表:最后一列宽度自动
【发布时间】:2019-04-16 16:01:45
【问题描述】:

我有这个CodePen。 我想要那个

a) 表格以宽度填充父级
b) 最后一列填充空白空间
c) a 列和 b 列固定 (100 & 200)px

table {
  width: 100%;
  table-layout: fixed;
  border: 1px solid lightgray;
}

td {
  overflow: hidden;
  text-overflow: ellipsis;
}

th:nth-child(2);
td:nth-child(2) {
  min-width: 200px;
  max-width: 200px;
}

th:nth-child(1),
td:nth-child(1) {
  min-width: 100px;
  max-width: 100px;
}

thead,
tbody>tr:nth-child(even) {
  background-color: #ffffff;
}

tbody>tr:nth-child(odd) {
  background-color: lightblue;
}

.red {
  background: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-10 col-xs-offset-1 red">
  hello
  <table>
    <thead>
      <tr>
        <th>Id (100px)</th>
        <th>Name (200px)</th>
        <th>Description (auto)</th>
      </tr>
    </thead>
    <tr>
      <td>654</td>
      <td>987</td>
      <td>this is a description</td>
    </tr>
    <tr>
      <td>963</td>
      <td>147</td>
      <td>this is the second description</td>
    </tr>
    <tr>
      <td>753</td>
      <td>951</td>
      <td>this is the third description</td>
    </tr>
  </table>
</div>

【问题讨论】:

  • 您面临什么类型的问题。并请明确你想做什么。

标签: html css layout html-table


【解决方案1】:

使用width 代替max-widthmin-width,您在第二列规则中得到; 而不是,

table {
  width: 100%;
  table-layout: fixed;
  border: 1px solid lightgray;
}

td {
  overflow: hidden;
  text-overflow: ellipsis;
}

th:nth-child(2),
td:nth-child(2) {
  width: 200px;
}

th:nth-child(1),
td:nth-child(1) {
  width: 100px;
}

thead,
tbody>tr:nth-child(even) {
  background-color: #ffffff;
}

tbody>tr:nth-child(odd) {
  background-color: lightblue;
}

.red {
  background: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-xs-10 col-xs-offset-1 red">
  hello
  <table>
    <thead>
      <tr>
        <th>Id (100px)</th>
        <th>Name (200px)</th>
        <th>Description (auto)</th>
      </tr>
    </thead>
    <tr>
      <td>654</td>
      <td>987</td>
      <td>this is a description</td>
    </tr>
    <tr>
      <td>963</td>
      <td>147</td>
      <td>this is the second description</td>
    </tr>
    <tr>
      <td>753</td>
      <td>951</td>
      <td>this is the third description</td>
    </tr>
  </table>
</div>

【讨论】:

  • 也很奇怪,尽管存在引导程序,但没有考虑 col-xs-10
猜你喜欢
  • 1970-01-01
  • 2021-08-29
  • 2014-10-31
  • 2011-05-18
  • 2011-11-22
  • 2014-08-28
  • 1970-01-01
  • 2017-10-24
  • 2019-07-22
相关资源
最近更新 更多