0

我最初的帖子表述不当,因此我决定简化我的问题并再试一次。(任何不便敬请谅解!)

我有一个容器 DIV(固定高度)并且想在里面垂直放置一张桌子。该表具有固定的标题和可滚动的 tbody。我希望 tbody 自动调整到周围容器的高度(减去头)。

例子

.tbody {height: auto;}

不幸的是什么也没做。你知道一些技巧(flexbox?)来实现我的目标吗?

请参阅附件/codepen 作为起点...

代码笔

body {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
}

.container {	/* wrapper element for table */
  width: 20%;
  height: 20%;
  background-color: lightcoral;
  border: 3px solid black;
}

.tableX {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  text-align: left;
  border: 1px dotted black;
}

.tableX thead {
  display: block;
  color: white;
  background-color: darkgray;
}

.tableX tbody {
  display: block;
  overflow-y: auto;
  color: black;
  height: 100px;	/* ----> this is the problem, I don't want a fixed height... */
}

.tableX tr {
  height: 1.5rem;
}
<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <title>TableTerror</title>
  <link rel="stylesheet" href="tableTerror.css">
</head>
<body>
  <div class="container">
    <table class="tableX">
      <thead>
        <tr>
          <th>Company</th>
          <th>Contact</th>
          <th>Country</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>first company</td>
          <td>first name</td>
          <td>first place</td>
        </tr>
        <tr>
          <td>some company</td>
          <td>some name</td>
          <td>some place</td>
        </tr>
        <tr>
          <td>some company</td>
          <td>some name</td>
          <td>some place</td>
        </tr> 
        <tr>
          <td>some company</td>
          <td>some name</td>
          <td>some place</td>
        </tr>
        <tr>
          <td>last company</td>
          <td>last name</td>
          <td>last place</td>
        </tr>
      </tbody>
    </table>
  </div>
</body>
</html>

PS:我不在乎(atm)列宽,滚动条的样式等。

4

1 回答 1

0

我正在尝试复制您的图像。这是您需要的布局吗?

body {
  display: grid;
  place-content: center;
  height: 100vh;
}
.container {
  height: 30vh;
  overflow-y: scroll;
  border: 3px solid black;
}
.table {
  width: 30vw;
  min-width: 240px;
  border-collapse: collapse;
  overflow-y: scroll;
  display: block;
}
.table thead {
  position: absolute;
  background: white;
}
.table thead th {
  padding-right: 10px;
}
.table tbody tr:first-child td {
  padding-top: 30px;
}
.element {
  background: tomato;
  border: 3px solid black;
}
.element + .element {
  border-top: none;
}
<div class="container">
  <table class="table">
    <thead>
      <tr>
        <th>Company</th>
        <th>Contact</th>
        <th>Country</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>company one</td>
        <td>name one</td>
        <td>place one</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>some company</td>
        <td>some name</td>
        <td>some place</td>
      </tr>
      <tr>
        <td>last company</td>
        <td>last name</td>
        <td>last place</td>
      </tr>
    </tbody>
  </table>
</div>

于 2018-12-04T15:49:11.900 回答