【问题标题】:A way to center TD in TR?一种将 TD 置于 TR 中心的方法?
【发布时间】:2012-01-28 19:05:06
【问题描述】:

我正在处理 HTML 电子邮件并尝试将绿色 TD 居中放置在白色 TR 中,以便在绿色框的左侧和右侧有 20 像素的白色边距。

我尝试为绿色部分设置 TD 宽度并将边距设置为 0 自动,但绿色只是扩展到 TR 的宽度。

尝试再放入 2 个 TD 以将绿色 TD 推到中心,但也没有用。

包括代码 sn-p,我在使用带有 #a6d971 的 TR 时遇到问题。

<table cellspacing="0" cellpadding="0" border="0" width="600" height="" bgcolor="" style="margin: 0 auto;">
    <tbody>
        <tr>
            <td style="margin: 0 auto;">
                <img width="600" height="23" padding="0" src="assets/graphic_scalloped_top.png" alt="" style="display: block;" />
            </td>
        </tr>
        <tr bgcolor="#fff" height="75">
            <td valign="top" style="text-align:center;">
                <p style="margin:0; padding-bottom: 3px; padding-top: 3px; color:#545d69; font-size: 24px; text-align:center; font-family: Arial, Helvetica;">
                    Regular sales happen every day
                </p>
                <p style="margin:0; padding-bottom: 3px; padding-top: 3px; color:#4bc1d6; font-size: 16px; text-align:center; font-family: Arial, Helvetica;">
                    9am - 11pm
                </p>
            </td>
        </tr>


        <tr bgcolor="#fff" height="75" padding="10">
            <td bgcolor="#000" width="20"></td>
            <td bgcolor="#a6d971" width="300" style="margin: 10;">
            </td>
            <td bgcolor="#000" width="20"></td>
        </tr>



        <tr bgcolor="#fff">
            <td valign="top">
                <table cellspacing="0" cellpadding="10" border="0" align="center" width="100%" bgcolor="#fff" style="">
                    <tbody>
                        <tr>
                            <td height="80" style="font-family: Helvetica, Arial, sans-serif; font-size: 18px; font-weight: bold; color: #555; background:url('assets/graphic_9am.png') no-repeat; background-position: 10% center; padding:10px; margin:0;">
                                <h3>Nine @ Nine</h3>
                                <p>Fuel up! Dresses, tunics and other items including:</p>
                            </td>
                        </tr>




                        <tr>

                        </tr>

                    </tbody>
                </table>
            </td>
        </tr>


        <tr>
            <td style="margin: 0 auto;">
                <img width="600" height="23" padding="0" src="assets/graphic_scalloped_bottom.png" alt="" style="display: block;" />
            </td>
        </tr>
    </tbody>
</table>

【问题讨论】:

  • 对于电子邮件客户端在使用 CSS 时遇到的所有问题……当您想要做的只是将一个框居中并设置一些颜色时,它们通常非常好。不要为此滥用表格。
  • 你有图片说明吗?
  • 您使用表格有什么特别的原因吗?
  • @Purmou,尽管让我很痛苦,但表格和内联 CSS 仍然是电子邮件开发的最佳方式。 =/
  • 是的,这就是我必须使用表格的原因:\

标签: html html-table alignment


【解决方案1】:

切换到 DIV 和 CSS,大多数电子邮件客户端都很好地支持样式,您可以在 TD 元素中使用 DIV,它可以轻松居中或做其他您可能想要的事情。

举例

<tr style="background-color: white;">
  <td  style="background-color: green;">
    <div style="background-color: purple; margin-right: 20px; margin-left: 20px;">Content Here</div>
  </td>
</tr>

另外请注意,如果您使用 DIV,您也可以避免使用表格。

【讨论】:

  • 非常感谢,这非常有效。我以为我已经尝试过这个,但是当我尝试它时,它总是把其余的内容弄乱。
  • 顺便说一句,我不知道我们现在可以在电子邮件客户端中使用 DIV 和 CSS。上次查了,不推荐。谢谢
  • Grace 是正确的,由于电子邮件客户端向后兼容,在 2018 年仍然不推荐。像 MailChimp 这样的专业批量邮件发件人从不使用它们。
【解决方案2】:

如果你必须使用表格,不妨滥用一下:

<table><tr align="center">
    <td width="50%">one</td>
    <td style="background-color:green">two</td>
    <td width="50%">three</td>
</tr></table>

http://jsfiddle.net/mblase75/yntfu/

【讨论】:

  • 这种滥用是否必须包括使用已弃用且现在不受支持(在 HTML5 中)的属性?
【解决方案3】:

在破解之上破解。

<table width="100%" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="border-left: 20px solid white; border-right: 20px solid white; background: green; color: white; text-align: center;">
                This is stuff.
            </td>
        </tr>
    </tbody>
</table>

http://jsfiddle.net/zy6GU/

顺便说一句,DIV 也可以实现同样的效果:

<div style="border-left: 20px solid white; border-right: 20px solid white; background: green; color: white; text-align: center;">This is a DIV.</div>

http://jsfiddle.net/zy6GU/1/

【讨论】:

  • 非常感谢,这非常有效。我以为我已经尝试过这个,但是当我尝试它时,它总是把其余的内容弄乱。
【解决方案4】:

您在说什么“用于电子邮件”?你的意思是一个电子邮件地址,比如&lt;a href="mailto:somewhere@domain.com"&gt;Email Me&lt;/a&gt;?如果是这样,您需要一些将链接置于 TD 中心的 CSS,或者与 TD 上的 colspan 结合使用。

【讨论】:

    【解决方案5】:

    我不是 CSS 专家,但这对我有用(没有额外的标签):

    <table>
      <tr style="background-color: white; border: 1px solid black;">
        <td style="background-color: green; display: block; margin: 0 20px;">
          <!-- Content -->
        </td>
      </tr>
    </table>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 2014-06-14
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2013-10-12
      相关资源
      最近更新 更多