Dave
Dave

Reputation: 2018

Table height content inside moves

I am trying to achieve same look of table as on image, but I can't get it to work. Whenever I add 100% height on table, the elements inside get out of place. How can I make my code to work like on the image? I want the table to default have 100% height with a border, and overflow-x on responsive view. enter image description here

table {
  width: 100%;
  border: 1px solid black;
}

.container {
  width: 100%;
  box-sizing: border-box;
  padding: 0px 10px;
}

.heading > th {
  text-align: left;
}


.name {
  display: flex;
  align-items: center;
}
<div class="container">
<table>
  <tr class="heading">
  <th>Name</th>
  <th>Status</th>
  <th>Rating</th>
  <th>Test</th>
</tr>
  <tr>
    <td class="name">
    <img src="https://via.placeholder.com/80x40" />
    John Johnson</td>
     <td>Free</td>
    <td>Test</td>
    <td>Test</td>

  </tr>
</table>
</div>

Upvotes: 0

Views: 27

Answers (1)

Azu
Azu

Reputation: 1550

Give tr.heading some height and give td vertical-align: top

html, body{
    height:100%;    
}
table {
  width: 100%;
  height: 100%;
  border: 1px solid black;
}

.container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 0px 10px;
}

.heading > th {
  text-align: left;
  height: 20px;
}
td {
    vertical-align: top;
}

.name {
  display: flex;
  align-items: center;
}
<div class="container">
<table>
  <tr class="heading">
  <th>Name</th>
  <th>Status</th>
  <th>Rating</th>
  <th>Test</th>
</tr>
  <tr>
    <td class="name">
    <img src="https://via.placeholder.com/80x40" />
    John Johnson</td>
     <td>Free</td>
    <td>Test</td>
    <td>Test</td>

  </tr>
</table>
</div>

Upvotes: 1

Related Questions