htmlcsshtml-tablecss-tables

Split headerless html table rows 50% left and 50% right


I have 20 rows like this in a standard html table (table>tbody>tr>td) without a header. current situation

<table>
    <tbody>
        <tr>
            <td>1</td>
        </tr>
        <tr>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
        </tr>
        <tr>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
        </tr>
        <tr>
            <td>6</td>
        </tr>
        <tr>
            <td>7</td>
        </tr>
        <tr>
            <td>8</td>
        </tr>
        <tr>
            <td>9</td>
        </tr>
        <tr>
            <td>10</td>
        </tr>
        <tr>
            <td>11</td>
        </tr>
        <tr>
            <td>12</td>
        </tr>
        <tr>
            <td>13</td>
        </tr>
        <tr>
            <td>14</td>
        </tr>
        <tr>
            <td>15</td>
        </tr>
        <tr>
            <td>16</td>
        </tr>
        <tr>
            <td>17</td>
        </tr>
        <tr>
            <td>18</td>
        </tr>
        <tr>
            <td>19</td>
        </tr>
        <tr>
            <td>20</td>
        </tr>
    </tbody>
</table>

I would like to render this like: desired result

When i use flexbox it will sort 1,2 next line 3,4 etc.

I found loads of results on how to do other things, but not this. I remember it could be done without javascript and without modifying the html - and it was a pure css-thing couple lines long, but can't find that doc anymore. Also searched on stackoverflow, nowhere to be found. If anyone can put me into the correct direction - thanks!


Solution

  • Here is something that comes close, using CSS columns and display parameters different from the original (table) ones:

    table {
      display: block;
    }
    tbody {
      display: block;
      width: 120px;
      column-count: 2;
      column-gap: 0;
    }
    tr {
      display: block;
      width: 60px;
      text-align: center;
      border: 1px solid #ccc;
    }
    td {
      display: inline;
    }
    <table>
      <tbody>
        <tr>
          <td>1</td>
        </tr>
        <tr>
          <td>2</td>
        </tr>
        <tr>
          <td>3</td>
        </tr>
        <tr>
          <td>4</td>
        </tr>
        <tr>
          <td>5</td>
        </tr>
        <tr>
          <td>6</td>
        </tr>
        <tr>
          <td>7</td>
        </tr>
        <tr>
          <td>8</td>
        </tr>
        <tr>
          <td>9</td>
        </tr>
        <tr>
          <td>10</td>
        </tr>
        <tr>
          <td>11</td>
        </tr>
        <tr>
          <td>12</td>
        </tr>
        <tr>
          <td>13</td>
        </tr>
        <tr>
          <td>14</td>
        </tr>
        <tr>
          <td>15</td>
        </tr>
        <tr>
          <td>16</td>
        </tr>
        <tr>
          <td>17</td>
        </tr>
        <tr>
          <td>18</td>
        </tr>
        <tr>
          <td>19</td>
        </tr>
        <tr>
          <td>20</td>
        </tr>
      </tbody>
    </table>