skip to Main Content

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!

3

Answers


  1. The trick is to make sure the width is 50% and you need to gravitate to the left, like

        tr {
           float: left;
           width: 50%;
        }
    
        tr {
           float: left;
           width: 50%;
        }
    <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>
    Login or Signup to reply.
  2. 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>
    Login or Signup to reply.
  3. CSS grid can also do the job

    table,
    tbody {
      display: grid;
      justify-content: start;
    }
    
    table tr:nth-child(n + 20) {
      grid-column: 2;
    }
    
    /* styling */
    tr {
      display: grid;
      place-content: center;
      padding: 2px 5px;
      background:  #f2f2f2;
      border:1px solid;
    }
    <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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search