现代 CSS

HTML Email Boilerplate源码阅读

特别声明:如果您喜欢小站的内容,可以点击申请会员进行全站阅读。如果您对付费阅读有任何建议或想法,欢迎发送邮件至: airenliao@gmail.com!或添加QQ:874472854(^_^)

这篇文章主要是来阅读HTML Email Boilerplate模板的源码,以及扩展其相关的知识点,帮助大家更好的理解EDM模板的设计与开发。首先这是一个HTML电子邮件模板,这里的代码及示例代码是创建一个没有设计或布局的模板。他主要作用就是帮助你避免一些最常见的电子邮件客户端(如,Gmail,Outlook和Yahoo邮箱等)渲染时引起的主要问题。虽然不是可以直接插入使用(你知道的,你必须要做一些事情。),但它提供了一些非常有用的例子和代码片段,这些都可以帮助你设计和开发的HTML电子邮件在邮件客户端上能得到更好的渲染。

关于HTML Email Boilerplate

HTML Email Boilerplate模板是seanpowell根据Paul IrishHTML5 Boilerplate作为起点,在HTML电子邮件中使用像HTML5这样的新技术来展示一些老技术,让电子邮件客户端能更好的呈现。

如何使用

使用这些示例代码作为格式化HTML电子邮件的指导方针。您可能想基于这些代码片段创建自己的模板或者你只是想选择性用来修改你的问题。在整个模板中有两个主要领域:

  • 文档头部区域,你会发现全局的样式指明移动到内联
  • body中具体包含了修复和指导你具体需要的地方

注意:不要把注释和代码说明复制到你的信息中,因为这有可能存在把你的邮件当作垃圾邮件风险。

特别需要注意:头部区域的样式有时不需要或不应该放到内联中。这些实例都标注了对应的注释说明。

源码解读

我想大家和我一样,更关注的是如何应用HTML Email Boilerplate模板中的代码,让他能帮助我们修复或避免HTML电子邮件在客户端渲染时存在的问题。那么接下来,我们一起来阅读其源码。

下载源码

首要的条件,我们要得到HTML Email Boilerplate模板的源码。因为无码没法使用,也无法讨论,所以大家可以通过以下几种方法先获取其源码:

  • 直接点击这里下载源码文件
  • 打开你的终端,通过git在命令行输入git clone https://github.com/seanpowell/Email-Boilerplate(当然,你的电脑要装了git),将源码克隆到你本地
  • 官网下载或复制源码

使用XHTML文档类型

写过Web页面的都很清楚,到目前为止,能运用于Web页面的文档类型有很多种。在这里,我们更关心的是,在HTML电子邮件中,应采用哪种文档类型。经过五个多月的实战,得出在HTML电子邮件中使用xHTML文档类型更好。这个结论并不是凭空得到的,而是经过测试得出的结论。详细的介绍可以阅读HTML电子邮件我应该使用哪种文档类型为什么!DOCTYPE是HTML电子邮件设计的败家子

xHTML文档类型也有多种形式,在这里我们选择了"XHTML 1.0 Strict"的类型,主要是因为Gmail和Hotmail这两种流行的客户端会删除你自定义的文档类型,用它作为默认的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

请记住这段DOCTYPE很重要,因为很多客户端会删除你定义的文档类型——这将直接影响marginpadding,居中等样式渲染。

扩展阅读

客户端指定样式

HTML电子邮件的重置都是基于Mail Chimp。但重置的代码在Outlook、Hotmail和Gmail几个客户端存在问题。它还包括修复智能手机客户端因为字体大小损坏布局的bug。接下来,我们看一些客户端指定的样式代码:

/*迫使Outlook提供一个浏览器渲染的链接*/
#outlook a {padding:0;}
/*防止Webkit和Windows Mobile平台更改默认字体大小,破坏桌面设计*/
body{
    width:100% !important; 
    -webkit-text-size-adjust:100%; 
    -ms-text-size-adjust:100%; 
    margin:0; 
    padding:0;
}
/*迫使Hotmail电子邮件全屏显示*/
.ExternalClass {width:100%;} 
/*迫使Hotmail电子邮件显示正常的行间距,更多信息:http://www.emailonacid.com/forum/viewthread/43/*/
.ExternalClass, 
.ExternalClass p, 
.ExternalClass span, 
.ExternalClass font, 
.ExternalClass td, 
.ExternalClass div {
    line-height: 100%;
} 
#backgroundTable {
    margin:0; 
    padding:0; 
    width:100% !important; 
    line-height: 100% !important;
}

