Shining the light on HTML Emails Dark Modes

A presentation at Smashing Conference Freiburg 2022 in September 2022 in Freiburg, Germany by Rémi Parmentier

Slide 1

Slide 1

Smashing Conference Freiburg September 6th 2022

Slide 2

Slide 2

My name is Rémi Parmentier. I go by the nickname HTeuMeuLeu. And I am an email developer. Some of you might remember me because three years ago, I had the chance to be on this stage to talk about HTML emails already. And I ended this talk with a surprise announcement…

Slide 3

Slide 3

… with the launch of the website Caniemail.com. It is similar to caniuse.com but for email developers.

I’ve been maintaining this site for the past three years and a lot of things sure happened in the emails world.

Slide 4

Slide 4

Including this thing, just last month, where Microsoft had to push a quick fix to Outlook on Windows because Uber emails were crashing Outlook.

Slide 5

Slide 5

And the official reason from Microsoft was because the emails « used complex tables ». Which is hilarious to me, because the only reason we use tables in the first table is because Microsoft Outlook uses Word as a rendering engine and only understands tables in the first place.

Slide 6

Slide 6

This is a top contender for me in the Category of Email Clients That Break Because of a Thing They Force You To Do In The First Place.

Slide 7

Slide 7

But one of the most significant news from the past three years is the debut of dark modes in operating systems and email clients. From macOS to iOS, Android and Windows, every modern OS now features a dark mode option.

Slide 8

Slide 8

The idea is that you’ll check this setting on your operating system preferences, and then it’s up to any application to adjust its interface with a darker color scheme.

And this also applies to browsers and websites, and to email clients and HTML emails. But as you’d might expect, there are a few twists in emails that can make this a tiny bit more complicated.

Slide 9

Slide 9

In November 2020, Email Analytics tool Litmus estimated that 36% of iPhone users where opening their emails while in dark mode.

Just out of curiosity, who here uses dark mode (either on their phone or their computer)?

Slide 10

Slide 10

In the next 30 minutes or so, I’ll be shining the light on HTML Emails Dark Modes and share with you 5 tips to make your emails better in dark modes.

Slide 11

Slide 11

“modes”, plural. Because there’s not one dark mode, there are as many as there are email clients families. In this talk I’ll focus on Apple Mail, Outlook, Gmail.

Slide 12

Slide 12

You don’t get to chose whether your email has a dark mode or not. Email clients have something we call « Forced Dark Mode ». And if you don’t do anything your emails can look like hell. Literally.

Slide 13

Slide 13

Take this example from a web host. Super cute email, lovely colors and illustration. And here it is in Gmail Forced Dark Mode. Now to be fair, Gmail since adjusted its Forced Dark Mode algorithm changes big images like this.

Slide 14

Slide 14

Email Developer Alice Li (@AliceLiCode) wrote a fantastic guide on Litmus about Forced Dark Mode in email clients.

Slide 15

Slide 15

And for some of them, like Apple Mail on macOS, there are easy tweaks to avoid getting a white color turned in black. Which is to use #fffffe instead.

Slide 16

Slide 16

But my most important recommendation from this entire talk is Do Not Fight Dark Mode. Embrace It. Dark Mode is a user preference, the same way a user would increase their font size or read on a small device. So DO NOT try to keep a light email in dark mode. And one part of this « Do Something » is to test. You can test on real devices.

Slide 17

Slide 17

Or you can use one of these Dark Mode simulator for emails tools. Like this one from Proofjump.

Slide 18

Slide 18

And this one that was introduced just last week in Parcel.io. Which is a fantastic email coding tool.

Slide 19

Slide 19

My second tip is to use transparent images.

Slide 20

Slide 20

Take this email from Epic Games for Fortnite. Beautiful in light mode. But in Forced Dark Mode, here in Outlook.com, you can see how every image was sliced up.

Slide 21

Slide 21

But be thoughtful about when to use fully transparent images. Take a look at these social icons. They look ok in light mode. But they become unreadable in dark mode in Outlook com. So here, it might be better to fill these images with a white background inside the circles.

Slide 22

Slide 22

And one technique I found myself using a lot is adding an outline to transparent images.

Slide 23

Slide 23

Take that logo from that same email. No longer readable in dark mode.

Slide 24

Slide 24

What I’d do in such a case is go back to Photoshop, add an Outline to that content from the background color in light mode.

Slide 25

Slide 25

And voilà!

Slide 26

Slide 26

Here’s another example.

Slide 27

Slide 27

And another one.

Slide 28

Slide 28

Of course we don’t have to go through Forced Dark Modes. And we can be more proactive. And so we can use the dark mode media query. This is something relatively new that appeared at the same time as dark mode in OS.

Slide 29

Slide 29

And it looks like this. […] And then inside you’d fill your classes and styles changes, just like you would in a responsive media query.

Slide 30

Slide 30

There are handy tools in browser DevTools to test dark mode styles. Firefox has these super handy icons to easily switch from a light mode to dark mode.

Slide 31

Slide 31

Chrome also has a similar option but it’s much more complicated.

Slide 32

Slide 32

One neat thing about the dark mode media query is you can also use it to change images.

Slide 33

Slide 33

<picture>
   <source srcset="logo-dark.png" media="(prefers-color-scheme:dark)" />
   <img src="logo-light.png" alt="DuckDuckGo" />
</picture>

Slide 34

Slide 34

One quirk of the dark mode media query is that for it to work in Apple Mail, on macOS or iOS, you need to add the meta tag for older versions…

Slide 35

Slide 35

… or the CSS property for newer versions. This is totally not a requirement on the web. But weirdly it is in Apple Mail.

Slide 36

Slide 36

Now the overall support for this media query is… mitigated. Works well in Apple Mail. Works in Outlook on macOS, iOS or Outlook.com. But other than that, not so much. One thing that’s been bothering me with outlook.com…

Slide 37

Slide 37

… is that since it includes a little icon to switch your email in dark mode or light mode. But because we’re in a webmail, using this switch doesn’t do anything if you use dark mode styles.

Slide 38

Slide 38

So this is my tip number 4 and how to use custom styles and make your emails properly react to outlook.com dark mode.

Slide 39

Slide 39

So take this piece of code. In outlook.com it would be turned like this.

Slide 40

Slide 40

Outlook.com applies the prefix « x_ » to any class name. This is a good practice for security.

Slide 41

Slide 41

And yes, that pun here was totally intended. Because not only The X Files was my favorite TV show growing up. But also because it taught me the only words I can possibly speak in German. « Ich habe keine unruhe » but maybe a little bit when coding emails.

Slide 42

Slide 42

Then in dark mode, Outlook.com adds these data attributes.

Slide 43

Slide 43

So back to our initial code…

Slide 44

Slide 44

The first step is to exclude Outlook.com from media query styles. For this we append a :not([class^="x_"]) pseudo-class to every class selector.

Slide 45

Slide 45

Then we duplicate styles only for Outlook.com using a [data-ogsb] attribute.

Slide 46

Slide 46

But then there’s Gmail. Gmail is really a bad student here because not only it doesn’t support the dark mode media query. But it also doesn’t have any sort of trickery or targeting to differentiate light and dark mode. And not only that, but it’s also sometimes terrible at Forced Dark Mode.

Slide 47

Slide 47

Especially if you already have a dark theme email. Gmail on iOS will try to invert it anyway.

Slide 48

Slide 48

Or this one from Google themselves.

Slide 49

Slide 49

This is one of favorite thing in the category of Email Clients That Break Because Of A Thing They Force You To Do In The First Place.

Slide 50

Slide 50

One cool thing about Gmail though is that it supports CSS Blend Modes.

Blends Modes is that thing in Photoshop that makes your developer hates you because it becomes horrible to export any individual layer.

But also Blend Modes are about blending colors from overlapping layers.

Slide 51

Slide 51

So let’s take this green button with white text. In Gmail in dark mode, the background color turns into a dark green, and the text turns into black.

Slide 52

Slide 52

Our base code looks like this:

<div style="color:#fff; background:#4d86eb;">
Get A Reward Cards
</div>

(Don’t use <div>s for buttons. This was a bad example for the sake of having less on screen.)

Slide 53

Slide 53

Gmail in dark mode turns it into the following:

<div style="color:#000; background:#007740;">
Get A Reward Cards
</div>

Slide 54

Slide 54

The first thing we’ll do is maintain the background color by using a linear-gradient. They’re well supported in Gmail and are not changed in dark mode.

Slide 55

Slide 55

In Gmail in dark mode, only the background-color is changed, not the background-image.

Slide 56

Slide 56

So this is what we have now.

Slide 57

Slide 57

Now let’s add blend modes into the mix. We’re going to need two blend modes. The first one will be a difference blend mode.

Slide 58

Slide 58

The second one will be a screen blend mode. I introduce them in this order because this is the order they’ll be applied (inside out based on the HTML).

Slide 59

Slide 59

If we look back at what it looks like…

Slide 60

Slide 60

… we can kind of represent it like this. We now have three nested layers with different background colors and blend modes. So let’s see what happens when we apply the first blend mode.

Slide 61

Slide 61

According to the spec, mix-blend-mode:difference “subtracts the darker of the two constituent colors from the lighter color.”

Yay, that didn’t make much sense for me either.

Slide 62

Slide 62

But this formula made things clearer. The result is the absolute value of the difference between our source color (Cs) and our backdrop color (Cb).

B(Cb, Cs) = |Cb - Cs|

Slide 63

Slide 63

So let’s see how this looks like in light mode.

Slide 64

Slide 64

The white text from the source color (Cs) is blended with the black from the backdrop color (Cb).

Slide 65

Slide 65

The white text becomes…

B(Cb, Cs) = |Cb - Cs| = |#000 - #fff| = #fff

White!

Slide 66

Slide 66

Now let’s do the same for the black background color from the source merged with the black background from the backdrop.

Slide 67

Slide 67

The black background becomes…

B(Cb, Cs) = |Cb - Cs| = |#000 - #000| = #000

Black!

Slide 68

Slide 68

Now let’s do the same but in dark mode.

Slide 69

Slide 69

The black text (changed by Gmail) from the source color (Cs) is blended with the white (changed by Gmail) from the backdrop color (Cb).

Slide 70

Slide 70

The black text becomes…

B(Cb, Cs) = |Cb - Cs| = |#fff - #000|

White!

Slide 71

Slide 71

Now let’s do the same for the white background color from the source merged with the white background from the backdrop.

Slide 72

Slide 72

The white background becomes…

B(Cb, Cs) = |Cb - Cs| = |#fff - #fff| = #000

Black!

Slide 73

Slide 73

We now end up with the same thing in both light and dark mode. So let’s apply our second blend mode!

Slide 74

Slide 74

According to the spec, mix-blend-mode:screen “multiplies the complements of the backdrop and source color values, then complements the result”.

Yeah, that made no sense to me either.

Slide 75

Slide 75

But again, the mathematical formula made it a bit clearer.

B(Cb, Cs) = 1 - [(1 - Cb) * (1 - Cs)]

Slide 76

Slide 76

Which we can develop into the following:

B(Cb, Cs) = 1 - [(1 - Cb) * (1 - Cs)]
          = Cb + Cs - (Cb * Cs)

Slide 77

Slide 77

So let’s visualize what it looks like.

Slide 78

Slide 78

The white text becomes…

B(Cb, Cs) = Cb + Cs - (Cb * Cs) = #4d86eb + #fff - (#4d86eb)

Slide 79

Slide 79

… white!

Slide 80

Slide 80

The black background becomes…

B(Cb, Cs) = Cb + Cs - (Cb * Cs) = #4d86eb + #000 - (#000)

Slide 81

Slide 81

Green!

Slide 82

Slide 82

And that’s how we end up with the same button colors in both light and dark mode.

Slide 83

Slide 83

I wrote a fully detailed article if you want to learn more about this.

Source : https://www.hteumeuleu.com/2021/fixing-gmail-dark-mode-css-blend-modes/

Slide 84

Slide 84

One final trick I wanted to share with you is for Outlook on Windows. I won’t go into much details about it, but I want you to know about it.

Slide 85

Slide 85

This was created by email wizard Nicole Merlin. It uses Word’s gradient to force specific colors to be maintained in both light and dark mode.

Source : https://webdesign.tutsplus.com/tutorials/how-to-fix-outlook-dark-mode-problems—cms-37718

Slide 86

Slide 86

That’s all I had! To end up with a good news, Microsoft released a new version of Outlook on Windows. And for the first time in 15 years, it no longer uses Word’s rendering engine. It uses EdgeView, based on Chromium, just like in Edge.

Source : https://twitter.com/HTeuMeuLeu/status/1527761355167739907

Slide 87

Slide 87

THANK YOU

@HTeuMeuLeu remi@hteumeuleu.fr https://www.hteumeuleu.com