如何使用 CSS 创建响应式表格?
要使用 CSS 创建响应式表格,代码如下 -
示例
<!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>
输出
以上代码将产生以下输出 -
当调整窗口大小时,将出现滚动条,如下所示 -
广告