Email Development

Notes from the Dev: Using CSS Variables in an Interactive Wordle EmailĀ 

Image for Notes from the Dev: Using CSS Variables in an Interactive Wordle EmailĀ 
May 9, 2023

Interactive emails are showstoppers that really help you stand out in the inbox. But theyā€™re not easy to pull off unless youā€™ve got some serious email coding skills.

In this episode of Notes from the Dev: Video Edition, weā€™ve got just the guy (or bloke) for the job. Jay Oram of the digital agency ActionRocket joined me to explain how he developed a Wordle game for ActionRocketā€™s weekly newsletter.

To refresh your memory… People went crazy for this word guessing game last year. Wordle was the most-Googled word of 2022, and the gameā€™s creator sold it to the New York Times in a seven-figure deal.

Sometime around the height of the Wordle craze, the email geek community questioned who would be the first to deliver a Wordle game for the inbox. Jay rose to the challenge. His secret trick? Using CSS variables.

Watch the video below to find out how he built the interactive Wordle email and keep reading for all the details.

How Jayā€™s Wordle email game works

When people solve a Wordle puzzle, they typically have six chances to guess the word of the day using the process of elimination. Players make a guess by typing in a word. If a letter isnā€™t in the answer at all, the box itā€™s in will be gray. Letters that are in the word, but in the wrong spot, are yellow. And the right letter in the right spot will be green.

To build this gamified email, Jay needed an interactive keyboard, the ability to clear and reset the puzzle, a way to change colors of letter tiles based on the subscriberā€™s input, and a whole bunch of CSS variables for all the different possibilities.

Thereā€™s even a surprise animated GIF of the party blob emoji after you figure it out. Hereā€™s how it looks when you win:

Jay says it took him three or four hours to get his interactive Wordle email working. (Of course, heā€™s a living legend among email geeks – so donā€™t compare yourself.)

Using the checkbox method for interactivity

To make it happen, he used the trusty checkbox method for interactive emails. Jay wrote about this for the Email on Acid blog several years ago. The method involves using the :checked CSS pseudo-selector with a form that has radio buttons or checkboxes to hide and reveal content in an interactive email.

Basically, what youā€™re doing when you code emails this way is replicating JavaScript functionality that toggles things on and off.

For example, hereā€™s an interesting use: Jay coded separate keyboards for each of the five letters in the puzzle. When a subscriber clicks a letter on the keyboard, it hides that keyboard and reveals one for entering the next letter. But because they are identical keyboards, and it happens so quickly, recipients donā€™t notice.

Note that the so-called ā€œclick-to-revealā€ or ā€œpunched cardā€ method is only supported on Apple Mail, Outlook for Mac, and some other email clients using the WebKit rendering engine. The same goes for email client support for CSS variables, which are a big part of the Wordle email.

So, be sure you have a view in browser link for subscribers on Gmail. For interactivity in Gmail, you could try creating a Wordle game using the AMP for Email component amp-bind, which the folks at Mailmodo have done.

What are CSS variables?

Also known as custom properties or cascading variables, CSS variables are entities containing specific values that the coder defines and that can be reused and repeated. The variables provide an effective way to manage property values. Web developers often use custom properties to cut down on repetitive CSS throughout a website, which makes things easier to update.

You can create CSS variables using the :root pseudo-selector when you want the variables to be applied globally, but they can be applied to any specific element. You insert or access them using the var() function. CSS custom properties are formatted using two hyphens before the name, and the basic syntax would look something like this:

                            

                                :root {
  --name-of-variable: 'Update the value here';
}

css property: var(--name-of-variable); 
                            
                        

If youā€™ve never used CSS variables in email code, thatā€™s no big surprise. Jay says most developers donā€™t, and its support is limited to Apple Mail and Outlook for Mac along with some of the smaller email clients.

Using CSS variables in interactive emails

In the Wordle email, CSS variables are used to define and display options for both letters and colors.

Jay says the main reason for doing this is to avoid repeating tons of code in interactive emails. In the video, he walks us through some basics of using CSS variables in email. Here are some important notes:

  1. Each variable is only related to the element it is within, but custom properties must be in a CSS selector.
  2. Place CSS variables in a :root selector if you want to use them throughout the entire document. But thatā€™s not required.
  3. You decide what to name CSS variables. Make sure they start with two hyphens, end with a colon, and donā€™t contain any special characters. They are also case sensitive.
  4. Variables can be colors, numbers, text, spacing ā€“ just about anything you can code.
  5. Use a comma after the variable name to define a default value that can serve as a fallback.
  6. You can also use CSS variables within media queries to create different experiences depending on screen size.

Hereā€™s Jayā€™s example code for color variables. It applies a red font color to the entire document while specifying purple text in a specific module of the email. It also has a fallback/default color of pink.

                            

                                <style>
:root {
--fontcolor: red;

}

.module {
--fontcolor: rebeccapurple;

}

.testcolor {
color: var(--fontcolor, pink)
font-size: 40px;

}

</style>
                            
                        

See all of Jay’s example code and play around with it on Parcel.

So again, the main purpose of CSS variables is to reduce repetitive coding in interactive emails. It supports the DRY (Donā€™t Repeat Yourself) approach to coding. To read a more in-depth explanation, check out the CSS Tricks guide to custom properties.

Another potential use for CSS variables that Jay and I discussed is using them to manage and update brand colors in an email design system.

More email coding tips from Jay Oram

Not only is Jay Oram an awesome email coder, heā€™s also a super generous creator of content for email geeks. Here are just a few of the popular articles Jay has written for our blog:

Jay was also a presenter at Email Camp 2022. Watch him live code a click-to-reveal module and hear more about using CSS variables in interactive emails.

Plus, Jay and I recently faced off in a debate over using artificial intelligence for email development. Watch a replay of the webinar Email Geeks vs. ChatGPT to find out about our thoughts and opinions.

Follow @emailjay_ on Twitter and reach out to him in the Email Geeks Slack community. And if youā€™re not already subscribed to Email Weekly from ActionRocket, do yourself a favor and sign up right now. When the ActionRocket team isnā€™t using their newsletter for cool inbox experiments like the Wordle email, theyā€™re delivering an excellent collection of curated content for marketers of all types.

While youā€™re in a subscribing mood, why not subscribe to Email on Acidā€™s YouTube channel? That way youā€™ll never miss an episode of Notes from the Dev.

Related blogs