6

I have this table that is part of a larger email. When the user's device is a mobile phone, or small screen I want the table to go from 2 columns/2 rows, to 1 column 4 rows. Would like to use the least amount of code possible.

<table style="width:100%;table-layout:fixed;border-collapse:separate !important;border-spacing:20px 40px; font-family:Helvetica, Arial, sans-serif; color:#333333; font-size:16px; line-height:24px; font-weight:100;" class="bodywrapcenter">
  <tr>
    <td>
      Dr. Andrew Borg<br>
      Chapter Advisor<br>
      aborg@example.edu<br>
    </td>
    <td>
      Stuart J. Sigman, PhD<br>
      Dean, College of Arts and Sciences<br>
    </td>
  </tr>
  <tr>
    <td>
      Dr. Dale Mancini<br>
      Chapter Advisor<br>
      dmancini@example.edu<br>
    </td>
    <td>
      David DeHaven, PhD<br>
      Dean, Graduate School of Business and Management<br>
    </td>
  </tr>
</table>
3

4 Answers 4

6

/*
    Max width before this PARTICULAR table gets nasty. This query will take effect for any screen smaller than 760px and also iPads specifically.
    */

@media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px) {
  /* Force table to not be like tables anymore */
  table, thead, tbody, th, td, tr {
    display: block;
  }
  /* Hide table headers (but not display: none;, for accessibility) */
  thead tr {
    position: absolute;
    top: -9999px;
    left: -9999px;
  }
  tr {
    margin: 0 0 1rem 0;
  }
  tr:nth-child(odd) {
    background: #ccc;
  }
  td {
    /* Behave  like a "row" */
    border: none;
    border-bottom: 1px solid #eee;
    position: relative;
    padding-left: 50%;
  }
  td:before {
    /* Now like a table header */
    position: absolute;
    /* Top/left values mimic padding */
    top: 0;
    left: 6px;
    width: 45%;
    padding-right: 10px;
    white-space: nowrap;
  }
  /*
        Label the data
    You could also use a data-* attribute and content for this. That way "bloats" the HTML, this way means you need to keep HTML and CSS in sync. Lea Verou has a clever way to handle with text-shadow.
        */
  td:nth-of-type(1):before {
    content: "First Name";
  }
  td:nth-of-type(2):before {
    content: "Last Name";
  }
  td:nth-of-type(3):before {
    content: "Job Title";
  }
  td:nth-of-type(4):before {
    content: "Favorite Color";
  }
  td:nth-of-type(5):before {
    content: "Wars of Trek?";
  }
  td:nth-of-type(6):before {
    content: "Secret Alias";
  }
  td:nth-of-type(7):before {
    content: "Date of Birth";
  }
  td:nth-of-type(8):before {
    content: "Dream Vacation City";
  }
  td:nth-of-type(9):before {
    content: "GPA";
  }
  td:nth-of-type(10):before {
    content: "Arbitrary Data";
  }
}
<table role="table">
  <thead role="rowgroup">
    <tr role="row">
      <th role="columnheader">First Name</th>
      <th role="columnheader">Last Name</th>
      <th role="columnheader">Job Title</th>
      <th role="columnheader">Favorite Color</th>
      <th role="columnheader">Wars or Trek?</th>
      <th role="columnheader">Secret Alias</th>
      <th role="columnheader">Date of Birth</th>
      <th role="columnheader">Dream Vacation City</th>
      <th role="columnheader">GPA</th>
      <th role="columnheader">Arbitrary Data</th>
    </tr>
  </thead>
  <tbody role="rowgroup">
    <tr role="row">
      <td role="cell">James</td>
      <td role="cell">Matman</td>
      <td role="cell">Chief Sandwich Eater</td>
      <td role="cell">Lettuce Green</td>
      <td role="cell">Trek</td>
      <td role="cell">Digby Green</td>
      <td role="cell">January 13, 1979</td>
      <td role="cell">Gotham City</td>
      <td role="cell">3.1</td>
      <td role="cell">RBX-12</td>
    </tr>
    <tr role="row">
      <td role="cell">The</td>
      <td role="cell">Tick</td>
      <td role="cell">Crimefighter Sorta</td>
      <td role="cell">Blue</td>
      <td role="cell">Wars</td>
      <td role="cell">John Smith</td>
      <td role="cell">July 19, 1968</td>
      <td role="cell">Athens</td>
      <td role="cell">N/A</td>
      <td role="cell">Edlund, Ben (July 1996).</td>
    </tr>
    <tr role="row">
      <td role="cell">Jokey</td>
      <td role="cell">Smurf</td>
      <td role="cell">Giving Exploding Presents</td>
      <td role="cell">Smurflow</td>
      <td role="cell">Smurf</td>
      <td role="cell">Smurflane Smurfmutt</td>
      <td role="cell">Smurfuary Smurfteenth, 1945</td>
      <td role="cell">New Smurf City</td>
      <td role="cell">4.Smurf</td>
      <td role="cell">One</td>
    </tr>
    <tr role="row">
      <td role="cell">Cindy</td>
      <td role="cell">Beyler</td>
      <td role="cell">Sales Representative</td>
      <td role="cell">Red</td>
      <td role="cell">Wars</td>
      <td role="cell">Lori Quivey</td>
      <td role="cell">July 5, 1956</td>
      <td role="cell">Paris</td>
      <td role="cell">3.4</td>
      <td role="cell">3451</td>
    </tr>
    <tr role="row">
      <td role="cell">Captain</td>
      <td role="cell">Cool</td>
      <td role="cell">Tree Crusher</td>
      <td role="cell">Blue</td>
      <td role="cell">Wars</td>
      <td role="cell">Steve 42nd</td>
      <td role="cell">December 13, 1982</td>
      <td role="cell">Las Vegas</td>
      <td role="cell">1.9</td>
      <td role="cell">Under the couch</td>
    </tr>
  </tbody>
</table>

View :https://codepen.io/Waruna/pen/pmEJvg

1
  • This works very well, except for one bugbear, which is the the CSS required for each individual column.
    – crafter
    Aug 7 at 9:18
4

Try using Bootstrap for responsive tables.

The .table-responsive class creates a responsive table. The table will then scroll horizontally on small devices (under 768 px). When viewing on anything larger than 768 px wide.

Use .table-responsive{-sm|-md|-lg|-xl} as needed to create responsive tables up to a particular breakpoint. From that breakpoint and up, the table will behave normally and not scroll horizontally.

2

Give below css to mobile breakpoint in your css code, and you're all done...

table.bodywrapcenter>tr>td {
    width: 100%;
    float: left;
}
2

Try wrapping in a Div

<div style="overflow-x:auto;">
<table>
...
</table>
</div>

From W3Schools - https://www.w3schools.com/howto/howto_css_table_responsive.asp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.