注:上面的样式不需要写在行内

合理的图像默认样式

HTML Email Boilerplate给图像设置了一些默认的样式。主要有以下几点:

  • "-ms-interpolation-mode: bicubic":IE中帮助IE适当调整图像(如果你使用宽度和高度属性调整图像)
  • "border:none":移除链接图像的边框
  • 更新了Gmail、Hotmail图像常见的问题:Gmail和Hotmail中的图像会在下面增加空距。你可能并不总想给所有图像设置为块元素。那么你只需要在你需要修复的图像元素不添加类名image_fix
img {
    outline:none; 
    text-decoration:none; 
    -ms-interpolation-mode: bicubic;
}
a img {
    border:none;
}
.image_fix {
    display:block;
}

注:以上样式绑定到行内。

修复Yahoo邮箱段落和链接问题

在Yahoo电子邮件客户端,不支持段落在文档中默认的margin值(通常段落在文档中渲染时分别在顶部和底部具有1em的margin值)。此模板中修复了这个问题:

p {margin: 1em 0;}

更多的信息可以阅读Appendix D. Default style sheet for HTML 4User Agent Style Sheets: Basics and Samples

美化链接颜色,让链接在新Yahoo邮件中变得更简单。事实上,为了符合主要邮件邮件客户端,可以把样式股票呢在行内。这样你的链接颜色在客户端都将统一起来。

a {color: orange;}

注:以上样式绑定到行内。

重置Hotmail中标题颜色

Hotmail在h2~h6标题上使用green颜色取代了你默认的颜色风格。在这个例子中,没有链接的标题设置为black,链接的标题设置为blue,点击时链接标题为red(很少客户端支持),访问过的链接标题为purple(很少客户端支持)。替换你使用的颜色。在Hotmail中使用!important是真正的可以覆盖Hotmail的一切样式。Hotmail也将h1h2设置为相同大小。

h1, h2, h3, h4, h5, h6 {color: black !important;}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {color: blue !important;}
/*最好是与正常的标题链接颜色不一样,仅提供给支持伪类的电子邮件客户端*/
h1 a:active, h2 a:active,  h3 a:active, h4 a:active, h5 a:active, h6 a:active {
color: red !important; 
}
h1 a:visited, h2 a:visited,  h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited {
color: purple !important; }

注:以上样式绑定到行内。

Outlook的内距问题

新版本的Outlook(Outlook07,10)给单元格增加了一些内距。这个问题可能导致增加空间,也完全丢失边框。可以在顶部样式或行内样式中添加下面的代码来进行修复:

table td {border-collapse: collapse;}

移动端样式设置

随着移动端的爆发,然而EDM的并未紧跟上,让移动端用户阅读电子邮件体验并不是很好。那么在移动端要让用户更有良好的体验,我们就必须考虑新的技术。那么在HTML Email Boilerplate模板中,也考虑了这方面的因素——使用媒体查询来实现移动端的一些效果。

使用媒体查询需要小心点,你不应该把样式写在内联,所以建议你写在<head>区域同的<style>中。

为了让Yahoo邮箱更友好的使用媒体查询,使用属性选择器替代类和ID选择器要更好些,如table[class=classname]。有关于这方面更详细的介绍,可以点击Prevent Yahoo! Mail from displaying the mobile version of your email

@media only screen and (max-device-width: 480px) {
    a[href^="tel"], a[href^="sms"] {
        text-decoration: none;
        color: black; /* 或者设置你喜欢的颜色 */
        pointer-events: none;
        cursor: default;
    }
    .mobile_link a[href^="tel"], 
    .mobile_link a[href^="sms"] {
        text-decoration: default;
        color: orange !important; /* 或者设置你喜欢的颜色 */
        pointer-events: auto;
        cursor: default;
    }
}
/* 更具体的目标 */
@media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
    /* 你猜对了, iPad平板电脑,小屏幕等 */
    a[href^="tel"], a[href^="sms"] {
        text-decoration: none;
        color: blue; /* 设置你需要的颜色 */
        pointer-events: none;
        cursor: default;
    }
    .mobile_link a[href^="tel"], 
    .mobile_link a[href^="sms"] {
        text-decoration: default;
        color: orange !important;
        pointer-events: auto;
        cursor: default;
    }
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
    /* iPhone4的样式放在这 */
}
/* Android参考资料http://developer.android.com/guide/webapps/targeting.html或http://pugetworks.com/2011/04/css-media-queries-for-targeting-different-mobile-devices/ ; */
@media only screen and (-webkit-device-pixel-ratio:.75){
    /* Android 低分辨的CSS布局*/
}
@media only screen and (-webkit-device-pixel-ratio:1){
    /* Android中分辨的CSS布局 */
}
@media only screen and (-webkit-device-pixel-ratio:1.5){
    /* Android高分辨的CSS布局 */
}

特殊客户端条件样式

现在很多window系统的移动设备,那么在这些设备上阅读电子邮件,也有其自身的样式设置。如果你在Windows下的移动端样多需要调整,可以在<head>区域添加条件样式:

<!--[if IEMobile 7]>
    <style type="text/css">

    </style>
<![endif]-->  

另外在Outlook客户端,其有些版本使用的内核不在是浏览器的内核,比如Outlook2007和Outlook2010版本,我们需要使用另外的条件样式:

<!--[if gte mso 9]>
    <style>

    </style>
<![endif]-->    

表容器

由于有一部分电子邮件客户端会把<body>标签屏蔽,因此需要使用一个<table>标签来替代<body>标签,交在这个表格上控制宽度和你电子邮件的背景颜色:

<table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
    <tr>
        <td>
            <!-- 邮件内容放在这里 -->
        </td>
    </tr>
</table>

常用标签设置

电子邮件常常使用<table>标签,可以设置表格内单元格宽度和大多数情况下重置cellspacingcellpaddingborder属性值为0。在电子邮件中需要使用间距,一般都是使用嵌套表格实现。

<table cellpadding="0" cellspacing="0" border="0" align="center">
    <tr>
        <td width="200" valign="top"></td>
        <td width="200" valign="top"></td>
        <td width="200" valign="top"></td>
    </tr>
</table>            

另外为了让Yahoo邮箱中的链接颜色能和其他电子邮件客户端一致,简单的修复方法就是在链接元素内使用行内样式来设置:

<a href="http://htmlemailboilerplate.com" target ="_blank" title="Styling Links" style="color: orange; text-decoration: none;">Coloring Links appropriately</a>

在Gmail和Hotmail邮件箱中,图片都会产生空白的间距。但很多时候你或许不想使用display:block;来修复图片元素产生的间距。可以给图片元素添加类名image_fix来删除图片的空白间距。另外为了给图片设置对齐方式,我们不能直接使用浮动属性,更好的方法是在元素内设置样式:

<img class="image_fix" src="full path to image" alt="Your alt text" title="Your title text" width="x" height="x" />

还有在移动端出现的数字,我们需要做一定的处理。也就是说使用电话号码(包括短信提示)。可以在标签中添加类名mobile-link来控制:

<span class="mobile_link">123-456-7890</span>

你的表格准备好了?

表格可以说是电子邮件制作中最重要的一个元素,在HTML Email Boilerplate也特别考虑这一点。在模板中给表格提供了几个初始化:第一个是最外的表格(#backgroundTable),充当一个容器,可以让电子邮件的宽度能占满电子邮件客户端全屏。另外为了避免一些样式问题,在表格上就重置了cellspacingcellpaddingborder值为0,并且设置单元格顶端对齐。

#backgroundTable {
    margin:0; 
    padding:0; 
    width:100% !important; 
    line-height: 100% !important;
}
 ... 
 <table cellpadding="0" cellspacing="0" border="0" id="backgroundTable"> 
    <tr> 
        <td>    

第二个表格嵌套在表格的单元格内,并且给表格设置宽度为600px。因为一个好的宽度可以用来让更多的电子邮件客户端保持更好的布局。

<table cellpadding="0" cellspacing="0" border="0" align="center"> 
    <tr> 
        <td width="200" valign="top"></td> 
        <td width="200" valign="top"></td> 
        <td width="200" valign="top"></td> 
    </tr> 
</table>                    

HTML Email Boilerplate模板

