简体   繁体   English

如何在页面中呈现电子邮件模板?

[英]How to render email template in a page?

I have an email template which I wants to render in a page. 我有一个要在页面中呈现的电子邮件模板。 email template have their own CSS like : 电子邮件模板具有自己的CSS,例如:

<style>
body{
   font-size : 20px;
}
</style>

, when I am trying to render email template on my web page their CSS is overriding my page CSS. ,当我尝试在网页上呈现电子邮件模板时,其CSS会覆盖我的页面CSS。 How to resolve this overriding issue ? 如何解决这个首要问题? , I want to render template like gmail. ,我想呈现类似gmail的模板。

Note : Dont want to render inside iframe. 注意:不想在iframe中进行渲染。

The error you are getting is logical, because the webpage and email template both having same style tag name such "body" tag is in both aspects. 您得到的错误是合乎逻辑的,因为网页和电子邮件模板都具有相同的样式标签名称,例如“ body”标签。

You have 2 ways 1) do all in-line styling for email template (This is hard to do). 您有2种方法1)对电子邮件模板进行所有内联样式设置(这很难做到)。 or 2)Change the style tag names of the email template both in html and CSS. 或2)更改html和CSS中电子邮件模板的样式标签名称。 (This is easy way to complete your task just add "email" before each style tag of your email template's html and css pages). (这是完成任务的简单方法,只需在电子邮件模板的html和css页面的每个样式标签之前添加“电子邮件”)。

Then create a div tag in you web page with appropriate height and width as your email template and put your template into that. 然后在网页中创建一个高度和宽度适当的div标签作为您的电子邮件模板,然后将模板放入其中。

Option 1 When doing email styling, keep the styles inline. 选项1在进行电子邮件样式设置时,请保持样式内联。 While CSS is supported by many email clients today, the best way to prevent them from interfering with the rest of the page is to restrict the styles to the individual cells. 尽管当今许多电子邮件客户端都支持CSS,但防止它们干扰页面其余部分的最佳方法是将样式限制为单个单元格。

In essence, you're gonna have a lot of: 本质上,您将拥有很多:

<td style="font-size:20px">
    content here
</td>

Option 2 If you cannot change the email HTML for some reason, the other way is to adapt your page CSS to be stricter, and more targeted to specific elements on the page. 选项2如果由于某种原因无法更改电子邮件HTML,则另一种方法是使页面CSS更严格,并针对页面上的特定元素。

Assuming your page has a header, a main content and a footer, and the email is appearing inside a section of your main-content, you should give each of these blocks an id. 假设您的页面具有页眉,主要内容和页脚,并且电子邮件显示在主要内容的一部分内,则应为每个块指定一个ID。 Then, your page CSS could look like this: 然后,页面CSS可能如下所示:

/***
 * Header
 ***/
#header {
    font-size:16px;
}
#header-nav {
    font-size:15px;
}
#header-nav > a { /* affects all links inside header-nav */
    font-size:14px;
}

/***
 * Main Content
 ***/
#main {
    font-size:18px;
}
#email-section {
    /* we expect font-size of the email to be declared within itself.
       And we are not worried that its style would overwrite any other
     */
}
#some-other-section {
    font-size:14px;
}
#some-other-section > p { /* all paragraphs in this section */
    font-size:16px;
}

/***
 * Footer
 ***/
#footer {
    font-size: 16px
}

If you follow this discipline of targeting your elements very specifically with your CSS, you usually have little to worry about when you import external stylesheets into your page. 如果您遵循使用CSS专门针对元素进行定位的原则,那么在将外部样式表导入到页面中时,通常无需担心。

Often, you see people using too loose selector rules that apply to too many things. 通常,您会看到人们使用适用于太多事物的过于宽松的选择器规则。 It's important that CSS developers understand the cascading and specificity well enough. CSS开发人员必须充分了解级联和特定性,这一点很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM