Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 588
Rob C Avatar asked Oct 29 '25 02:10

Rob C


2 Answers

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>
like image 110
Johannes Avatar answered Oct 30 '25 17:10

Johannes


CSS grid can also do the job

table,
tbody {
  display: grid;
  grid-auto-flow: dense;
  justify-content: start;
}
table tr {
  grid-column: 1;
}
table tr:nth-child(n + 11) {
  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>
like image 26
Temani Afif Avatar answered Oct 30 '25 18:10

Temani Afif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!