Search code examples
cssemailgmailhtml-email

Gmail changing max-width in % to px?


I'm currently working on a html email for various clients, and having some strange issues using gmail (yay) - specifically, when viewing an email through the browser. (No issues in the app)

The issue only really comes about when using the browser on a small screen (eg mobile) - all the images are displaying too wide, despite a max-width, meaning the layout is stretched and requires horizontal scrolling. Whilst it causes no issues on desktop, the same thing happens to the code

On my images, I've set

style="max-width:100% !important;"

inline on each image. I also have a <style> block in the head with

img {max-width: 100% !important;}

When inspecting the image element (both on my phone and pc), I'm seeing no sign of the max-width from the head, which is not totally unexpected.

What's weird is that each image still has a max-width set inline- BUT it is no longer in %, but in px.

Inspected in the pc browser, the inline max-width now says

    max-width: 1920px;

Viewed on my phone (android, inline max-width is

    max-width: 767px;

In this case, the image is now WAY too wide and is breaking the layout. The same thing happens on all images, regardless of their actual size.

I'm not seeing any message telling me that the email has been adapted for my screen, or any option to turn this on or off.

Has anyone had this happen before? Any ideas on how to prevent gmail messing with the max-width, or getting it to respect the max-width in the head? (I've avoided using classes or id's to attach styles from the head as as far as I know, google strips these out)

Even weirder (or maybe I'm losing it...) I'm SURE this was working yesterday...

UPDATE: think I've solved why it was apparently working yesterday - it wasn't - changes I'd made in dev tools weren't clearing when I reloaded, so the max-width I'd added in as a test (replicating a max-width in the head) was still there...

After a bit more playing, adding width: 100% !important; inline on all wide images seems to work as a fix - gmail doesn't mess with the value. Would still be interesting to know why it changes the value of the max-width though, if anyone has any clues!


Solution

  • Gmail is notorious for misusing its maxwidths for set layouts.

    try and avoid setting max-width to a percentage value for elements that aren't high level containers. you will run into weird rendering on things like gmail app as well. typically gmail app converts all widths into max-widths so it can crunch your content but still maintain the structure at higher screen resolutions.

    gmail webapp does something similar. it restricts the widths you can set an element to so you can't add things that are wider than your display, at which the gmail web app is displaying at 100%.

    gmail mobile app - forces your content into a 320px/480px wide box

    gmail desktop app - forces your content to never exceed your screen res

    Gmail desktop treats styles in the head quite strange anyway, it completely ignores most classes/id's (don't listen to people that say it strips them completely) if you use other tags as identifiers other than #id and .class it will apply those styles. e.g.

     * [lang~="identifier"]
    

    lang is pretty much the only tag that you can use that wont get in the way of tags that are being used, i.e. Alt, Title and Href.

    Just be careful, if you are using media queries or anything that starts with "@" in the style tag gmail may potentially (depends on how its feeling that day) completely remove that style tag. You can avoid this by adding those styles in a style tag in the body though.

    in conclusion. Don't rely on max-width to control your layout from inside out, set pixel values to limit width="100%".

    Hope this helps