skip to Main Content

My question is similar to How to make a table display rows in two columns?, but actually even more simple:

I want a long, but narrow table to wrap into two columns (when the media has enough space), but the original ordering of rows should be preserved (that’s the difference from the question cited).

I searched a lot, and I tried a lot, but I couldn’t get it working (while it seems it must be easy).

<table>
<tr><td>Cell A1</td><td>Cell B1</td></tr>
<tr><td>Cell A2</td><td>Cell B2</td></tr>
<tr><td>Cell A3</td><td>Cell B3</td></tr>
<tr><td>Cell A4</td><td>Cell B4</td></tr>
<tr><td>Cell A5</td><td>Cell B5</td></tr>
<tr><td>Cell A6</td><td>Cell B6</td></tr>
<tr><td>Cell A7</td><td>Cell B7</td></tr>
<tr><td>Cell A8</td><td>Cell B8</td></tr>
<tr><td>Cell A9</td><td>Cell B9</td></tr>
</table>

Or maybe re-phrase the question:
Why doesn’t it work when I put the table in a <div> that has style column-count: 2 (and some little more tweaking)?

In a text processor like LibreOffice Writer I can define a region with two columns, and the a table inside wraps nicely.

Illustration

Here’s an example from LibreOffice Writer to illustrate what I mean (as some got a wrong idea, it seems):

Screenshot of LibreOffice Writer showing text with a two-column-table inside a two-column region

Actual sample

Here is an actual demo sample I’m trying to fix.
Sorry, but it’s lengthy.

<html xmlns="http://www.w3.org/1999/xhtml">
  <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>(Test)</title>
    <style type="text/css">/*><!--*/
legend { font-family: sans-serif; font-weight: bold }
div.supplier { font-family: sans-serif; font-weight: bold }
div.supplier-notes { font-style: italic }
div.offering { column-count: 2 }
div.offering-notes { font-style: italic }
fieldset { display: inline-block }
span.button-spacer { display: inline-block; width: 2mm }
table#offering { border-style: none; border-width: thin }
th { text-align: left; font-family: sans-serif; }
td.numeric { text-align: right } 
table#offering tbody tr:nth-of-type(even) { background-color: #ccc }
tfoot { font-weight: bolder }
/**/-->
    </style>
  </head>
  <body>
    <div class="supplier">Test</div>
    <div class="supplier-notes">
      <supplier.notes>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
      </supplier.notes>
    </div>
    <div class="offering">
      <form>
    <fieldset class="offering">
      <legend>Angebot / Bestellung</legend>
      <table id="offering">
        <thead>
          <tr id="item.header"><th>Beschreibung</th><th>Preis</th><th>Prod-Nr.</th><th>Anzahl</th><th>Summe</th></tr>
        </thead>
        <tbody>
          <tr id="item-0"><td>ABC-Wärme Hansaplast...</td><td class="numeric">1,23</td><td class="numeric">111</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-1"><td>Kamillin extern</td><td class="numeric">12,34</td><td class="numeric">222</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-2"><td>ASS (Aspirin protect)</td><td class="numeric">3,21</td><td class="numeric">11</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-3"><td>ASS (Aspirin) plus</td><td class="numeric">3,33</td><td class="numeric">1234</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-0"><td>ABC-Wärme Hansaplast...</td><td class="numeric">1,23</td><td class="numeric">111</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-1"><td>Kamillin extern</td><td class="numeric">12,34</td><td class="numeric">222</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-2"><td>ASS (Aspirin protect)</td><td class="numeric">3,21</td><td class="numeric">11</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-3"><td>ASS (Aspirin) plus</td><td class="numeric">3,33</td><td class="numeric">1234</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-0"><td>ABC-Wärme Hansaplast...</td><td class="numeric">1,23</td><td class="numeric">111</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-1"><td>Kamillin extern</td><td class="numeric">12,34</td><td class="numeric">222</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-2"><td>ASS (Aspirin protect)</td><td class="numeric">3,21</td><td class="numeric">11</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
          <tr id="item-3"><td>ASS (Aspirin) plus</td><td class="numeric">3,33</td><td class="numeric">1234</td><td class="numeric"><input value="0" type="number"></td><td class="numeric result" ></td></tr>
        </tbody>
        <tfoot>
          <tr><td colspan="3">Summen</td><td class="numeric result" id="num-total">0</td><td class="numeric result" id="sum-total">0</td></tr>
        </tfoot>
      </table>
    </fieldset>
    <div>
      <input type="button" value="Summieren" onclick="sum_items()">
      <span class="button-spacer"></span>
      <input type="button" value="Drucken" onclick="print_items()">
    </div>
      </form>
    </div>
    <div class="offering-notes">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
    </div>
  </body>
</html>

2

