DIV+CSS实现自适应表格布局

举报
AlbertYang 发表于 2021/02/05 23:22:38 2021/02/05
【摘要】 方式1: 效果:  代码: <!DOCTYPE html><html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css"> .customer-page { ba...

方式1:

效果:

 代码:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <style type="text/css">
  7. .customer-page {
  8. background: #FFFFFF;
  9. box-shadow: 0 2px 4px 0 rgba(147, 157, 165, 0.48);
  10. text-align: center;
  11. }
  12. .customer-page-table {
  13. display: table;
  14. width: 100%;
  15. }
  16. .table-row {
  17. display: table-row;
  18. }
  19. .item-content {
  20. display: table-cell;
  21. }
  22. .table-row.header .item-content {
  23. font-weight: 600;
  24. color: #707485;
  25. }
  26. .table-row:not(:last-child) .item-content {
  27. border-bottom: 1px solid #979797;
  28. }
  29. .table-row .item-content {
  30. height: 60px;
  31. text-align: left;
  32. line-height: 60px;
  33. }
  34. @media screen and (max-width: 500px) {
  35. .customer-page-tabl {
  36. display: inline;
  37. }
  38. .table-row {
  39. border: 1px solid #ddd;
  40. margin-bottom: 10px;
  41. display: block;
  42. border-bottom: 2px solid #ddd;
  43. }
  44. .table-row.header {
  45. display: none;
  46. }
  47. .table-row .item-content {
  48. display: block;
  49. text-align: right;
  50. font-size: 13px;
  51. border-bottom: 1px dotted #ccc;
  52. height: 50px;
  53. line-height: 50px;
  54. padding: 0 10px;
  55. }
  56. .table-row .item-content:last-child {
  57. border-bottom: 0;
  58. }
  59. .table-row .item-content:before {
  60. content: attr(data-label);
  61. float: left;
  62. text-transform: uppercase;
  63. font-weight: bold;
  64. }
  65. }
  66. </style>
  67. <title>DIV+CSS实现自适应表格布局</title>
  68. </head>
  69. <body>
  70. <div class="customer-page">
  71. <div class="customer-page-table">
  72. <div class="table-row header">
  73. <div class="item-content">姓名</div>
  74. <div class="item-content">邮箱</div>
  75. <div class="item-content">年龄</div>
  76. <div class="item-content">公司</div>
  77. <div class="item-content">地址</div>
  78. </div>
  79. <div class="table-row body-row" data-id="13">
  80. <div data-label="年龄" class="item-content">13</div>
  81. <div data-label="邮箱" class="item-content">albert@163.com</div>
  82. <div data-label="姓名" class="item-content">sitebuilder</div>
  83. <div data-label="公司" class="item-content">Dec 31, 1969 3:59pm</div>
  84. <div data-label="地址" class="item-content">$6660.00</div>
  85. </div>
  86. <div class="table-row body-row" data-id="13">
  87. <div data-label="年龄" class="item-content">13</div>
  88. <div data-label="邮箱" class="item-content">albert@163.com</div>
  89. <div data-label="姓名" class="item-content">sitebuilder</div>
  90. <div data-label="公司" class="item-content">Dec 31, 1969 3:59pm</div>
  91. <div data-label="地址" class="item-content">$6660.00</div>
  92. </div>
  93. <div class="table-row body-row" data-id="13">
  94. <div data-label="年龄" class="item-content">13</div>
  95. <div data-label="邮箱" class="item-content">albert@163.com</div>
  96. <div data-label="姓名" class="item-content">sitebuilder</div>
  97. <div data-label="公司" class="item-content">Dec 31, 1969 3:59pm</div>
  98. <div data-label="地址" class="item-content">$6660.00</div>
  99. </div>
  100. <div class="table-row body-row" data-id="13">
  101. <div data-label="年龄" class="item-content">13</div>
  102. <div data-label="邮箱" class="item-content">albert@163.com</div>
  103. <div data-label="姓名" class="item-content">sitebuilder</div>
  104. <div data-label="公司" class="item-content">Dec 31, 1969 3:59pm</div>
  105. <div data-label="地址" class="item-content">$6660.00</div>
  106. </div>
  107. </div>
  108. </div>
  109. </body>
  110. </html>

