【发布时间】:2018-04-30 21:56:50
【问题描述】:
我无法在下表中的<td> 单元格上设置宽度属性,而且我很难弄清楚原因。我试过同时使用文字width="100" 和内联样式width: 100px 和max-width: 100px,但由于某种原因,某些单元格会自动生成比它们包含的img 元素更宽的宽度。
我只是试图将每个 td 的宽度限制为其中 img 的宽度,以便每一行的宽度相同并且图像对齐。
对于上下文 - 这是一个包含来自 Photoshop 的图像“切片”的表格。内联样式是必要的,因为这是用于 Gmail 和 Outlook 中的电子邮件签名(我理解为什么这通常是一个坏主意,但在这一点上客户是老板,我只关心这个问题中的表格对齐方式)。 我将表格背景颜色更改为石灰以帮助调试。
编辑:我在此处使用占位符图像来保护客户的隐私。我认为 Gmail 中这些图像切片之间出现的垂直“间隙”与表格的宽度未固定有关。这就是为什么我将table-layout 设置为fixed 并希望将每个<td> 调整为图像宽度并将剩下的所有内容浮动可以解决问题。
<table id="Table_01" width="600" height="192" border="0" cellpadding="0" cellspacing="0" style="background: lime;table-layout: fixed">
<tbody width="600" style="display: table;max-width: 600px; width: 600px">
<tr width="600" id="row1" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="600" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/TS20G.gif" width="600" height="114">
</td>
</tr>
<tr width="600" id="row2" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="159" rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/jD36x.gif" width="159" height="18">
</td>
<td cellspacing="0" cellpadding="0" width="152" style="line-height: 0;font-size: 0px;">
<a href="#" target="_blank">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/fBCnK.gif" width="152" height="15" border="0"></a>
</td>
<td cellspacing="0" cellpadding="0" width="289" rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/vZaBD.gif" width="289" height="18">
</td>
</tr>
<tr width="600" id="row3" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="152" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/8Xdce.gif" width="152" height="3"></td>
</tr>
<tr width="600" id="row4" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="137" rowspan="4" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/J1iQh.gif" width="137" height="59">
</td>
<td cellspacing="0" cellpadding="0" width="181" style="line-height: 0;font-size: 0px;">
<a href="#">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/34yid.gif" width="180" height="12" border="0"></a>
</td>
<td cellspacing="0" cellpadding="0" width="283" rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/t84fT.gif" width="283" height="39">
</td>
</tr>
<tr width="600" id="row5" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="181" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/skKMN.gif" width="180" height="27">
</td>
</tr>
<tr width="600" id="row6" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="139" rowspan="2" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/MbrAv.gif" width="139" height="20">
</td>
<td cellspacing="0" cellpadding="0" width="161" style="line-height: 0;font-size: 0px;">
<a href="#">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/imDAp.jpg" width="161" height="14" border="0"></a>
</td>
<td cellspacing="0" cellpadding="0" width="163" rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/0TpK3.gif" width="163" height="20">
</td>
</tr>
<tr width="600" id="row7" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" width="161" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/9TPqy.gif" width="161" height="6">
</td>
</tr>
<tr width="600" id="row8" style="line-height: 0;font-size: 0px;">
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="137" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="22" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="117" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="35" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="6" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="120" height="1"></td>
<td>
<img border="0" style="display:block;line-height: 0" src="https://i.stack.imgur.com/25rPU.gif" width="163" height="1"></td>
</tr>
</tbody>
</table>
编辑#2:
我可以通过完全移除固定的表格布局来靠近,但单元格仍然比其中的 img 稍宽(注意每个 <td> 右侧的绿色空间):
<table id="Table_01" width="600" height="192" border="0" cellpadding="0" cellspacing="0" style="background: lime;">
<tr id="row1" style="line-height: 0;font-size: 0px;">
<td cellspacing="0" cellpadding="0" colspan="7" style="line-height: 0;font-size: 0px;" width="600">
<img border="0" style="display:block;line-height: 0; width: 600px; height: auto;float:left;" src="https://i.stack.imgur.com/TS20G.gif" alt=" "></td>
</tr>
<tr id="row2" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" colspan="2 " rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; height: 18px; width: 100%;float:left;" src="https://i.stack.imgur.com/jD36x.gif" alt=" "></td>
<td cellspacing="0" cellpadding="0" colspan="2 " style="line-height: 0;font-size: 0px;">
<a target="_blank " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; height: 15px;float:left;" src="https://i.stack.imgur.com/fBCnK.gif" border="0 " alt=" "></a>
</td>
<td cellspacing="0" cellpadding="0" colspan="3 " rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: auto;float:left;" src="https://i.stack.imgur.com/vZaBD.gif" alt=" "></td>
</tr>
<tr id="row3" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" colspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 152px;float:left;" src="https://i.stack.imgur.com/8Xdce.gif" height="3 " alt=" "></td>
</tr>
<tr id="row4" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" rowspan="4 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 137px;float:left;" src="https://i.stack.imgur.com/J1iQh.gif" height="59 " alt=" "></td>
<td cellspacing="0" cellpadding="0" colspan="4 " style="line-height: 0;font-size: 0px;">
<a href="#" style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 181px;cursor: pointer;float:left;" src="https://i.stack.imgur.com/34yid.gif" height="12 " border="0 " alt=" "></a>
</td>
<td cellspacing="0" cellpadding="0" colspan="2 " rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 283px;float:left;" src="https://i.stack.imgur.com/t84fT.gif" height="39 " alt=" "></td>
</tr>
<tr id="row5" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" colspan="4 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 181px;float:left;" src="https://i.stack.imgur.com/skKMN.gif" height="27 " alt=" "></td>
</tr>
<tr id="row6" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" colspan="2 " rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 140px;float:left;" src="https://i.stack.imgur.com/imDAp.jpg" height="20 " alt=" "></td>
<td cellspacing="0" cellpadding="0" colspan="3 " style="line-height: 0;font-size: 0px;">
<a href="#">
<img border="0 " style="display:block;line-height: 0;width: 161px;cursor: pointer;float:left;" src="https://i.stack.imgur.com/imDAp.jpg" height="14 " border="0 " alt=" "></a>
</td>
<td cellspacing="0" cellpadding="0" rowspan="2 " style="line-height: 0;font-size: 0px;">
<img border="0 " style="display:block;line-height: 0; width: 163px;float:left;" src="https://i.stack.imgur.com/9TPqy.gif" height="20" alt=""></td>
</tr>
<tr id="row7" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" colspan="3" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 161px;float:left;" src="https://i.stack.imgur.com/9TPqy.gif" height="6 " alt=" "></td>
</tr>
<tr id="row8" style="line-height: 0;font-size: 0px;" width="600">
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
</td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width:22px; height: 0px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" alt="">
</td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 117px; height: 0px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" height="0" alt=""></td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 35px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" height="0" alt=""></td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 6px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" height="0" alt=""></td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 120px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" height="0" alt=""></td>
<td cellspacing="0" cellpadding="0" style="line-height: 0;font-size: 0px;">
<img border="0" style="display:block;line-height: 0; width: 163px;float:left;" src="https://i.stack.imgur.com/25rPU.gif" height="0" alt=""></td>
</tr>
</table>
【问题讨论】:
-
您似乎有 7 列,但从未说明每列有多宽,而且您的第一列似乎总是跨越 2 - 需要吗?我将首先整理您的表格布局并简化它。然后整理你的图像 - 其中一些溢出了它们的列。如果你有一张决赛桌应该是什么样子的图片,那将是一个很大的帮助
-
我会说从表中删除所有
width属性,尤其是从 tr 元素中,并放入 7 个具有正确宽度的col元素。 -
内联样式太多了。使代码更加混乱。
-
请阅读详细信息:内联样式是必需的,因为这是用于 Gmail 和 Outlook 中的电子邮件签名
标签: css html html-table