热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

关于html表格单元格宽度的计算规则

*{margin:0;padding:0}body{background:#fafafa}ul,li{list-style:none}h1{margin:20px0
关于表格宽度的渲染规则

表格单元格宽度的计算方式主要分为两种方式:固定表格布局、自动表格布局,这个经常写css的人应该还是知道的,但是我们经常会发现给表格列定了宽度不起作用,又或是没有定宽度渲染出来的却是正常的吗,下面就来介绍下这两个方式具体是怎么计算渲染的。

先设定几个通用的变量:

  • tableWidth=表格宽度=100%
  • tableBorderWidth=表格左右边框宽度
  • tdBorderWidth=所有列左右边框宽度和(合并的边框算1px)
  • tdPadding=所有列左右内填补和
  • tdWidth=所有定义了width的列的宽度和
  • tdLength=列个数

一、固定表格布局,表格添加table-layout:fixed

ps:在固定表格布局中,表格列的宽度与列内容多少无关,只与表格宽度、列宽度、表格左右边框、列左右边框、列左右内填补有关

通过使用固定表格布局,用户代理在接收到第一行后就可以显示表格,即只有第一行的宽度才会起作用

width为auto的列的宽度(即未定义width的列的宽度,如果计算结果为负数则为0)= (tableWidth-tableBorderWidth-tdBorderWidth-tdPadding-tdWidth)/tdLength

1、所有th宽度未定义

每列的宽度通过表格宽度平均分配

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

2、所有th都定了宽度,同时所有列宽度之和小于表格宽度(tableBorderWidth+tdBorderWidth+tdPadding+tdWidth <= tableWidth)

每列的宽度通过总宽度平均分配;表格的宽度为其定义的宽度

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

3、所有th都定了宽度,同时所有列宽度之和大于表格宽度(tableBorderWidth+tdBorderWidth+tdPadding+tdWidth > tableWidth)

每列的宽度为自身定义的宽度;表格的宽度为所有列宽度总和(会超出表格定义的宽度)

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

4、部分th定了宽度,同时定了th宽度的列的宽度之后小于表格宽度(tableBorderWidth+tdBorderWidth+tdPadding+tdWidth <= tableWidth)

ps:深灰色背景的列为定义了宽度的列

定义宽度的列的宽度为自身定义的宽度,其他没有定义宽度的列的宽度为表格总宽度减去定义的宽度之和再平均分配

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

5、部分th定了宽度,同时定了th宽度的列的宽度之后大于表格宽度(tableBorderWidth+tdBorderWidth+tdPadding+tdWidth > tableWidth)

ps:深灰色背景的列为定义了宽度的列

定义宽度的列的实际宽度为自身定义的宽度,其他没有定义宽度的列的宽度为表格总宽度减去定义的宽度之和再平均分配,平均分配后的宽度小于零,则其它没有定义宽度的列的宽度为0

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

二、自动表格布局,表格设置table-layout:auto(该属性默认值就是auto)

每个列的宽度由单元格中没有折行的最宽的内容设定的,此种算法有时候会很慢,这是由于它需要在确定最终的布局之前访问表格中所有的列

1、所有th都未定最小宽度

每一列的宽度完全由里面的内容所决定。

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1row1row1row1row1row1row1row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5row5row5row5row5row5row5row5 row6 row7row7row7row7row7row7row7row7 row8row8row8row8row8row8row8row8 row9row9row9row9row9row9row9row9 row10row10row10row10row10row10row10

2、所有th都定义了最小宽度,根据内容计算的所有列之和小于表格宽度

每列宽度首先根据内容计算,同时不能小于定义的最小宽度,多余的宽度每一列上面平均分配点。

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1 row2row2row2row2row2row2row2row2 row3 row4 row5 row6 row7 row8 row9 row10

3、所有th都定义了最小宽度,根据内容计算的所有列之和大于表格宽度

每列宽度首先根据内容计算,其次不能小于定义的最小宽度

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5 row6row6row6row6row6row6row6row6 row7 row6row6row6row6row6row6row6row6 row9 row10row10row10row10row10row10row10

4、部分th定义了最小宽度,根据内容计算的所有列之和小于表格宽度

ps:深灰色背景的列为定义了最小宽度的列

每列宽度首先根据内容计算,其次不能小于定义的最小宽度,最后表格渲染出来的宽度不能小于表格自身定义的宽度。

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1 row2 row3 row4row4row4row4row4row4row4row4 row5 row6 row7 row6 row9 row10

5、部分th定义了最小宽度,根据内容计算的所有列之和小于表格宽度

ps:深灰色背景的列为定义了最小宽度的列

每列宽度首先根据内容计算,其次不能小于定义的最小宽度

th1 th2 th3 th4 th5 th6 th7 th8 th9 th10
row1 row2row2row2row2row2row2row2row2 row3 row4row4row4row4row4row4row4row4 row5 row6row6row6row6row6row6row6row6 row7 row6row6row6row6row6row6row6row6 row9 row10row10row10row10row10row10row10

推荐阅读
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 本文介绍了解决IE678伪类不兼容问题的方法,包括少用CSS3和HTML5独有的属性,使用CSS hacker,使用last-child清除浮动、批量添加标签、去掉list item最后一个的border-right等技巧。同时还介绍了使用after清除浮动时加上IE独有属性zoom:1的处理方法。另外,本文还提到可以使用jQuery代替批量添加标签的功能,以及使用负边距和CSS2选择器element+element去掉list item最后一个的border-right的方法。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • 运算放大器使用规则及注意事项
    本文介绍了运算放大器的使用规则和注意事项,包括输入电压的限制、输出直接并接电容的安全性等。通过了解这些规则和注意事项,可以更好地使用运算放大器,避免出现意外情况。 ... [详细]
  • Java图形化计算器设计与实现
    本文介绍了使用Java编程语言设计和实现图形化计算器的方法。通过使用swing包和awt包中的组件,作者创建了一个具有按钮监听器和自定义界面尺寸和布局的计算器。文章还分享了在图形化界面设计中的一些心得体会。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
author-avatar
冲动王子2502901503
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有