【发布时间】:2016-07-01 02:15:17
【问题描述】:
我希望在不使用 CSS 媒体查询或 flex-box 的情况下设计一个简单的响应式 HTML 电子邮件模板。我希望电子邮件的中间区域在大屏幕上时有两列:
但在小屏幕上只有一列内容居中:
如果不使用媒体查询,您将如何做到这一点?
【问题讨论】:
标签: html css responsive-design html-email email-templates
我希望在不使用 CSS 媒体查询或 flex-box 的情况下设计一个简单的响应式 HTML 电子邮件模板。我希望电子邮件的中间区域在大屏幕上时有两列:
但在小屏幕上只有一列内容居中:
如果不使用媒体查询,您将如何做到这一点?
【问题讨论】:
标签: html css responsive-design html-email email-templates
我发现实现此目的的最佳方法是在表格内使用 DIV,同时使用 MSO 条件来控制桌面 Outlook 的宽度。
例子:
<!--[if (gte mso 9) | (IE) ]><table width="640"><tr><td align="center"><![endif]-->
<table width="100%" align="center">
<tr>
<td align="center">
<!--[if (gte mso 9) | (IE) ]><table width="100%"><tr><td width="300" align="center"><![endif]-->
<div style="width:300px; display:inline-block; margin:0 auto; text-align:center;">
<table align="center" width="100%">
<tr>
<td class="left" align="center">
<img border="0" src="yourimage.png" width="280" />
</td>
</tr>
</table>
</div>
<!--[if (gte mso 9) | (IE) ]></td><td width="320" align="right"><![endif]-->
<div style="max-width:320px; display:inline-block; margin:0 auto;">
<table align="center" width="100%">
<tr>
<td align="center">Your copy goes here</td>
</tr>
</table></div>
<!--[if (gte mso 9) | (IE) ]></td></tr></table><![endif]-->
</td>
</tr>
</table>
<!--[if (gte mso 9) | (IE) ]></td></tr></table><![endif]-->
下一个最好的方法是通过 TH 标签:
<!--[if (gte mso 9) | (IE) ]><table width="640"><tr><td align="center"><![endif]-->
<table width="100%" align="center" style="max-width:640px;">
<tr>
<th align="center" width="300" style="display:inline-block;"><img src="yourimage.png"></th>
<th align="center" width="310" style="display:inline-block;">your copy here</th>
</tr>
</table>
<!--[if (gte mso 9) | (IE) ]></td></tr></table><![endif]-->
这两个都有问题,需要不断地调整和操作才能让它们正常工作。但是,我发现 TH 解决方案有更多的怪癖(例如字体加粗、随机无法解释的边框等),并且比 div/mso 条件选项更不“可定制”。
大多数客户端 (ref) 都接受 div 中的最大宽度,但 Outlook 桌面除外,它由 MSO 条件句处理。如果您想进一步控制,您还可以将 div 设置为定义的宽度而不是最大宽度,从而实现更可控的环境。
【讨论】:
你可以通过下一个想法来实现这一点。
您可以为每个列定义一个固定宽度,并为父容器(在本例中为表本身)定义一个最大宽度。目标是如果没有足够的空间容纳第二列,则使第二列折叠在第一列下方。
在CodePen 上查看此示例bZWokw。CSS
.column-1 {float:left; width:200px;}
.column-2 {float:left; width:400px;}
.content { padding:10px; }
HTML
<table width="100%" cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="100%">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Hic autem ratione porro perferendis minus itaque ab harum molestiae natus ipsam.</p>
</td>
</tr>
<tr>
<td width="100%">
<div class="column-1">
<div class="content">
<img src="http://placehold.it/200x200/" alt="" style="max-width:100%;">
</div>
</div>
<div class="column-2">
<div class="content">
<h2>HAMILTON - The Revolution</h2>
<a href="">Review this Product</a>
</div>
</div>
</td>
</tr>
<tr>
<td width="100%">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ullam, esse itaque. Aliquid doloremque tempore, dicta iure consectetur odit vel eveniet minus mollitia reprehenderit incidunt ratione minima aut dignissimos, pariatur in.</p>
</td>
</tr>
</table>
【讨论】:
您可以通过将列设置为display: inline-block 来组织它们自己。不确定是否所有电子邮件客户端都支持它,但它应该可以正常工作。
HTML:
<div>Whatever content before</div>
<div class="row">
<div class="col"><img src="image.jpg" /></div>
<div class="col">text content</div>
<div class="col"><img src="image2.jpg" /></div>
</div>
<div>Whatever content after</div>
CSS:
.row {
text-align: center;
}
.col {
display: inline-block;
max-width: 300px;
text-align: left;
}
【讨论】:
@Gortonington 提供的答案非常好!我会更详细一点并提供一些代码:
<!--[if mso]>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="660">
<tr>
<td align="center" valign="top" width="660">
<![endif]-->
<table border="0" cellpadding="0" cellspacing="0" align="center" width="100%" style="max-width:660px;">
<tr>
<td align="center" valign="top" style="font-size:0;">
<!--[if mso]>
<table border="0" cellspacing="0" cellpadding="0" align="center" width="660">
<tr>
<td align="left" valign="top" width="330">
<![endif]-->
<div style="display:inline-block; Margin: 0 -2px; width:100%; min-width:200px; max-width:330px; vertical-align:top;">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td>
CONTENT DOES HERE
</td>
</tr>
</table>
</div>
<!--[if mso]>
</td>
<td align="left" valign="top" width="330">
<![endif]-->
<div style="display:inline-block; Margin: 0 -2px; width:100%; min-width:200px; max-width:330px; vertical-align:top;">
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td>
CONTENT DOES HERE
</td>
</tr>
</table>
</div>
<!--[if mso]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</table>
<!--[if mso]>
</td>
</tr>
</table>
<![endif]-->
此方法称为混合方法,可以为电子邮件客户端的不同屏幕尺寸重新配置布局,无论媒体查询支持如何。它的核心是使用 max-width 和 min-width 来强加刚性基线(允许一些移动),并为无论如何都束缚在桌面上的 Outlook 强加一个固定的宽宽度。一旦设置了适合移动设备的基线,媒体查询会在支持它的客户端中逐步增强电子邮件。
有一些开源模板可以实现这一点。我maintain one,他的hybrid template 满足你的需要。
【讨论】:
您使用表格并依赖正常的文档流 (breif description on that here)。这里要解释的太多了,虽然我自己讨厌只有链接的答案,但给你。
IMO,这可以解决您的确切问题,并且是我读过的最好的电子邮件模板教程之一。
“在没有媒体查询的情况下创建面向未来的响应式电子邮件”
编辑:此外,这是一个很好的链接,可以验证不同电子邮件客户端中 CSS 属性的兼容性:
【讨论】:
你可以试试:width: 100%;最大宽度:400px
【讨论】:
试试 Bootstrap3 它可能会对您有所帮助,它基本上是一个简化的 css 库。它适用于简单的网站和博客,而不是
【讨论】: