HTML email in Gmail - CSS style attribute removed
Asked Answered
K

8

45

I'm working on an HTML email and I am using MailChimp's Responsive Email Templates in combination with their CSS inliner tool. For the most part, the email looks great across the myriad of email clients, but in Gmail things are horribly misrepresented.

If I use Gmail's "Show original" option from the drop down menu next to the reply arrow, the original HTML is different from what is actually displayed in the email client. I can confirm this by inspecting the element with the developer tools. This happens on desktop and mobile; the email client is removing inline style attributes from elements.

It seems that one of the criteria for removing the style attribute is if the element also contains a class. Can anyone confirm this? Also, it appears to remove all style attributes from a table tag regardless. Can anyone confirm this as well?

What are the workarounds for this?

Screenshots of email with source in Gmail and Yahoo included below.


Screenshot of email in Gmail with source displayed via Chrome developer tools

enter image description here


Screenshot of email in Yahoo with source displayed via Chrome developer tools

enter image description here

Kristoferkristoffer answered 11/7, 2013 at 20:1 Comment(1)
I face the same issue as you 10 year later. When users reply to the email, the original content is mutated. Please share with me how did you fix this?Manstopper
B
37

As someone who regularly codes emails for marketing campaigns at my job, I feel your pain. Gmail, along with many other email clients, can be a bit funky to code for. For one, it strips out any CSS that's outside the body. So putting in things like media queries and document level styles don't work. The best piece of advice I can give you is hand code your inline CSS and try to avoid anything fancy. In fact, if you can use an HTML attribute to do your styling, use that in place of any CSS. An example would be bgcolor instead of background-color.

Here is an article related to your specific problem I found. Best of luck.

Butterwort answered 12/7, 2013 at 2:56 Comment(2)
Thought I would share this responsive template that has been outstanding for my use. It works in nearly every email client, including desktop versions of Outlook (which Zurb does not support). Hope it helps someone else emailonacid.com/blog/details/C13/…Kristoferkristoffer
When I tested it it didn't work on all clients. They had a problem with tables dropping down, hopefully they fixed it.Aerolite
P
32

I just checked now: Gmail strips out your inline style attribute if you don't put spaces between ; , , and : chars

this works fine:

<span style="color: #8d8c87; display: block; font-family: Arial, sans-serif; font-size: 12px; line-height: 120%; text-align:center;">text</span>

but the same rule will be stripped out if you don't use spaces; if you write this:

<span class="small-text" style="color:#8d8c87;display:block;font-family:'Titillium Web',Arial,sans-serif;font-size:12px;line-height:120%;text-align:center">text</span>

you will get this output on Gmail client:

<span>text</span>

EDIT:

In addition to this behavior, I noticed that Gmail tends to strip out the inline style if you declare a font-family inside a nested <table> or a <td>, I'm still not sure on what is the general rule of its preprocessor, I checked on Google but I can't find any official documentation about html mail composition for Gmail.

Papery answered 26/6, 2016 at 17:36 Comment(6)
I made some changes based on this and I still can't believe this solves it. How did the internet get so badly broken :(Keare
@MattLacey this is NOT correct. Gmail stripes style due to its invalid.Gautier
Yeah, what @DatTT said. Run your email through an HTML/CSS validation tool. There's often some syntax error that Gmail doesn't like, even though the email renders fine in a normal web browser.Ticktacktoe
In my case the issue was "font-family inside a nested <table> or a <td>". Thanks manTerrain
Same here, font-family was the issue but I had to strip out box-shadow as well for the style to not be removedAdjutant
I had eg. rgb(79 70 229) causing the style to be stripped. Included commas before Gmail accepts it, ie. it should be rgb(79, 70, 229). font-family in nested <table> or <td> wasn't an issue for me.Combustion
F
5

There are multiple work arounds for gmail. Gmail is quite a joker when it comes to your styling, as it'll strip what it doesn't like completely out of your e-mail.

Here are some little tips:

Gmail adds white space between images, or stretched the size of it's container td: You can correct this by specifying style="display:block" on your images (Make sure your TD has the same width and/or height as your image).

Gmail renders black links as blue links: Yes, this is ugly. Use #000001 instead of #000000.

Gmail makes phone numbers clickable: That may be a good thing or not, depending on your client, but one way to work around that is to include an empty anchor tag with styles around the phone number.

Ex: <a style="color:#000001; text-decoration:none;>555 555-5555</a>. It will make you feel ashamed of your code, but it's an effective little hack.

Faro answered 31/7, 2013 at 18:24 Comment(0)
S
3

Just thought I'd add this in to the mix. I encountered as this problem as well and when looking at the raw source I realized that the 8-bit encoding was splitting lines in odd places due to the 1000 character limit, so I was ending up with content like this:

<td style="border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; padding: 7px 14px">734 340 9795</td></tr><tr> <td sty
 le="border-right: 1px solid #DDDDDD; border-bottom: 1px solid #DDDDDD; padding: 7px 14px">

Solution is to base64 encode the content and use chunk split or whatever tools you have to accomplish the same.

In php I did $html = chunk_split(base64_encode($html)) and then set Content-Transfer-Encoding: base64. Now gmail loves me.

Senatorial answered 30/11, 2015 at 8:43 Comment(0)
W
3

Check your CSS for typos

The reason that Gmail strips certain styles might have changed since the OP in 2011, but here's what I discovered in 2020...

In a <style> block in the <head> of my email I had a typo that was essentially (note the "d"):

.link {
  text-decoration: underline;d
}

Inspecting the email, I could see that the rule was there (along with everything else) in the <style> block. However, every place I had used that class in the email had been stripped out by Gmail. So <p class="link"> ... </p> magically became <p> ... </p>.

In addition, all rules declared under .link { ... } were also stripped from the HTML document. So - in the example below - class="small" would work, but class="link" and class="headline" would be stripped from the HTML.

<style>
  .small {
    font-size: 12px;
  }
  .link {
    text-decoration: underline;d
  }
  .headline {
    font-size: 18px;
  }
</style>
Winnick answered 21/8, 2020 at 17:53 Comment(1)
Gmail will also do this for inline styles! It'll just remove the whole style attribute from the tag. For example, if you've got a stray !important hanging out at the end like this, <a href="https://example.com" style="padding 20px; !important;">, your link won't have any padding.Ticktacktoe
E
2

I had the same problem for some elements in my email message. You need to remove all box-shadow from your CSS. Gmail doesn't like it for some reason.

Ellon answered 17/8, 2022 at 7:49 Comment(1)
Yes, this was my case. In fact is a combination of all these suggestions. Doh!Ambrotype
B
1

Using CSS selectors is another workaround that I was able to use. In my example I am trying to format a HTML table. I found gmail stripped out all the ID and CLASS attributes rendering my CSS useless.

After some investigation I noticed gmail did not remove my title attribute. So I created a CSS rule using a title selector. This appeares to work fine:

[title~=myTitle] {background: black; color: white;}

I don't think this is best practice, but I thought I would mention it.

Balliol answered 19/12, 2013 at 16:28 Comment(4)
Cant get this to work. I also tried it with the Lang attribute as the title attribute displays a little tooltip. Gmail seems to remove [title~=myTitle] {background: black; color: white;} from my css but the code below shows, but just doesnt style my element. [title~=myTitle] {background: black; color: white;}Begum
What is the title of your element? Cause if you use the CSS rule exactly as I have it your element title should be title="myTitle"Balliol
I changed it to "container" to match my title attribute.Begum
Where in your html email is your CSS rule?Balliol
S
-1

I'm using Maizzle for rendering emails.

I have forgot to add the inlineCSS: true to my config.production.js:

module.exports = {
  inlineCSS: true,
}

After that, it worked completely fine!

Spine answered 24/12, 2022 at 9:49 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.