Answers


  1. wrap the <tr> elements in a <div> with the flex display property set to flex-wrap. This will create a responsive two-column layout that maintains the original ordering of the rows.

      .wrapper { display: flex; flex-wrap: wrap; }
      .wrapper table { width: 50%; }
    <div class="wrapper">
      <table>
        <tr><td>Cell A1</td><td>Cell B1</td></tr>
        <tr><td>Cell A2</td><td>Cell B2</td></tr>
        <tr><td>Cell A3</td><td>Cell B3</td></tr>
        <tr><td>Cell A4</td><td>Cell B4</td></tr>
        <tr><td>Cell A5</td><td>Cell B5</td></tr>
        <tr><td>Cell A6</td><td>Cell B6</td></tr>
        <tr><td>Cell A7</td><td>Cell B7</td></tr>
        <tr><td>Cell A8</td><td>Cell B8</td></tr>
        <tr><td>Cell A9</td><td>Cell B9</td></tr>
      </table>
    </div>

    Now when the media has enough space, the table will wrap into two columns. However, if the media is narrow, the table will remain in one column to maintain legibility.

      .wrapper { column-count: 2; }
      .wrapper table { width: 100%; }
      .wrapper tr { display: flex; flex-wrap: wrap; }
    <div class="wrapper">
      <table>
        <tr><td>Cell A1</td><td>Cell B1</td></tr>
        <tr><td>Cell A2</td><td>Cell B2</td></tr>
        <tr><td>Cell A3</td><td>Cell B3</td></tr>
        <tr><td>Cell A4</td><td>Cell B4</td></tr>
        <tr><td>Cell A5</td><td>Cell B5</td></tr>
        <tr><td>Cell A6</td><td>Cell B6</td></tr>
        <tr><td>Cell A7</td><td>Cell B7</td></tr>
        <tr><td>Cell A8</td><td>Cell B8</td></tr>
        <tr><td>Cell A9</td><td>Cell B9</td></tr>
      </table>
    </div>
    Login or Signup to reply.
  2. Using tables, thead, tfoot, seems very unstable, but here’s the sample:
    All I did was target the tbody, and use some display: block on thead and tfoot.

    legend {
      font-family: sans-serif;
      font-weight: bold
    }
    
    div.supplier {
      font-family: sans-serif;
      font-weight: bold
    }
    
    div.supplier-notes {
      font-style: italic
    }
    
    div.offering {
      
    }
    
    div.offering-notes {
      font-style: italic
    }
    
    fieldset {
      display: block
    }
    
    span.button-spacer {
      display: inline-block;
      width: 2mm
    }
    
    table#offering {
      border-style: none;
      border-width: thin
    }
    
    th {
      text-align: left;
      font-family: sans-serif;
    }
    
    td.numeric {
      text-align: right
    }
    
    table#offering tbody tr:nth-of-type(even) {
      background-color: #ccc
    }
    
    tfoot {
      font-weight: bolder
    }
    
    table thead {
      display: block;
    }
    
    table tbody {
     display: block;
     column-count: 2;
    }
    
    table tfoot {
      display: block;
    }
    <div class="supplier">Test</div>
    <div class="supplier-notes">
      <supplier.notes>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
      </supplier.notes>
    </div>
    <div class="offering">
      <form>
        <fieldset class="offering">
          <legend>Angebot / Bestellung</legend>
          <table id="offering">
            <thead>
              <tr id="item.header">
                <th>Beschreibung</th>
                <th>Preis</th>
                <th>Prod-Nr.</th>
                <th>Anzahl</th>
                <th>Summe</th>
              </tr>
            </thead>
            <tbody>
              <tr id="item-0">
                <td>ABC-Wärme Hansaplast...</td>
                <td class="numeric">1,23</td>
                <td class="numeric">111</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-1">
                <td>Kamillin extern</td>
                <td class="numeric">12,34</td>
                <td class="numeric">222</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-2">
                <td>ASS (Aspirin protect)</td>
                <td class="numeric">3,21</td>
                <td class="numeric">11</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-3">
                <td>ASS (Aspirin) plus</td>
                <td class="numeric">3,33</td>
                <td class="numeric">1234</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-0">
                <td>ABC-Wärme Hansaplast...</td>
                <td class="numeric">1,23</td>
                <td class="numeric">111</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-1">
                <td>Kamillin extern</td>
                <td class="numeric">12,34</td>
                <td class="numeric">222</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-2">
                <td>ASS (Aspirin protect)</td>
                <td class="numeric">3,21</td>
                <td class="numeric">11</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-3">
                <td>ASS (Aspirin) plus</td>
                <td class="numeric">3,33</td>
                <td class="numeric">1234</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-0">
                <td>ABC-Wärme Hansaplast...</td>
                <td class="numeric">1,23</td>
                <td class="numeric">111</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-1">
                <td>Kamillin extern</td>
                <td class="numeric">12,34</td>
                <td class="numeric">222</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-2">
                <td>ASS (Aspirin protect)</td>
                <td class="numeric">3,21</td>
                <td class="numeric">11</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
              <tr id="item-3">
                <td>ASS (Aspirin) plus</td>
                <td class="numeric">3,33</td>
                <td class="numeric">1234</td>
                <td class="numeric"><input value="0" type="number"></td>
                <td class="numeric result"></td>
              </tr>
            </tbody>
            <tfoot>
              <tr>
                <td colspan="3">Summen</td>
                <td class="numeric result" id="num-total">0</td>
                <td class="numeric result" id="sum-total">0</td>
              </tr>
            </tfoot>
          </table>
        </fieldset>
        <div>
          <input type="button" value="Summieren" onclick="sum_items()">
          <span class="button-spacer"></span>
          <input type="button" value="Drucken" onclick="print_items()">
        </div>
      </form>
    </div>
    <div class="offering-notes">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit</p>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search