英文:
How to force background color in gmail for Dark Mode?
问题
我正在处理HTML电子邮件模板。我在强制Dark模式的背景颜色和文本颜色属性方面遇到了问题。我的普通内联CSS是background-color:#f2f2f2和color:#191919。但在Dark模式下,我想要background-color:#ff5f00和color:#ffffff。
在Outlook中,我能够通过@media实现相同的效果,但是根据各种博客的建议在Gmail中使用混合模式,我无法实现相同的效果。有人可以帮助制作HTML模板或HTML代码吗?
以下是您提供的代码:(请注意,我已将HTML代码翻译成中文,代码部分不受翻译影响)
<html>
<head>
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
<style>
:root {
color-scheme: light dark;
supported-color-schemes: light dark;
}
body {
font-family: Arial;
background-color: #eee;
color: #111;
}
.Greetings_section {
background-color: #f2f2f2;
padding: 40px 20px;
}
.darkmodecustom {
background-color: #f2f2f2;
color: #191919;
padding: 5px;
}
.bg-orange {
background-color: #FF5F00;
border-color: 1px solid #ff5f00;
}
.button_text {
color: #ffffff;
}
.darkimage {
display: none;
}
a[href] {
color: #ffffff;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #111;
color: #eee;
}
.Greetings_section {
background-color: #f2f2f2;
padding: 20px 10px;
}
.darkmodecustom {
background-color: #f2f2f2;
color: #eee;
padding: 5px;
}
.bg-orange {
background-color: #FF5F00;
border-color: 1px solid #FF5F00;
}
.button_text {
color: #eee;
}
}
[data-ogsc] .body {
background-color: #111;
color: #eee;
}
[data-ogsc] .Greetings_section {
background-color: #f2f2f2;
padding: 40px 20px;
}
[data-ogsc] .darkmodecustom {
background-color: #f2f2f2;
color: white;
padding: 5px;
}
[data-ogsc] .bg-orange {
background-color: #FF5F00;
border-color: 1px solid #ff5f00;
}
[data-ogsc].button_text {
color: #eee;
}
</style>
</head>
<body class="body">
<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0" style="background-color:azure;padding:10px">
<tr>
<td style="padding: 5px;">Dark mode - 系统(用户代理样式)</td>
</tr>
<tr>
<td align="center" bgcolor="transparent" style="padding:0px; font-size:0px; line-height:0px;" valign="top" width="100%">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table align="center" border="0" cellpadding="0" cellspacing="0" style="padding:0; max-width:600px; font-size:0px; line-height:0px; background-color:#ffffff" width="100%">
<tr>
<td align="left" class="mobile-padding Greetings_section" style="padding:0px 24px 20px 24px; font-size:0px; line-height:0px" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="mobile-padding Greetings_section" style=" font-size:0px; line-height:0px;" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="fs14lh16 darkmodecustom" style="padding:0px 0px 8px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:20px; line-height:24px; font-weight:600; color:#191919" valign="top">
<span class="fs14lh16" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:20px; line-height:24px; font-weight:600; color:#191919"> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Libero, facilis assumenda soluta id ut labore ipsa aliquam reprehenderit</span>
</td>
</tr>
<tr>
<td align="left" style="padding:0px; font-size:0px; line-height:0px" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="fs12lh14 darkmodecustom" style="padding:8px 0px 0px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919" valign="top" width="15">
<span class="fs12lh14" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919
<details>
<summary>英文:</summary>
I am working on HTML email templates.I am stuck with forcing background color and text color attributes for Dark mode.My normal inline css is background-color:#f2f2f2 and color:#191919.But in dark mode I want background-color:#ff5f00 and color:#ffffff.
I am able to achieve the same in outlook using @media but as said in various blogs to use blend modes for gmail,I am not able to achieve the same.Can anyone help with a HTML template or HTML code?
CODE:
<html>
<head>
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
<style>
:root {
color-scheme: light dark;
supported-color-schemes: light dark;
}
body {
font-family: Arial;
background-color: #eee;
color: #111;
}
.Greetings_section {
background-color: #f2f2f2;
padding: 40px 20px;
}
.darkmodecustom {
background-color: #f2f2f2;
color: #191919;
padding: 5px;
}
.bg-orange {
background-color: #FF5F00;
border-color: 1px solid #ff5f00;
}
.button_text {
color: #ffffff;
}
.darkimage {
display: none;
}
a[href] {
color: #ffffff;
}
@media (prefers-color-scheme: dark) {
body {
background-color: #111;
color: #eee;
}
.Greetings_section {
background-color: #f2f2f2;
padding: 20px 10px;
}
.darkmodecustom {
background-color: #f2f2f2;
color: #eee;
padding: 5px;
}
.bg-orange {
background-color: #FF5F00;
border-color: 1px solid #FF5F00;
}
.button_text {
color: #eee;
}
}
[data-ogsc] .body {
background-color: #111;
color: #eee;
}
[data-ogsc] .Greetings_section {
background-color: #f2f2f2;
padding: 40px 20px;
}
[data-ogsc] .darkmodecustom {
background-color: #f2f2f2;
color: white;
padding: 5px;
}
[data-ogsc] .bg-orange {
background-color: #FF5F00;
border-color: 1px solid #ff5f00;
}
[data-ogsc].button_text {
color: #eee;
}
</style>
</head>
<body class="body">
<table role="presentation" width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table role="presentation" width="600" border="0" cellspacing="0" cellpadding="0" style="background-color:azure;padding:10px">
<tr>
<td style="padding: 5px;">Dark mode - System (User Agent Styles)</td>
</tr>
<tr>
<td align="center" bgcolor="transparent" style="padding:0px; font-size:0px; line-height:0px;" valign="top" width="100%">
<!--[if (gte mso 9)|(IE)]>
<table align="center" border="0" cellspacing="0" cellpadding="0" width="600">
<tr>
<td align="center" valign="top" width="600">
<![endif]-->
<table align="center" border="0" cellpadding="0" cellspacing="0" style="padding:0; max-width:600px; font-size:0px; line-height:0px; background-color:#ffffff" width="100%">
<tr>
<td align="left" class="mobile-padding Greetings_section" style="padding:0px 24px 20px 24px; font-size:0px; line-height:0px" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="mobile-padding Greetings_section" style=" font-size:0px; line-height:0px;" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="fs14lh16 darkmodecustom" style="padding:0px 0px 8px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:20px; line-height:24px; font-weight:600; color:#191919" valign="top">
<span class="fs14lh16" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:20px; line-height:24px; font-weight:600; color:#191919"> Lorem ipsum dolor, sit amet consectetur adipisicing elit. Libero, facilis assumenda soluta id ut labore ipsa aliquam reprehenderit</span>
</td>
</tr>
<tr>
<td align="left" style="padding:0px; font-size:0px; line-height:0px" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" class="fs12lh14 darkmodecustom" style="padding:8px 0px 0px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919" valign="top" width="15">
<span class="fs12lh14" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919">&bull;</span>
</td>
<td align="left" class="fs12lh14" style="padding:8px 0px 0px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919" valign="top">
<span class="fs12lh14" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919">Lorem ipsum dolor, sit amet consectetur adipisicing elit. </span>
</td>
</tr>
<tr>
<td align="left" class="fs12lh14 darkmodecustom" style="padding:8px 0px 0px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919" valign="top" width="15">
<span class="fs12lh14" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919">&bull;</span>
</td>
<td align="left" class="fs12lh14" style="padding:8px 0px 0px 0px; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919" valign="top">
<span class="fs12lh14" style="font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; line-height:19px; font-weight:400; color:#191919">Lorem ipsum dolor, sit amet consectetur adipisicing elit. </span>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left" style="padding:16px 0px 0px 0px; font-size:0px; line-height:0px" valign="top">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="middle">
<table border="0" cellpadding="0" cellspacing="0" class="bg-orange" width="auto">
<tr>
<!--[if (gte mso 9)|(IE)]>
<td width="20">&nbsp;</td>
<![endif]-->
<td align="center" class="h32px fs12lh14 bg-orange" width="144" height="44" style="color:#191919; font-family:'Roboto', 'Helvetica Neue', Arial, 'SF Pro', 'Corbel', 'Microsoft Sans Serif', 'Segoe UI', sans-serif; font-size:16px; mso-line-height-rule:exactly; line-height:16px; font-weight:600; padding:0" valign="middle">
<a class="fs12lh14 button_text" href="#" style="text-decoration:none; display:block; padding:8px 16px 8px 16px;" target="_blank">Lorem ipsum dolor </a>
</td>
<!--[if (gte mso 9)|(IE)]>
<td width="20">&nbsp;</td>
<![endif]-->
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!--[if (gte mso 9)|(IE)]>
</td>
</tr>
</table>
<![endif]-->
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
</details>
# 答案1
**得分**: 1
我建议使用 `@media (prefers-color-scheme: dark)` 媒体查询并创建一些类。然后将这些类应用于应该应用深色模式样式的元素。
示例:
```css
@media (prefers-color-scheme: dark) {
.darkmode-bg {
background-color: #ff5f00 !important;
}
.darkmode-color {
color: #ffffff !important;
}
}
很难回答这个问题,没有代码进行测试,但这是我建议的方法。来源。
还建议在您的 <head>
标签中添加以下元标签,以确保对于已启用深色模式的用户,电子邮件中启用了深色模式。
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
希望这有所帮助!
英文:
I would suggest using the @media (prefers-color-scheme: dark)
media query and creating a couple classes. Then apply those classes to the elements that should have the darkmode style applied.
Example:
@media (prefers-color-scheme: dark) {
.darkmode-bg {
background-color: #ff5f00 !important;
}
.darkmode-color {
color: #ffffff !important;
}
}
It's hard to answer this without code to test, but that would be what I'd suggest. Source.
It's also recommended to add these meta tags in your <head>
tag to ensure that Dark Mode is enabled in your email for users that have Dark Mode turned on.
<meta name="color-scheme" content="light dark">
<meta name="supported-color-schemes" content="light dark">
I hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论