方式二:

代码:


  
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <style type="text/css">
  7. table.responsive{
  8. width:98%;
  9. margin: 0 auto;
  10. color: #000;
  11. border-collapse: collapse;
  12. border: 1px solid #666666;
  13. box-shadow: 0 0 10px rgba(0, 0, 0, 0.4);
  14. }
  15. table.responsive td,table.responsive th{
  16. border: 1px solid #666666;
  17. padding: .5em 1em;
  18. }
  19. table.responsive th {
  20. background: #35B558;
  21. height: 24px;
  22. line-height: 24px;
  23. }
  24. @media (max-width:480px){
  25. table.responsive thead {
  26. display: none;
  27. }
  28. table.responsive tr, table.responsive td {
  29. display: block;
  30. }
  31. table.responsive td{
  32. padding-left: 25%;
  33. }
  34. table.responsive td:nth-child(1)::before {
  35. content:'姓名';
  36. position: absolute;
  37. left: .5em;
  38. font-weight: bold;
  39. }
  40. table.responsive td:nth-child(2)::before {
  41. content:'邮箱';
  42. position: absolute;
  43. left: .5em;
  44. font-weight: bold;
  45. }
  46. table.responsive td:nth-child(3)::before {
  47. content:'年龄';
  48. position: absolute;
  49. left: .5em;
  50. font-weight: bold;
  51. }
  52. table.responsive td:nth-child(4)::before {
  53. content:'公司';
  54. position: absolute;
  55. left: .5em;
  56. font-weight: bold;
  57. }
  58. table.responsive td:nth-child(5)::before {
  59. content:'地址';
  60. position: absolute;
  61. left: .5em;
  62. font-weight: bold;
  63. }
  64. table.responsive tr {
  65. position: relative;
  66. margin-bottom: 1em;
  67. box-shadow: 0 1px 10px rgba(0, 0, 0, 0.2);
  68. }
  69. }
  70. </style>
  71. <title>DIV+CSS实现自适应表格布局</title>
  72. </head>
  73. <body>
  74. <table class="responsive">
  75. <thead>
  76. <tr>
  77. <th>姓名</th>
  78. <th>邮箱</th>
  79. <th>年龄</th>
  80. <th>公司</th>
  81. <th>地址</th>
  82. </tr>
  83. </thead>
  84. <tbody>
  85. <tr>
  86. <td class="name">albert</td>
  87. <td class="email">albert@163.com</td>
  88. <td class="age">13</td>
  89. <td class="company">XX科技网络有限公司</td>
  90. <td class="addres">中国河南</td>
  91. </tr>
  92. <tr>
  93. <td class="name">albert</td>
  94. <td class="email">albert@163.com</td>
  95. <td class="age">13</td>
  96. <td class="company">XX科技网络有限公司</td>
  97. <td class="addres">中国河南</td>
  98. </tr>
  99. <tr>
  100. <td class="name">albert</td>
  101. <td class="email">albert@163.com</td>
  102. <td class="age">13</td>
  103. <td class="company">XX科技网络有限公司</td>
  104. <td class="addres">中国河南</td>
  105. </tr>
  106. <tr>
  107. <td class="name">albert</td>
  108. <td class="email">albert@163.com</td>
  109. <td class="age">13</td>
  110. <td class="company">XX科技网络有限公司</td>
  111. <td class="addres">中国河南</td>
  112. </tr>
  113. <tr>
  114. <td class="name">albert</td>
  115. <td class="email">albert@163.com</td>
  116. <td class="age">13</td>
  117. <td class="company">XX科技网络有限公司</td>
  118. <td class="addres">中国河南</td>
  119. </tr>
  120. </tbody>
  121. </table>
  122. </body>
  123. </html>

 

文章来源: albertyang.blog.csdn.net,作者:Albert Yang,版权归原作者所有,如需转载,请联系作者。

原文链接:albertyang.blog.csdn.net/article/details/105296098

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。