指定 HTML 固定列宽度(宽度或样式属性)的更好方法是什么
我想问一下指定HTML列宽的更好方法是什么?宽度属性还是样式属性?假设我使用的是 IE 6。IE 渲染宽度属性是否比样式更好?
按宽度属性
<table width="900">
<tr>
<td width="450">A</td>
<td colspan="2" width="450">B&C</td>
</tr>
....
</table>
或按样式属性
<table style="width:900px;">
<tr>
<td style="width: 450px;">A</td>
<td colspan="2" style="width: 450px;">B&C</td>
</tr>
....
</table>
I would like to ask what is the better way of specifying HTML column width? the width attribute or the style attribute? Assuming I am using IE 6. Does IE render the width attribute better than style?
By width attribute
<table width="900">
<tr>
<td width="450">A</td>
<td colspan="2" width="450">B&C</td>
</tr>
....
</table>
OR by style attribute
<table style="width:900px;">
<tr>
<td style="width: 450px;">A</td>
<td colspan="2" style="width: 450px;">B&C</td>
</tr>
....
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在我回答您的问题之前,您应该知道表格是如何呈现的,请尝试表格元素的表格布局固定样式:
如果浏览器预先知道第一个表格行列的宽度(如果您提供表格布局)表格上的固定样式)浏览器甚至可以在计算任何结果行的宽度之前开始渲染表格的顶部。这意味着什么?由具有固定布局的 Ajax 调用填充的表可以在完整的 ajax 调用完成之前开始向用户显示结果。最好的理解方式就是像一张渐进式 jpg。最后,您的页面将显示加载速度更快。
现在回答你的问题。
实际上您提供的两个例子都不正确。您通常不会在跨越 2 个或更多单元格的单元格上设置宽度。在任何表格中,最好创建至少 1 行包含所有单元格,这可以在 TH 中,也可以在空白 tr 中(就像我喜欢在空白 tr 中执行的方式一样)。
例如...
您决定采用哪种方式要使用样式或仅使用标准 html 宽度,选择权在您,但最终您应该有第一行(如果表格布局固定)或任何行(如果表格布局不固定)来包含每个个体的宽度定义这也将帮助您规划正确的表格,希望这可以
测试固定的表格布局,并测试渲染速度与非固定的表格布局。
通过创建一个巨大的 10 000 行表格来 HTML 4 与 XHTML ,样式与属性我认为实际上是可维护性的问题,我认为使用样式或纯宽度与 HTML 4 过渡设置宽度没有任何错误,它们都做同样的事情。可以同时做到这两点是因为 HTML 已经进化了一些,是的,它可能会变得混乱,祝你好运!
Firstly before I answer your question, something you should know is how tables are rendered, experiment with the table-layout fixed style for the table element:
If the browser knows the width of the first table row columns upfront (if you provide the table layout fixed style on the table) the browser can begin rendering the top of the table even before its calculated the width of any resulting rows. What this means? Tables populated by Ajax calls with a fixed layout can begin displaying results to a user before the full ajax call is finished. Best way to think of this is like a progressive jpg. In the end your pages will appear to load faster.
Now to answer your question.
Actually neither example you provided is correct. you typically do not set width on a cell that is spanned across 2 or more cells. In any table its a good idea to create at least 1 row with all the cells, this can either be in the TH or (just the way I like to do it in a blank tr.
For example...
What ever way you decide to use style or just standard html width, the choice is yours, but in the end you should have your first row (if table layout is fixed) or any row (if table layout is not fixed) to contain the width definition for each invidivual cell. This will also help you with planning the correct looking table, hope this helps.
Test the table layout fixed, by creating a huge like 10 000 row table, and test the rendering speed vs a non fixed table layout.
The whole debate about HTML 4 vs XHTML , style vs attributes I think is really a question of maintainability. I don't think there is anything wrong setting the width using Style or plain width with HTML 4 transitional, they both do the same thing. The reason why you can do both is because HTML has evolved a bit, yes it can get messy! Good luck
只需在
内添加
标签或
在
内定义宽度代码>.这会对你有所帮助。其他都不起作用。
例如。
Just add
<div>
tag inside<td>
or<th>
define width inside<div>
. This will help you. Nothing else works.eg.