Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to rearrange table td

I have this problem, is there a way to change the order of the td and th with "testing" class such that it will be the first column on mobile but moves to the last column on desktop? thanks.

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First</th>
      <th scope="col">Last</th>
      <th scope="col" class="testing">Handle</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">1</th>
      <td>Mark</td>
      <td>Otto</td>
      <td class="testing">@mdo</td>
   </tr>
   <tr>
      <th scope="row">2</th>
      <td>Jacob</td>
      <td>Thornton</td>
      <td class="testing">@fat</td>
    </tr>
  </tbody>
 </table>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is an example using flex and the css order property. I used the hover property because it’s easier to test than resizing the snippet, but you could change the style from a hover to a media query and check the screen size.

table {
  border-collapse: collapse;
}

td {
  border: 1px solid black;
  padding: 0.2rem 0.6rem;
}

tr {
  display: flex;
}

td:nth-child(0) {
  order: 0;
}

td:nth-child(1) {
  order: 1;
}

td:nth-child(2) {
  order: 2;
}

td:nth-child(3) {
  order: 3;
}

td:nth-child(4) {
  order: 4;
}

td:nth-child(5) {
  order: 5;
}

.fl {
  background-color: lightgreen;
}

.lf {
  background-color: pink;
}

table:hover .fl {
  order: 5;
}

table:hover .lf {
  order: 0;
}
<table>
  <tr>
    <td class="fl">first no-hover, last hover</td>
    <td>second</td>
    <td>third</td>
    <td>fourth</td>
    <td class="lf">first hover, last no-hover</td>
  </tr>
</table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading