为 Gmail 设计 HTML 电子邮件
我正在生成一封使用内部样式表的 html 电子邮件,即
<!doctype html>
<html>
<head>
<style type="text/css">
h2.foo {color: red}
</style>
</head>
<body>
<h2 class="foo">Email content here</foo>
</body>
</html>
在 Gmail 中查看时,内部样式表中的所有样式似乎都被忽略。 Gmail 似乎忽略了除内联规则之外的所有样式,例如,
<h2 style="color: red">Email content here</foo>
这是我在使用 Gmail 查看时设置 HTML 电子邮件样式的唯一选项吗?
I'm generating a html email that uses an internal stylesheet, i.e.
<!doctype html>
<html>
<head>
<style type="text/css">
h2.foo {color: red}
</style>
</head>
<body>
<h2 class="foo">Email content here</foo>
</body>
</html>
When viewed in Gmail it seems all the styles in the internal stylesheet are ignored. It seems Gmail ignores all styles other than inline rules, e.g.
<h2 style="color: red">Email content here</foo>
Is this my only option for styling HTML emails when viewed with Gmail?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对所有内容都使用内联样式。该网站会将您的类转换为内联样式:http://premailer.dialect.ca/
Use inline styles for everything. This site will convert your classes to inline styles: http://premailer.dialect.ca/
截至 2016 年 9 月 30 日,此处的答案已过时。 Gmail 目前 推出对
style
标签的支持head
,以及媒体查询。如果 Gmail 是您唯一关心的问题,那么您可以像现代开发人员一样安全地使用课程!作为参考,您可以查看官方 gmail CSS 文档。
附带说明一下,Gmail 是唯一不支持
style
的主要客户端(参考,直到他们更新)。这意味着您几乎可以安全地停止将样式内联。一些比较不起眼的客户可能仍然需要它们。The answers here are outdated, as of today Sep 30 2016. Gmail is currently rolling out support for the
style
tag in thehead
, as well as media queries. If Gmail is your only concern, you're safe to use classes like a modern developer!For reference, you can check the official gmail CSS docs.
As a side note, Gmail was the only major client that didn't support
style
(reference, until they update anyway). That means you can almost safely stop putting styles inline. Some of the more obscure clients may still need them.Gmail 开始对头部区域的样式标签提供基本支持。尚未发现任何官方信息,但您可以轻松地自己尝试。
它似乎忽略了类和 id 选择器,但基本元素选择器可以工作。
它将在自己的头部区域创建一个样式标签,仅限于包含邮件正文的 div
Gmail started basic support for style tags in the head area. Found nothing official yet but you can easily try it yourself.
It seems to ignore class and id selectors but basic element selectors work.
it will create a style tag in its own head area limited to the div containing the mail body
我同意每个支持类和内联样式的人的观点。您现在可能已经了解了这一点,但是如果您的样式表中存在一个错误,Gmail 将忽略它。
你可能会认为你的 CSS 是完美的,因为你经常这样做,为什么我的 CSS 会出现错误呢?通过 CSS 验证器运行它(例如 http://www.css-validator.org/)看看会发生什么。我在遇到一些 Gmail 显示问题后这样做了,令我惊讶的是,几个 Microsoft Outlook 特定样式声明显示为错误。
这对我来说很有意义,所以我将它们从样式表中删除,并将它们放入
only for Microsoft
代码块中,如下所示:这只是一个简单的示例,但是,谁知道,它可能会出现一段时间派上用场。
I agree with everyone who supports classes AND inline styles. You might have learned this by now, but if there is a single mistake in your style sheet, Gmail will disregard it.
You might think that your CSS is perfect, because you've done it so often, why would I have mistakes in my CSS? Run it through the CSS Validator (for example http://www.css-validator.org/) and see what happens. I did that after encountering some Gmail display issues, and to my surprise, several Microsoft Outlook specific style declarations showed up as mistakes.
Which made sense to me, so I removed them from the style sheet and put them into a
only for Microsoft
code block, like so:This is just a simple example, but, who know, it might come in handy some time.
请注意,用于发送电子邮件的服务和工具可能能够为您内联 CSS,从而允许
标记中的 CSS 在 Gmail 中使用。
例如,如果您使用 MailChimp 发送电子邮件,则默认情况下,
标记中的 CSS 将自动内联。使用 Mandrill,您可以启用此功能(尽管默认情况下它是禁用的 出于性能原因),方法是选中“设置”的“发送默认值”部分中的“HTML 电子邮件中的内联 CSS 样式”框选项卡:
Note that services and tools for sending emails may be able to inline your CSS for you, allowing CSS in
<style>
tags to work in Gmail.For instance, if you're sending emails with MailChimp, your CSS from
<style>
tags will get inlined automatically by default. With Mandrill, you can enable this functionality (although it's disabled by default for performance reasons) by checking the "Inline CSS Styles in HTML Emails" box in the "Sending Defaults" section of the Settings tab:正如其他人所说,某些电子邮件程序不会读取 CSS 样式。如果您已经编写了网络电子邮件,则可以使用 zurb 中的以下工具来内联所有样式:
http ://zurb.com/ink/inliner.php
当使用上面提到的来自 mailchimp、活动监视器等的模板时,这非常方便,因为正如您所发现的,它们在某些电子邮件中不起作用程序。该工具将您的样式部分留给将读取它的邮件程序,并将所有样式内联,以便以您想要的格式获得更通用的可读性。
As others have said, some email programs will not read the css styles. If you already have a web email written up you can use the following tool from zurb to inline all of your styles:
http://zurb.com/ink/inliner.php
This comes in extremely handy when using templates like those mentioned above from mailchimp, campaign monitor, etc. as they, as you have found, will not work in some email programs. This tool leaves your style section for the mail programs that will read it and puts all the styles inline to get more universal readability in the format that you wanted.
我在 Mailjet 中设计模板时遇到了同样的问题。问题的解决方案是在
标签内缩小 CSS 代码。
I had the same problem while designing a template in Mailjet. Solution of the problem was minified CSS code inside
<style>
tags.@shawn 确实给出了如何将头部样式与内联样式结合使用的示例。一些电子邮件客户端(Outlook、Gmail 等)有一些棘手的问题,这些可以通过 head 样式中的 css 来修复。
额外的表格
用于 Outlook 解决“宽度”问题并使布局居中。
@shawn did give an example how to use a combination of styling in the head section together with inline styling. Some email clients (Outlook, Gmail, etc.) have tricky things, these can be fixed with css in the head style.
The extra table
<table width="580">
is for Outlook to solve 'width' problems and also to center the layout.