Table with fixed headers and tbody autoheight

92 Views Asked by At

My initial post was poorly formulated, therefor I decided to simplify my question and try it again. (Sorry for any inconvenience!)

I have a container DIV (fixed height) and want to vertically fit a table inside. The table has fixed headers and a scrollable tbody. I want the tbody to automatically adjust to the height of the surrounding container (minus the thead).

example

.tbody {height: auto;}

Does nothing unfortunately. Do you know of some trickery (flexbox?) to achieve my goal?

Please see the attached files/codepen as a starting point...

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: I don't care (atm) for column-width, the style of the scrollbar etc.

1

There are 1 best solutions below

5
On

I'm trying to replicate your image. Is this the layout that you need?

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>