Targeting The New Responsive Gmail With CSS [Updated]

Update 9/18/2017: Gmail made a change that requires some changes to targeting techniques. See updates below.

The new Gmail update with responsive email support has been a blessing for email designers everywhere.

However the update also creates a few issues for those of us that are interested in pushing the envelope with advanced CSS. This is because it is now harder to use CSS to distinguish Gmail that doesn’t support advanced CSS3 like interactivity and animations from clients like the iPhone email client that does.

Thankfully, uber email developer Mark Robbins from Rebelmail came up with a solution that allows you to apply a set of CSS that is only active within Gmail and Inbox.

Targeting Gmail

Update: Gmail made some changes on Sept 18, 2017 which affected the techniques used to target the clients. However, Mark Robbins from Rebel quickly came to the rescue. The following has been updated to target Gmail post 9/18.

Here’s the code to target all Gmail and Inbox versions (Webmail, iOS, Android).

In order to implement this technique your markup must contain
1) A DOCTYPE.
2) A body with a class name (ie. “body”)
3) An element with a class name (ie. “gmail”)

What happens is Gmail converts the doctype to <u></u> elements and places it adjacent to the body container (transformed into a div with classname body). Since Gmail is the only client that does this, you are able to target Gmail using the CSS in the code block above.

Shortcut

Cosmin commented below that you can use a shortcut that does not require adding a class to the body of your email. However this technique requires that you NOT use the <u> tag anywhere in your email content.


Targeting Gmail on Android Only

The CSS below allows you to target Gmail on Android (excludes Inbox).


Shortcut

Like the generic version, you can use a shortcut that does not require adding a class name to the body as well.


(Originally posted Oct 8, 2016. Updated Sept 19, 2017)



Latest Comments
  1. Tom

    This is nifty and all, but I can’t get this working in any Gmail apps for iOS or Android (unless Email on Acid results are acting up).

    It’s working for Gmail web client though – am I missing something? Wish the Gmail Apps just worked normally post Gmail media query ‘fix’. Grr.

  2. MrMeow

    This is working for me in gmail – with the use of the section tags, miss these out and it won’t work.

    I’m struggling to target the inbox app now though :/

    Will post with any update.

  3. Cosmin

    I have a scenario where the ESP will replace my body tag with a ‘default’ one, meaning any class I add to it will be removed.
    So I was trying to get around this somehow, and found that you can use a sibling selector instead of applying a class to the body:

    Currently, Google replaces your tag with a div, while preserving the attributes. A is inserted before this div, and the ~ sibling selector is supported, so you can easily target without relying on the body tag class.

    Tested in Gmail and Inbox on iOS 10, as well as Gmail on Android 6 & 7.

    • Cosmin

      Some of the code went missing in my reply, so I’ll rewrite:

      Currently, Google replaces your body tag with a div, while preserving the attributes. A u tag is inserted before this div, and the ~ sibling selector is supported, so you can easily target without relying on the body tag class.

  4. AJ

    Hello, does this only work with HTML5 or can I still use the transitional doctype?

    • Justin

      I believe this works as long as you have a doctype.

  5. Harry Binswanger

    As of 1/4/17, it’s not working for me either. I have this in the head section:
    u + .body > .gmail {
    display: block !important;
    }

    and this in the body:

    [div class="gmail" style="display: none;"]THIS IS GMAIL OR INBOX [/div]
    It’s visible for all clients, not just Gmail.
    I don’t see any typos there (can’t put in angle brackets or the code disappears from the comment!)

  6. Evan

    I was having the same issue until I wrapped the divs in a section:
    [section]
    [div class="gmail" style="display: none;"]THIS IS GMAIL OR INBOX [/div]
    [/section]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">