上面对模板源码各部分做了详细介绍,最后向大家展示一个完整的HTML Email Boilerplate源码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <title>Your Message Subject or Title</title>
        <style type="text/css">
            body{
                width:100% !important; 
                -webkit-text-size-adjust:100%; 
                -ms-text-size-adjust:100%; 
                margin:0; 
                padding:0;
            }
            .ExternalClass {
                width:100%;
            } 
            .ExternalClass, 
            .ExternalClass p, 
            .ExternalClass span, 
            .ExternalClass font, 
            .ExternalClass td, 
            .ExternalClass div {
                line-height: 100%;
            }
            #backgroundTable {
                margin:0; 
                padding:0; 
                width:100% !important; 
                line-height: 100% !important;
            }
            img {
                outline:none; 
                text-decoration:none; 
                -ms-interpolation-mode: bicubic;
            }
            a img {
                border:none;
            }
            .image_fix {
                display:block;
            }
            p {
                margin: 1em 0;
            }
            h1, h2, h3, h4, h5, h6 {
                color: black !important;
            }
            h1 a, h2 a, h3 a, h4 a, h5 a, h6 a {
                color: blue !important;
            }
            h1 a:active, 
            h2 a:active,  
            h3 a:active, 
            h4 a:active, 
            h5 a:active, 
            h6 a:active {
                color: red !important; 
            }
            h1 a:visited, 
            h2 a:visited,  
            h3 a:visited, 
            h4 a:visited, 
            h5 a:visited, 
            h6 a:visited {
                color: purple !important; 
            }
            table td {
                border-collapse: collapse;
            }
            a {
                color: orange;
            }
            /* Or to go the gold star route...
            a:link { color: orange; }
            a:visited { color: blue; }
            a:hover { color: green; }
            */
            @media only screen and (max-device-width: 480px) {
                a[href^="tel"], 
                a[href^="sms"] {
                    text-decoration: none;
                    color: black; 
                    pointer-events: none;
                    cursor: default;
                }
                .mobile_link a[href^="tel"], 
                .mobile_link a[href^="sms"] {
                    text-decoration: default;
                    color: orange !important; 
                    pointer-events: auto;
                    cursor: default;
                }
            }
            @media only screen and (min-device-width: 768px) and (max-device-width: 1024px) {
                a[href^="tel"], 
                a[href^="sms"] {
                    text-decoration: none;
                    color: blue; 
                    pointer-events: none;
                    cursor: default;
                }
                .mobile_link a[href^="tel"], 
                .mobile_link a[href^="sms"] {
                    text-decoration: default;
                    color: orange !important;
                    pointer-events: auto;
                    cursor: default;
                }
            }
            @media only screen and (-webkit-min-device-pixel-ratio: 2) {

            }
            @media only screen and (-webkit-device-pixel-ratio:.75){

            }
            @media only screen and (-webkit-device-pixel-ratio:1){
            }
            @media only screen and (-webkit-device-pixel-ratio:1.5){
            }
        </style>

        <!--[if IEMobile 7]>
            <style type="text/css">
            </style>
        <![endif]-->  

        <!--[if gte mso 9]>
            <style>
            </style>
        <![endif]-->
    </head>
    <body>
        <table cellpadding="0" cellspacing="0" border="0" id="backgroundTable">
            <tr>
                <td>
                    <table cellpadding="0" cellspacing="0" border="0" align="center">
                        <tr>
                            <td width="200" valign="top"></td>
                            <td width="200" valign="top"></td>
                            <td width="200" valign="top"></td>
                        </tr>
                    </table> 
                    <a href="http://htmlemailboilerplate.com" target ="_blank" title="Styling Links" style="color: orange; text-decoration: none;">Coloring Links appropriately</a>
                    <img class="image_fix" src="full path to image" alt="Your alt text" title="Your title text" width="x" height="x" />
                    <span class="mobile_link">123-456-7890</span>
                </td>
            </tr>
        </table> 
    </body>
</html> 

特别声明,以上代码来自于HTML EMAIL BOILERPLATE ,同时本文是根据文中的注释理解而成。

结论

HTML Email Boilerplate是一个优秀的HTML电子邮件模板。在这个模板中提供很多兼容各主流电子邮件客户端对电子邮件渲染的问题的解决文案。当然,并不是说这个模板可以解决所有的电子邮件在邮件客户端存在的问题,但至少有很多可以吸取的技巧。希望这篇文章能帮助大家解决平时制作EDM时碰到的坑。同时更希望同学们能一起参与到EDM的填坑队伍中,让更多的同学受益。如果您有什么好的想法或以前填过类似的坑,欢迎在下面的评论与我们一起分享。

如需转载,烦请注明出处:http://www.w3cplus.com/css/html-email-boilerplate.html

nike air max 90 infrared
返回顶部