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

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

  • table    此元素会作为块级表格来显示(类似 <table>),表格前后带有换行符。
  • table-row-group    此元素会作为一个或多个行的分组来显示(类似 <tbody>)。
  • table-header-group    此元素会作为一个或多个行的分组来显示(类似 <thead>)。
  • table-footer-group    此元素会作为一个或多个行的分组来显示(类似 <tfoot>)。
  • table-row    此元素会作为一个表格行显示(类似 <tr>)。
  • table-column-group    此元素会作为一个或多个列的分组来显示(类似 <colgroup>)。
  • table-column    此元素会作为一个单元格列显示(类似 <col>)
  • table-cell    此元素会作为一个表格单元格显示(类似 <td> 和 <th>)
  • table-caption    此元素会作为一个表格标题显示(类似 <caption>)

二、示例代码

1、普通表格

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>display普通表格</title>
<style type="text/css">
.table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;}
.table {display: table; width: 80%; border-collapse: collapse;}
.table-tr {display: table-row; height: 30px;}
.table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;}
.table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;}
</style>
</head>
<body>
<div class="table">
<div class="table-tr">
<div class="table-th">省份/直辖市</div>
<div class="table-th">GDP(亿元)</div>
<div class="table-th">增长率</div>
</div>
<div class="table-tr">
<div class="table-td">广东</div>
<div class="table-td">72812</div>
<div class="table-td">8.0%</div>
</div>
<div class="table-tr">
<div class="table-td">河南</div>
<div class="table-td">37010</div>
<div class="table-td">8.3%</div>
</div>
<div class="table-tr">
<div class="table-td">江苏</div>
<div class="table-td">70116</div>
<div class="table-td">8.5%</div>
</div>
</div>
</body>
</html>

运行效果

2、列合并实现表格

实现思路:基于display:table的表格实现,没有<table>的rowspan和colspan单元格合并的实现,所以曲折实现,将表格每行单独嵌套一个独立的表格,这样在嵌套的独立表格内部,单元格合并就能通过控制嵌套表格的行数和列数以及单元格的宽高来实现

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>基于display列合并表格</title>
<style type="text/css">
.table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;}
.table {display: table; width: 80%; border-collapse: collapse;} .table-tr {display: table-row; height: 30px;}
.table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;}
.table-td {display: table-cell; height: 100%;} .sub-table {width: 100%;height: 100%;display: table;}
.sub-table-tr {display: table-row; height: 100%;}
.sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} </style>
</head>
<body> <div class="table">
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="table-th" style="width: 40%;">省份/直辖市</div>
<div class="table-th" style="width: 30%;">GDP(亿元)</div>
<div class="table-th" style="width: 30%;">增长率</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 40%;">广东</div>
<div class="sub-table-td" style="width: 30%;">72812</div>
<div class="sub-table-td" style="width: 30%;">8.0%</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 40%;">河南</div>
<div class="sub-table-td" style="width: 30%;">37010</div>
<div class="sub-table-td" style="width: 30%;">8.3%</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 40%;">江苏</div>
<div class="sub-table-td" style="width: 30%;">70116</div>
<div class="sub-table-td" style="width: 30%;">8.5%</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 70%;">各省/直辖市GDP平均增长率</div>
<div class="sub-table-td" style="width: 30%;">8.26%</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

运行效果

3、行合并表格

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

 <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>基于display的行合并表格</title>
<style type="text/css">
.table, .table * {margin: 0 auto; padding: 0;font-size: 14px;font-family: Arial, 宋体, Helvetica, sans-serif;}
.table {display: table; width: 80%; border-collapse: collapse;} .table-tr {display: table-row; height: 30px;}
.table-th {display: table-cell;font-weight: bold;height: 100%;border: 1px solid gray;text-align: center;vertical-align: middle;background-color:#E5E5E5;}
.table-td {display: table-cell; height: 100%;} .sub-table {width: 100%;height: 100%;display: table;}
.sub-table-tr {display: table-row; height: 100%;}
.sub-table-td {display: table-cell; height: 100%;border: 1px solid gray; text-align: center;vertical-align: middle;} </style>
</head>
<body> <div class="table">
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="table-th" style="width: 40%;">省份/直辖市</div>
<div class="table-th" style="width: 30%;">GDP(亿元)</div>
<div class="table-th" style="width: 30%;">增长率</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 40%;">广东</div>
<div class="sub-table-td" style="width: 30%;">72812</div>
<div class="sub-table-td" style="width: 30%;">8.0%</div>
</div>
</div>
</div>
</div>
<div class="table-tr" style="height:60px;">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 40%; border: none;">
<div class="sub-table">
<div class="sub-table-tr" style="height:50%;">
<div class="sub-table-td" style="width: 100%; height:50%;">
河南
</div>
</div>
<div class="sub-table-tr" style="height:50%;">
<div class="sub-table-td" style="width: 100%; height:50%;">
江苏
</div>
</div>
</div>
</div>
<div class="sub-table-td" style="width: 30%;border: none;">
<div class="sub-table">
<div class="sub-table-tr" style="height:50%;">
<div class="sub-table-td" style="width: 100%; height:50%;">
37010
</div>
</div>
<div class="sub-table-tr" style="height:50%;">
<div class="sub-table-td" style="width: 100%; height:50%;">
70116
</div>
</div>
</div> </div> <div class="sub-table-td" style="width: 30%;border: none;">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 100%;">
8.4%
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="table-tr">
<div class="table-td">
<div class="sub-table">
<div class="sub-table-tr">
<div class="sub-table-td" style="width: 70%;">各省/直辖市GDP平均增长率</div>
<div class="sub-table-td" style="width: 30%;">8.26%</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>

运行效果:

END

基于CSS属性display:table的表格布局的使用的更多相关文章

  1. CSS:display:table

    使用display:table 垂直居中需要结合display:table-cell; 和vertical-align:middle; <!DOCTYPE html> <html l ...

  2. display:table合并表格

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. CSS属性display的浅略探讨

    display 的属性值有:none|inline|block|inline-block|list-item|run-in|table|inline-table|table-row-group|tab ...

  4. CSS属性Display(显示)和Visibility(可见性)

    隐藏一个元素可以通过把display属性设置为“none”,或把visibility属性设置为“hidden”.但是请注意,这两种方法会产生不同的效果. Visibility:hidden可以隐藏某个 ...

  5. CSS基础学习-6.CSS属性_列表、表格

  6. display属性的表格布局相关属性

    基于CSS属性display:table的表格布局的使用   项目改造中遇到DIV+CSS实现的table,新需求需要在表格使用单元格合并,网上调查返现CSS display:table实现的tabl ...

  7. 用CSS实现“表格布局”

    当我们进行浮动布局时,会发现存在着非浮动元素与浮动元素的底部难以对齐的情况,这就是浮动布局的缺陷.因此,过去的前端工作者曾利用<table>以实现"表格布局".因为表格 ...

  8. 关于css布局的记录(一) --table和flex布局

    1.table方式布局 效果图: 直接用table等标签布局,table布局自动垂直居中 亦可用 display:table == <table>.display:table-cell = ...

  9. CSS属性 table 的 border-collapse 边框合并

    说明 该CSS属性用来设定表格的行和列的边框是合并成单边框,还是分别有各自的边框 separate 缺省值.边框分开,不合并.collapse 边框合并.即如果相邻,则共用同一个边框. 虽然在DIV+ ...

随机推荐

  1. HTTP Status 404(The requested resource is not available)的几种解决方案

    1. 未部署Web应用 2.URL输入错误       排错方法:首先,查看URL的IP地址和端口号是否书写正确.其次,查看上下文路径是否正确 Project--------Properties--- ...

  2. VS2017 调试 Unity3D 脚本

    1. 安装Unity3D最新版本.   https://unity3d.com/cn/get-unity/download 2. 安装Visual Studio Community 2017. htt ...

  3. windows安装mysql8

    1:首先去官网下载安装包 下载地址:https://dev.mysql.com/downloads/mysql/ 2:将解压文件解压到你安装的目录:E:\mysql-8.0.11-winx64  (我 ...

  4. NB-Iot的应用领域、覆盖范围,是什么

    该部分分享的是物联网各垂直应用领域里,NB-IoT技术的部署,看看适合NB-IoT技术的垂直应用场景有哪些?垂直应用服务商又该如何部署? 1 NB-IoT适合的垂直应用场景有哪些? 2 NB-IoT垂 ...

  5. cs231n(三) 误差反向传播

    摘要 本节将对反向传播进行直观的理解.反向传播是利用链式法则递归计算表达式的梯度的方法.理解反向传播过程及其精妙之处,对于理解.实现.设计和调试神经网络非常关键.反向求导的核心问题是:给定函数 $f( ...

  6. 本地上传文件至服务器的技巧(linux文件压缩及解压文件)

    linux(ubuntu)文件解压及压缩文件 ubuntu支持文件的解压及压缩功能, 如果ubuntu上面没有安装过unzip工具的话,可以通过下面命令安装: sudo apt-get install ...

  7. 基于nmap扫描结果的端口爆破工具:BrutesPray

      大家搞内网或者C段渗透测试的时候可能遇到很多时候需要对大批的主机进行精确爆破,这时候BruteSpray就派上用场了. BruteSpray是一款基于nmap扫描输出的gnmap/XML文件.自动 ...

  8. C++ 中文拼音排序方法。

    参考文档:http://zisxks.com/2013/10/25/sort-Chinese-characters-in-cpp/ 采用locate.注意事项:排序的名字,如果出现某一个人,出现在顶上 ...

  9. mousedown、mousemove、mouseup和touchstart、touchmove、touchend

    拖动时候用到的三个事件:mousedown.mousemove.mouseup在移动端都不起任何作用.毕竟移动端是没有鼠标的,查资料后发现,在移动端与之相对应的分别是:touchstart.touch ...

  10. 限时免费 | 12月6日,广州保利洲际酒店,ABC Summit 2018云智峰会来了!

    随着科技的迅猛发展,人工智能技术也逐渐取得了各个突破.自20世纪70年代以来,作为计算机学科的一个分支,人工智能就被列为世界三大尖端技术之一.近年来,阿尔法狗战胜世界第一柯洁,使人工智能再度迎来新的热 ...