Search code examples
html-tablemedia-queriescellresponsive

Responsive table with td which go under one another


I want to make my table responsive but I don't know how to use media-queries, can somebody help me? My goal is to make my td act like blocks, going under one another if the screen size is smaller.

Current code:

td{border:1px dotted red;background-color:red;color:white;}

p{font-family:'Varela Round';font-weight:bold;text-align:center;}
<table width="100%" cellpadding="0" cellspacing="5">
  <tbody>
    <tr>
      <td><p>SOCIETES</p></td>
      <td><p>CONTACT</p></td>
      <td><p>EMAIL NOMINATIF</p></td>
      <td><p>OPT OUT</p></td>
      <td><p>LIGNES DIRECTES/MOBILES</p></td>
    </tr>
  </tbody>
</table>


Solution

  • You can change the td from display:block to display:table-cell at a minimum width using media queries for a mobile first approach.

    td{
      display:block;
      width:auto;
      border:1px dotted red;
      background-color:red;
      color:white;
      margin-bottom:10px;
    
    }
    
    @media only screen and (min-width: 70em) {
      td{
        display:table-cell;
        border:1px dotted red;
        background-color:red;
        color:white;
        margin-bottom:0px;
      }
    }
    
    
    p{font-family:'Varela Round';font-weight:bold;text-align:center;}
    <table width="100%" cellpadding="0" cellspacing="5">
      <tbody>
        <tr>
          <td><p>SOCIETES</p></td>
          <td><p>CONTACT</p></td>
          <td><p>EMAIL NOMINATIF</p></td>
          <td><p>OPT OUT</p></td>
          <td><p>LIGNES DIRECTES/MOBILES</p></td>
        </tr>
      </tbody>
    </table>