Table of Contents
一、CSS display属性的表格布局相关属性的解释:
二、示例代码
1、普通表格
2、列合并实现表格
3、行合并表格
Home Web Front-end HTML Tutorial 基于CSS属性display:table的表格布局的使用_html/css_WEB-ITnose

基于CSS属性display:table的表格布局的使用_html/css_WEB-ITnose

Jun 24, 2016 am 11:23 AM

项目改造中遇到DIV+CSS实现的table,新需求需要在表格使用单元格合并,网上调查返现CSS display:table实现的table表格,没有单元格的属性和样式,经过一番思考,曲折现实了单元格的合并,即采用正行嵌套一个单独的display:table的DIV,然后在嵌套的表格DIV内部通过控制行列数和行列的高度,实现单元格合并。个人建议全新实现使用

HTML标签即可

一、CSS display属性的表格布局相关属性的解释:

  • table    此元素会作为块级表格来显示(类似
  • ),表格前后带有换行符。
  • table-row-group    此元素会作为一个或多个行的分组来显示(类似
  • )。
  • table-header-group    此元素会作为一个或多个行的分组来显示(类似
  • )。
  • table-footer-group    此元素会作为一个或多个行的分组来显示(类似
  • )。
  • table-row    此元素会作为一个表格行显示(类似
  • )。
  • table-column-group    此元素会作为一个或多个列的分组来显示(类似
  • )。
  • table-column    此元素会作为一个单元格列显示(类似
  • table-cell    此元素会作为一个表格单元格显示(类似
  • 二、示例代码

    1、普通表格

     1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>display普通表格</title> 6 <style type="text/css"> 7 .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;} 8 .table {display: table; width: 80%; border-collapse: collapse;} 9 .table-tr {display: table-row; height: 30px;}10 .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;}11 .table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;}12 </style>13 </head>14 <body>15     <div class="table">16         <div class="table-tr">17             <div class="table-th">省份/直辖市</div>18             <div class="table-th">GDP(亿元)</div>19             <div class="table-th">增长率</div>20         </div>21         <div class="table-tr">22             <div class="table-td">广东</div>23             <div class="table-td">72812</div>24             <div class="table-td">8.0%</div>25         </div>26         <div class="table-tr">27             <div class="table-td">河南</div>28             <div class="table-td">37010</div>29             <div class="table-td">8.3%</div>30         </div>31         <div class="table-tr">32             <div class="table-td">江苏</div>33             <div class="table-td">70116</div>34             <div class="table-td">8.5%</div>35         </div>36     </div>37 </body>38 </html>
    Copy after login

    运行效果


    2、列合并实现表格

    实现思路:基于display:table的表格实现,没有

  • table-caption    此元素会作为一个表格标题显示(类似
  • 的rowspan和colspan单元格合并的实现,所以曲折实现,将表格每行单独嵌套一个独立的表格,这样在嵌套的独立表格内部,单元格合并就能通过控制嵌套表格的行数和列数以及单元格的宽高来实现

     1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>基于display列合并表格</title> 6 <style type="text/css"> 7 .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;} 8 .table {display: table; width: 80%; border-collapse: collapse;} 9 10 .table-tr {display: table-row; height: 30px;}11 .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;}12 .table-td {display: table-cell; height: 100%;}13 14 .sub-table {width: 100%;height: 100%;display: table;}15 .sub-table-tr {display: table-row; height: 100%;}16 .sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;}17 18 </style>19 </head>20 <body>21 22 <div class="table">23     <div class="table-tr">24         <div class="table-td">25             <div class="sub-table">26                 <div class="sub-table-tr">27                     <div class="table-th" style="width: 40%;">省份/直辖市</div>28                     <div class="table-th" style="width: 30%;">GDP(亿元)</div>29                     <div class="table-th" style="width: 30%;">增长率</div>30                 </div>31             </div>32         </div>33     </div>34     <div class="table-tr">35         <div class="table-td">36             <div class="sub-table">37                 <div class="sub-table-tr">38                     <div class="sub-table-td" style="width: 40%;">广东</div>39                     <div class="sub-table-td" style="width: 30%;">72812</div>40                     <div class="sub-table-td" style="width: 30%;">8.0%</div>41                 </div>42             </div>43         </div>44     </div>45     <div class="table-tr">46         <div class="table-td">47             <div class="sub-table">48                 <div class="sub-table-tr">49                     <div class="sub-table-td" style="width: 40%;">河南</div>50                     <div class="sub-table-td" style="width: 30%;">37010</div>51                     <div class="sub-table-td" style="width: 30%;">8.3%</div>52                 </div>53             </div>54         </div>55     </div>56     <div class="table-tr">57         <div class="table-td">58             <div class="sub-table">59                 <div class="sub-table-tr">60                     <div class="sub-table-td" style="width: 40%;">江苏</div>61                     <div class="sub-table-td" style="width: 30%;">70116</div>62                     <div class="sub-table-td" style="width: 30%;">8.5%</div>63                 </div>64             </div>65         </div>66     </div>67     <div class="table-tr">68         <div class="table-td">69             <div class="sub-table">70                 <div class="sub-table-tr">71                     <div class="sub-table-td" style="width: 70%;">各省/直辖市GDP平均增长率</div>72                     <div class="sub-table-td" style="width: 30%;">8.26%</div>73                 </div>74             </div>75         </div>76     </div>77 </div>78 </body>79 </html>
    Copy after login

    运行效果


    3、行合并表格

    行合并的实现思路:与列合并的实现思路类似,将有单元格合并的列单独嵌套一个display为table的DIV,高度=单行高*单元格合并数目的倍数,同行的其他列同样均单独嵌套DIV,实例代码如下

      1 <!DOCTYPE html>  2 <html>  3 <head>  4 <meta charset="UTF-8">  5 <title>基于display的行合并表格</title>  6 <style type="text/css">  7 .table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;}  8 .table {display: table; width: 80%; border-collapse: collapse;}  9  10 .table-tr {display: table-row; height: 30px;} 11 .table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;} 12 .table-td {display: table-cell; height: 100%;} 13  14 .sub-table {width: 100%;height: 100%;display: table;} 15 .sub-table-tr {display: table-row; height: 100%;} 16 .sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} 17  18 </style> 19 </head> 20 <body> 21  22 <div class="table"> 23     <div class="table-tr"> 24         <div class="table-td"> 25             <div class="sub-table"> 26                 <div class="sub-table-tr"> 27                     <div class="table-th" style="width: 40%;">省份/直辖市</div> 28                     <div class="table-th" style="width: 30%;">GDP(亿元)</div> 29                     <div class="table-th" style="width: 30%;">增长率</div> 30                 </div> 31             </div> 32         </div> 33     </div> 34     <div class="table-tr"> 35         <div class="table-td"> 36             <div class="sub-table"> 37                 <div class="sub-table-tr"> 38                     <div class="sub-table-td" style="width: 40%;">广东</div> 39                     <div class="sub-table-td" style="width: 30%;">72812</div> 40                     <div class="sub-table-td" style="width: 30%;">8.0%</div> 41                 </div> 42             </div> 43         </div> 44     </div> 45     <div class="table-tr" style="height:60px;"> 46         <div class="table-td"> 47             <div class="sub-table"> 48                 <div class="sub-table-tr"> 49                     <div class="sub-table-td" style="width: 40%; border: none;"> 50                         <div class="sub-table"> 51                             <div class="sub-table-tr" style="height:50%;"> 52                                 <div class="sub-table-td" style="width: 100%; height:50%;"> 53                                     河南 54                                 </div> 55                             </div> 56                             <div class="sub-table-tr" style="height:50%;"> 57                                 <div class="sub-table-td" style="width: 100%; height:50%;"> 58                                     江苏 59                                 </div> 60                             </div> 61                         </div> 62                     </div> 63                     <div class="sub-table-td" style="width: 30%;border: none;"> 64                         <div class="sub-table"> 65                             <div class="sub-table-tr" style="height:50%;"> 66                                 <div class="sub-table-td" style="width: 100%; height:50%;"> 67                                     37010 68                                 </div> 69                             </div> 70                             <div class="sub-table-tr" style="height:50%;"> 71                                 <div class="sub-table-td" style="width: 100%; height:50%;"> 72                                     70116 73                                 </div> 74                             </div> 75                         </div> 76                      77                     </div> 78                      79                     <div class="sub-table-td" style="width: 30%;border: none;"> 80                         <div class="sub-table"> 81                             <div class="sub-table-tr"> 82                                 <div class="sub-table-td" style="width: 100%;"> 83                                     8.4% 84                                 </div> 85                             </div> 86                         </div> 87                     </div> 88                 </div> 89             </div> 90         </div> 91     </div> 92     <div class="table-tr"> 93         <div class="table-td"> 94             <div class="sub-table"> 95                 <div class="sub-table-tr"> 96                     <div class="sub-table-td" style="width: 70%;">各省/直辖市GDP平均增长率</div> 97                     <div class="sub-table-td" style="width: 30%;">8.26%</div> 98                 </div> 99             </div>100         </div>101     </div>102 </div>103 </body>104 </html>
    Copy after login

     

    运行效果:

     

    END

     

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

    GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

    See all articles