HTML 表格单元格 <td> 的默认宽度是多少? [英] What is the default width of an HTML table cell &lt;td&gt;?

查看:61
本文介绍了HTML 表格单元格 <td> 的默认宽度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到这个问题的答案:规范或 UA 文档中的何处是 定义的默认宽度?

I haven't been able to find the answer to this question: Where in the spec or in UA documentation is the default width of a <td> defined?

我搜索了 HTML 生活标准HTML5 建议,以及其他各种来源.

I've searched the HTML Living Standard, the HTML5 Recommendation, and various other sources.

我的理解(基于使用和观察)是,默认情况下,表格单元格将占据其所在列的整个宽度.如果列中存在其他单元格,则单元格的宽度不能与列不同.

My understanding (based on usage and observation) is that a table cell will, by default, occupy the full width of the column in which it lives. And the cell cannot be given a different width than the column if other cells exist in the column.

我正在寻找对此行为的官方确认,最好是在 W3C 或用户代理文档中.但任何权威参考都是可以接受的.

I'm looking for official confirmation of this behavior, preferably in W3C or user agent documentation. But any authoritative reference is acceptable.

推荐答案

表格单元格的物理/视觉宽度不是由 HTML 定义的,而是由 CSS 定义的.CSS 2.1 规范有一整节专门介绍 表格布局,作为对 HTML 描述的补充表格数据.

The physical/visual width of a table cell is defined not by HTML, but by CSS. The CSS 2.1 specification has an entire section dedicated to table layout that complements HTML's description of tabular data.

此外,CSS 本身并不完全 定义 如何计算单元格的宽度.它使用固定表格布局算法:

Furthermore, CSS itself does not fully define how the width of a cell is calculated. It does with the fixed table layout algorithm:

在固定表格布局算法中,每列的宽度确定如下:

In the fixed table layout algorithm, the width of each column is determined as follows:

  1. 'width' 属性值不是 'auto' 的列元素设置该列的宽度.
  2. 否则,第一行中 'width' 属性值不是 'auto' 的单元格将决定该列的宽度.如果单元格跨越一列以上,则宽度会在各列之间进行划分.
  3. 任何剩余的列均分剩余的水平表格空间(减去边框或单元格间距).

表格的宽度是表格元素的 'width' 属性值和列宽(加上单元格间距或边框)的总和中的较大者.如果表格比列宽,额外的空间应该分布在列上.

The width of the table is then the greater of the value of the 'width' property for the table element and the sum of the column widths (plus cell spacing or borders). If the table is wider than the columns, the extra space should be distributed over the columns.

但是除了自动表格布局的粗略指南之外,它没有给出任何内容,用户代理可以自由遵循或偏离(它列出了一个与固定表格布局不同的分步过程,但整个列表是非规范的).通常,您可以在 大多数常见场景— 正如您所观察到的,自动调整大小的表格单元格通常会占用其内容所需的空间,仅此而已.但是深入研究边缘情况,你会发现各种 疯了.

but it doesn't give anything beyond a rough guideline for auto table layout, which user agents are free to follow or deviate from (it lists a step-by-step procedure not unlike that of fixed table layout, but that entire list is non-normative). Generally you can expect consistent behavior from UAs in the most common scenarios — as you observe, an auto-sized table cell generally takes up as much space as required by its content, and no more. But dig into edge cases, and you'll find all sorts of crazy.

这篇关于HTML 表格单元格 <td> 的默认宽度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