Html 表里面的div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2974573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
div inside table
提问by andrew Sullivan
Can somebody tell me whether div inside a table is allowed or not according to w3c
有人可以告诉我根据 w3c 是否允许在表中使用 div
回答by echox
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>test</title>
</head>
<body>
<table>
<tr>
<td>
<div>content</div>
</td>
</tr>
</table>
</body>
</html>
This document was successfully checked as XHTML 1.0 Transitional!
此文档已成功检查为 XHTML 1.0 Transitional!
回答by P?r Wieslander
You can't put a div
directlyinside a table
, like this:
你不能div
直接把 a 放在 a里面table
,像这样:
<!-- INVALID -->
<table>
<div>
Hello World
</div>
</table>
Putting a div
inside a td
or th
element is fine, however:
将 adiv
放在 atd
或th
元素内是可以的,但是:
<!-- VALID -->
<table>
<tr>
<td>
<div>
Hello World
</div>
</td>
</tr>
</table>
回答by Paul
you can put div tags inside a td tag, but not directly inside a table or tr tag. examples:
您可以将 div 标签放在 td 标签内,但不能直接放在 table 或 tr 标签内。例子:
this works:
这有效:
<table>
<tr>
<td>
<div>This will work.</div>
</td>
</tr>
<table>
this does not work:
这不起作用:
<table>
<tr>
<div> this does not work. </div>
</tr>
</table>
nor does this work:
这也不起作用:
<table>
<div> this does not work. </div>
</table>
回答by Robusto
While you can, as others have noted here, put a DIV inside a TD (not as a direct child of TABLE), I strongly advise against using a DIV as a child of a TD. Unless, of course, you're a fan of headaches.
正如其他人在此处指出的那样,虽然您可以将 DIV 放入 TD(而不是作为 TABLE 的直接子代),但我强烈建议不要将 DIV 用作 TD 的子代。当然,除非你是头痛的粉丝。
There is little to be gained and a whole lot to be lost, as there are many cross-browser discrepancies regarding how widths, margins, borders, etc., are handled when you combine the two. I can't tell you how many times I've had to clean up that kind of markup for clients because they were having trouble getting their HTML to display correctly in this or that browser.
由于在将两者结合时如何处理宽度、边距、边框等方面存在许多跨浏览器差异,因此获得的收益很少,损失却很多。我无法告诉您有多少次我不得不为客户清理这种标记,因为他们无法让他们的 HTML 在这个或那个浏览器中正确显示。
Then again, if you're not fussy about how things look, disregard this advice.
再说一次,如果您对事物的外观不挑剔,请忽略此建议。
回答by 2ndkauboy
It is allow as TD can contain inline- AND block-elements.
这是允许的,因为 TD 可以包含 inline-AND 块元素。
Here you can find it in the reference: http://xhtml.com/en/xhtml/reference/td/#td-contains
在这里你可以在参考中找到它:http: //xhtml.com/en/xhtml/reference/td/#td-contains