Styles not working in Gmail
Asked Answered
A

6

19

I'm working on sending emails to various email clients(such as yahoo,hotmail,gmail,....).

I have a div with id OrderInfo inside that I have a variable which generates a dynamic table.

HTML

<div id="OrderInfo">
  variable 
</div>

The dynamic table generates headers(th) with lower case, so I want to change that to uppercase and few more styling. So I have written a selectors

CSS

#OrderInfo table tr th {
  text-transform: uppercase;
  background-color: #737373;
  color: white;
}

This is working fine for yahoo, hotmail but not for gmail.

I came across that only inline styles work for gmail but how can I the styles of modify a dynamic one.

I have no control on the variable (I mentioned in the div) it generates a table with values which processes while sending to the client.

So I cannot keep a static table and cannot change the way it renders

Archibald answered 18/2, 2016 at 12:9 Comment(1)
can we write selectors inline?Archibald
C
36

gmail as well as some other web and desktop/mobile clients strips away css stylesheets either imported or embedded in a <style>...</style> node in the head

Put them inline:

<div id="OrderInfo">
    <table>
        <tr>
            <td style="text-transform: uppercase; background-color: #737373; color: white;">
                <!-- .......... -->
            </td>
        </tr>
    </table>
</div>

As a more general advice: building email html is not trivial as final result may vary a lot depending on the recipent's mail client.

The general rule is to make the html as simple as possible, avoiding "modern" css features; use nested tables instead of divs when possible (some says build the html as if you were building a 15 years ago webpage).

The above is very general and may not be always true.

There are several resources online that gives advices and rules on how to make an html email or template.

Finally the only and one rule to always follow if you want to be sure of the result: test your messages with various client

Cabinetmaker answered 18/2, 2016 at 12:14 Comment(10)
I would like to give it to th's of a dynamic(generated at run time) table not to the divArchibald
(updated the answer): ok put the css online on the <td> element - or whatever element needs to get styled.Cabinetmaker
I would like to give it to th's of a dynamic(generated at run time) table not to the div. I cannot put that static.It will be generated by some logic, which I have no control on it @CabinetmakerArchibald
You mean that you're not generating the html of the table, right?Cabinetmaker
In that case you can take the html as a string and edit it at runtime adding the css inline before sending the whole message to the client. This is not trivial at all and goes beyond the original question. Anywat there are tools to manipulate html content stored in a string.Cabinetmaker
Yes, I'm not generating it, but I have to modify it @CabinetmakerArchibald
Can I have a link or sample eg of what you explained about string @CabinetmakerArchibald
@DheerajPatnaik: ok, it requires to you some extra work. See my previous comment.Cabinetmaker
If you are on PHP start from here: php.net/manual/en/domdocument.loadhtml.phpCabinetmaker
I'm working on htm and whatever manipulations I have to do has to be through html or stylesArchibald
C
14

UPDATE 2018

GMAIL now and from a while ago has been supporting embedded CSS, so you can use CSS inside tag <style> in head, it even allow/supports the use of media queries.


OLD ANSWER

Gmail doesn't support embedded CSS, you need to use inline styles, take a look at this

12 Things you MUST Know when Developing Emails for Gmail and Gmail Mobile Apps

Here is what you could do:

<th bgcolor="#737373" style="text-transform: uppercase; color:white></th>

Conceptualize answered 18/2, 2016 at 12:14 Comment(1)
Gmail client will only allow small tables to be styled dynamically. If too large it will provide a view without the dynamic styling.Proliferous
F
5

Many email service provide not support to css included in email template. Instead use inline css.

Also, Email template should be formed using tables as it only support HTML3. You can use HTML4/5 elements withing td tags

Do check this link. It will help you to build email template.

Fear answered 18/2, 2016 at 12:13 Comment(0)
C
1

Gmail has certain limitations and restrictions when it comes to rendering CSS styles in emails. This is due to security concerns and the need for consistent user experience across different email clients and devices.

Maybe try some inline styles....

As text-transform, background-color and color properties are supported by gmail.

Checkoff answered 14/6, 2023 at 4:47 Comment(0)
M
0

Try with this styling making your link red with no special effect for the hover situation:

a:link{color: red}
a:visited{color: red}
a:hover{color: red}
a:active{color: red}

This works fine for me, but if anyone of the 4 statements are missing it will not work neither in a gmail client nor in Outlook. They must also appear in the order shown above.

Markhor answered 8/5, 2017 at 6:26 Comment(0)
E
0
  1. Use inline CSS - no references to URLs.
  2. CSS HAS to be within the .
Erdei answered 24/3 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.