【问题标题】:Responsive HTML emails响应式 HTML 电子邮件
【发布时间】:2016-11-11 19:55:21
【问题描述】:

亲爱的网络开发者你好!

我在手机上的 Outlook 客户端中显示电子邮件时遇到了问题,甚至在我的台式 PC 上的 Outlook 中显示。

当我在 Outlook 中收到我的电子邮件并在 IE 中打开它时,我尝试调整它的大小,当它遇到需要切换到@media 屏幕的断点时,一切都中断了。几乎每个手机上的 Outlook 客户端都会出现这种情况。

事情就是这样。当我在 IE 中打开的那封电子邮件上按 F12 时,我看到我的宽度内联样式被划掉并且 .col 类生效。但它仍然没有正确和响应地显示电子邮件。这只发生在前景中。知道如何解决吗?

下面是部分代码:

<style>
    @media screen and (max-width: 525px) {
        /* FLUID TABLES */
        .wrapper {
            width: 100% !important;
            max-width: 100% !important;
        }
        /*COLUMNS TO ROWS */
        .col {
            display: block!important;
            width: 100%!important;
            border: none!important;
        }

        .img-max {
            max-width: 100% !important;
            width: 100% !important;
            height: auto !important;
        }
        .wide {
            width: 100% !important;
        }
        }
</style>

<table style="padding-top: 0px;padding-right: 15px;padding-bottom: 0px;padding-left: 15px;table-layout: auto;" border="0" cellpadding="0" cellspacing="0" width="100%">
    <tbody>
        <tr>
            <td style="padding-top: 0px;padding-right: 15px;padding-bottom: 20px;padding-left: 15px; background-color: rgb(255, 255, 255);" class="section-pad" align="center">
                <!--[if (gte mso 9)|(IE)]>
            <table align="center" border="0" cellspacing="0" cellpadding="0" width="700">
            <tr>
            <td align="center" valign="top" width="700">
            <![endif]-->
                <table style="max-width: 700px;table-layout: auto;" class="wide" border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tbody>
                        <tr>
                            <td style="background-color: rgb(236, 242, 248);">
                                <!-- HERO IMAGE -->
                                <table style="table-layout: auto; background-color: rgb(236, 242, 248); width: 92%;" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
                                    <tbody>
                                        <tr>
                                            <td class="col" align="center" valign="top" style="width: 65%; padding-left: 0px; background-color: rgb(236, 242, 248);">
                                                <table style="table-layout: auto; background-color: white; padding-left: 10px; padding-right: 10px;" border="0" cellpadding="0" cellspacing="0" width="100%">
                                                    <tbody>
                                                        <tr>
                                                            <td style="padding-top: 15px;padding-right: 20px;padding-bottom: 0;padding-left: 20px;font-size: 26px;line-height: 30px;font-weight:400; font-family: Arial, sans-serif; color: #0056a6;" align="left"><img src="/resources/handlers/dcimage.ashx" style="height: auto; display: inline;" border="0">,</td>
                                                        </tr>
                                                        <tr>
                                                            <td style="padding-top: 15px;padding-right: 20px;padding-bottom: 20px;padding-left: 20px; font-size: 14px; font-weight:normal; line-height: 14px; font-family: Arial, sans-serif; color: #2d353d;" align="left">
                                                                LOREM IPSUM
                                                            </td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                            <td style="background-color: rgb(236, 242, 248); width: 35%; padding-right: 0px;" class="col" align="center" valign="top">
                                                <p valign="middle" style="line-height: 40px; font-size: 12px; color: white; font-family: Arial, sans-serif; background-color: rgb(0, 86, 166);">
                                                    In case you need to contact us</p>
                                                <img src="/r1.jpg" alt="telephone" class="img-max" border="0" height="50" width="240">
                                                <img src="/r2.jpg" alt="skype" class="img-max" border="0" height="50" width="240">
                                                <img src="/r3.jpg" alt="mail" class="img-max" border="0" height="50" width="240">
                                                <img src="/r4.jpg" alt="web" class="img-max" border="0" height="50" width="240">

                                            </td>
                                        </tr>
                                        <tr>
                                          <td class="col" align="center" valign="top" style="width: 65%;">
                                            <img src="/r2.jpg" class="img-max">
                                            </td>
                                            <td style="background-color: rgb(236, 242, 248); width: 35%;" class="col" align="center" valign="top">&nbsp;</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
            </td>
        </tr>
    </tbody>
</table>

【问题讨论】:

  • 请注意,当我从我的 td 中删除 col 类时,一切都很好,但响应部分并不好 :D 我应该让 Outlook 以它想要的方式运行吗?

标签: html-email responsive


【解决方案1】:

如果我理解正确,当您对小屏幕的媒体查询生效时,您想要的行为是将 class="col" 的每个列市场放入一行。

首先,Outlook 将去除所有内联样式(正如您注意到使用 F12 时的那样),因此最好使用每个元素的等效 html 属性将其加倍。

其次,Outlook(以及 Lotus Notes 8 和 8.5)不支持 max-width,因此您需要将“col”列中的每个 &lt;table&gt;s 包装到创建表格的条件代码中设置宽度以容纳所有内容,针对 IE 和 Microsoft Outlook。您将需要使用类似的东西:

<!--[if (gte mso 9)|(IE)]>
<table width="525" align="left" cellpadding="0" cellspacing="0" border="0">
<tr>
    <td>
    <![endif]-->

    <table style="table-layout: auto; background-color: white; padding-left: 10px; padding-right: 10px;" border="0" cellpadding="0" cellspacing="0" width="100%">
       <tbody>
           <tr>
               <td style="padding-top: 15px;padding-right: 20px;padding-bottom: 0;padding-left: 20px;font-size: 26px;line-height: 30px;font-weight:400; font-family: Arial, sans-serif; color: #0056a6;" align="left"><img src="/resources/handlers/dcimage.ashx" style="height: auto; display: inline;" border="0">,</td>
           </tr>
           <tr>
               <td style="padding-top: 15px;padding-right: 20px;padding-bottom: 20px;padding-left: 20px; font-size: 14px; font-weight:normal; line-height: 14px; font-family: Arial, sans-serif; color: #2d353d;" align="left">
                                                            LOREM IPSUM
               </td>
           </tr>
        </tbody>
    </table>

    <!--[if (gte mso 9)|(IE)]>
    </td>
</tr>
</table>
<![endif]-->

我强烈建议您关注guide to build responsive emails that work on all major mail clients 以获得更深入的解释,以及其他非常有用的技巧,即使在 Outlook 上也可以使电子邮件工作:)

【讨论】:

    【解决方案2】:

    嗯,我不确定移动 Outlook 应用是否支持媒体查询。移动应用在渲染标记方面要比桌面应用好得多(无论如何,在 Windows 上)。

    无论如何,混合设计实现了响应式、变形的布局,而不依赖于媒体查询。您上面的代码正朝着正确的方向前进,尽管下面的示例有点不同。这是来自Fabio Carneiro's code samples on GitHub 的基本 2 列脚手架(全归功于他!):

    <!doctype html>
    <html>
    	<body style="margin:0;">
    		<center>
    			<table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
    
    
    
    
    				<!-- // 2-COLUMN SCAFFOLD [CENTERING, FLUID] -->
    				<tr>
    					<td align="center" height="100%" valign="top" width="100%">
    						<!--[if mso]>
    						<table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
    						<tr>
    						<td align="center" valign="top" width="660">
    						<![endif]-->
    						<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:660px;">
    							<tr>
    								<td align="center" valign="top" style="font-size:0;">
    									<!--// DEVELOPER NOTES:
    										1. Setting font-size:0; is necessary to ensure
    										   that there is no extra spacing introduced
    										   between the centering divs that wrap each
    										   of the columns. //-->
    
    									<!--[if mso]>
    									<table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
    									<tr>
    									<td align="left" valign="top" width="330">
    									<![endif]-->
    									<div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;">
    										<!--// DEVELOPER NOTES:
    											1. To have each column center upon stacking,
    											   wrap them in individual divs, set the same
    											   max-width and width as the table within it,
    											   and set display to inline-block; using
    											   vertical-align is optional.
    
    											2. Setting min-width determines when the two
    											   columns of this block will wrap; in this
    											   case, when the total available width is
    											   less than or equal to 480px. //-->
    
    										<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;">
    											<tr>
    												<td align="center" valign="top">
    	
    													<!-- // REPLACE WITH BLOCK -->
    													<p style="background-color:#2BAADF; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">LEFT</p>
    													<!-- REPLACE WITH BLOCK // -->
    	
    												</td>
    											</tr>
    										</table>
    									</div>
    									<!--[if mso]>
    									</td>
    									<td align="left" valign="top" width="330">
    									<![endif]-->
    									<div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;">
    										<table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;">
    											<tr>
    												<td align="center" valign="top">
    	
    													<!-- // REPLACE WITH BLOCK -->
    													<p style="background-color:#51BBE5; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">RIGHT</p>
    													<!-- REPLACE WITH BLOCK // -->	
    	
    												</td>
    											</tr>
    										</table>
    									</div>
    									<!--[if mso]>
    									</td>
    									</tr>
    									</table>
    									<![endif]-->
    								</td>
    							</tr>
    						</table>
    						<!--[if mso]>
    						</td>
    						</tr>
    						</table>
    						<![endif]-->
    					</td>
    				</tr>
    				<!-- 2-COLUMN SCAFFOLD [CENTERING, FLUID] // -->
    
    
    
    
    			</table>
    		</center>
    	</body>
    </html>

    在那个 repo 和其他地方有更多的脚手架和模式,但这显示了基本原理。

    (如果您还不知道它是如何工作的)此方法使用 max-width 和 min-width 强加刚性基线(允许一些移动)并为无论如何都束缚在桌面上的 Outlook 强加一个固定的宽宽度。一旦设置了适合移动设备的基线,媒体查询会在支持它的客户端(如 iOS 邮件)中逐步增强电子邮件。

    【讨论】:

      【解决方案3】:

      我记得,Outlook 移动应用程序不支持响应式电子邮件。我不认为 display: block;将完全适用于它们(与大多数 Android 电子邮件客户端相同)。您使用的响应式方法可在 iPhone 原生应用上可靠运行。

      【讨论】:

        猜你喜欢
        • 2014-07-04
        • 2013-02-16
        • 2013-09-16
        • 2019-12-02
        • 1970-01-01
        • 2021-12-25
        • 2018-01-31
        • 2019-01-22
        • 2016-11-20
        相关资源
        最近更新 更多