How to create a responsive table with CSS?


To create a responsive table with CSS, the code is as follows −

Example

 Live Demo

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
   body{
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
   }
   div{
      overflow-x: auto;
   }
   table {
      border-collapse: collapse;
      border-spacing: 0;
      width: 100%;
      border: 1px solid rgb(0, 0, 0);
   }
   th, td {
      text-align: left;
      padding: 8px;
   }
   tr:nth-child(even){background-color: #f2f2f2}
</style>
</head>
<body>
<h1>Responsive Table Example</h1>
<div>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
<th>marks</th>
</tr>
<tr>
<td>JACK</td>
<td>ROY</td>
<td>50</td>
<td>60</td>
<td>10</td>
<td>20</td>
<td>40</td>
<td>50</td>
<td>10</td>
<td>20</td>
<td>30</td>
<td>40</td>
</tr>
<tr>
<td>Evelyn</td>
<td>Monroe</td>
<td>24</td>
<td>14</td>
<td>22</td>
<td>44</td>
<td>55</td>
<td>44</td>
<td>11</td>
<td>55</td>
<td>22</td>
<td>33</td>
</tr>
<tr>
<td>Joe</td>
<td>Anderson</td>
<td>54</td>
<td>22</td>
<td>99</td>
<td>55</td>
<td>91</td>
<td>61</td>
<td>81</td>
<td>11</td>
<td>22</td>
<td>55</td>
</tr>
</table>
</div>
</body>
</html>

Output

The above code will produce the following output −

On resizing the window the scrollbar will appear as follows −

Updated on: 07-May-2020

949 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements