0

I have a task where I am trying to align four tables in a page which is then printed. But for some reason I am unable to align them with same height. I have a constraint to use only <table> <tr><td> structure.

Below is the current picture, because the data is variant in each table:

enter image description here

And I am trying to achieve is the below because no matter which table has how much data, all of them should be of equal height:

enter image description here

Any help would be great.

1

3 Answers 3

2

Take a look at flexboxes:

.wrapper {
  display: flex;
}

table {
  display: flex;
  margin-left: 5px;
  border: 1px solid #000000;
}
<div class="wrapper">

  <table>
    <tr>
      <td>Foo</td>
     </tr>
  </table>
  
  <table>
    <tr>
      <td>Foo</td>
     </tr>
     <tr>
      <td>Foo</td>
     </tr>
     <tr>
      <td>Foo</td>
     </tr>
  </table>
  
  <table>
    <tr>
      <td>Foo</td>
     </tr>
     <tr>
      <td>Foo</td>
     </tr>
  </table>

</div>

Sign up to request clarification or add additional context in comments.

Comments

1

Get each table's total number of rows. Then get the maximum from these numbers :

<script type="text/javascript">
function max(tab_num_rows) { // you put the numbers into an array
var ret = tab_num_rows[0];
for (var i=1 ; i<tab_num_rows.length; i++) {
   if (ret < tab_num_rows[i])
       ret = tab_num_rows[i];
}
return ret;
}
</script>

Then you make a JQuery to loop each table to add extra rows until the maximum is reached.

Comments

1

Flexbox will be the typical solution for this (see the other answer), however, if you are also restricted to NOT use flexbox (for example because of compatibility requirements to older browsers), you can put all four tables into the cells of one "wrapper table", like

.wrap {
  width: 100%;
}

td {
  background: #eee;
  vertical-align: top;
}
<table class="wrap">
  <tr>
    <td>
      <table>
        <tr>
          <td>One</td>
        </tr>
        <tr>
          <td>One</td>
        </tr>
        <tr>
          <td>One</td>
        </tr>
        <tr>
          <td>One</td>
        </tr>
      </table>
    </td>
    <td>
      <table>
        <tr>
          <td>Two</td>
        </tr>
        <tr>
          <td>Two</td>
        </tr>
        <tr>
          <td>Two</td>
        </tr>
      </table>
    </td>
    <td>
      <table>
        <tr>
          <td>Three</td>
        </tr>
        <tr>
          <td>Three</td>
        </tr>
      </table>
    </td>
    <td>
      <table>
        <tr>
          <td>Four</td>
        </tr>
        <tr>
          <td>Four</td>
        </tr>
        <tr>
          <td>Four</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

1 Comment

@pheromix Forgot about that. now they are - I added vertical-align: top for the cells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.