Friday, July 21, 2023
HomeEmail MarketingSuperior MJML Coding Strategies for E-mail Improvement

Superior MJML Coding Strategies for E-mail Improvement


Notes from the Dev logo yellow


It’s time for the thrilling conclusion of our journey into one of the in style e mail frameworks accessible: MJML (Mailjet Markup Language). Okay, so it wasn’t an enormous cliffhanger or something. However we’re positively excited to share the second half of this interview with you.

Once we final left Nicole Hickman, she simply completed exhibiting us the fundamentals of find out how to use MJML to shortly and effectively code responsive HTML emails. This time, we’re diving a bit of deeper to find superior MJML methods.

I requested Nicole a few of the commonest questions that I’ve seen e mail geeks questioning concerning the MJML framework. That features darkish mode, picture swapping, and overlapping content material in emails.

A part of the great thing about MJML is its simplicity, as we noticed in Half 1 of this interview. However what occurs when you’ll want to take issues a bit of additional that the framework permits? Take a look at Nicole’s suggestions within the video beneath. And don’t overlook to subscribe to E-mail on Acid by Sinch on YouTube to catch new episodes of Notes from the Dev: Video Version.

(Go to our Useful resource Middle to view the total transcription of this episode) 

Introducing the <mj-raw> tag

Relating to superior MJML methods and conventional HTML e mail growth, there’s a means you may get the very best of each worlds.

I’ll minimize to the chase right here. The <mj-raw> tag is what you’ll want when it’s important to “escape of the field” of MJML, as Nicole put it. Mainly, every time she needs to code one thing for which there’s no easy answer with MJML markup, Nicole makes use of <mj-raw> to incorporate uncooked, responsive HTML.

Within the first installment of our exploration into MJML, you’ll recall how Nicole defined that parts like textual content, buttons, and tables at all times get enclosed in <mj-section> and <mj-column> tags.

Using <mj-raw> is an exception. It’s an ending tag that received’t embrace any MJML parts. As an alternative, you utilize it to code the identical means you’d in a standard HTML file.

“There are plenty of issues that MJML can do all by itself. However when you’ve got the necessity to do one thing that might be a bit of extra cumbersome to try to do throughout the MJML itself, you’ll be able to definitely bust out and simply wrap issues in <mj-raw>. That’s what it was developed for.”

Nicole Hickman, Direct Advertising Developer, Fishawack Well being

To place it one other means, you’re not utterly tied to the MJML framework whenever you use it to develop responsive emails.

Darkish mode and MJML

When Nicole confirmed us how she creates emails with darkish and light-weight variations, she defined that plenty of it takes place up within the <mj-head> part.

In the event you’ve seen any tutorials on find out how to code darkish mode emails, you’ll acknowledge the meta tags which are used to let e mail shoppers know that your code presents each gentle and darkish mode variations:

  1. <mj-raw>

  2. <meta title="color-scheme" content material="gentle darkish">

  3. <meta title="supported-color-schemes" content material="gentle darkish">

  4. </mj-raw>

Beneath the usual CSS styling in Nicole’s boilerplate for this e mail format is the place she continues including and defining darkish mode types, utilizing a root selector and the media question (prefers-color-scheme: darkish).

  1. <mj-style>

  2. :root {

  3.    color-scheme: gentle darkish;

  4.    supported-color-schemes: gentle darkish;

  5. }

  6.  

  7. @media (prefers-color-scheme: darkish) {

  8. ...darkish mode types right here...

  9. }

  10. </mj-style>

Inside the <mj-style> tag above, Nicole consists of darkish mode CSS courses and tells e mail shoppers to cover gentle mode photos.

Nicole says it’s vital to know find out how to specify CSS selectors when coding with MJML. That’s what permits the e-mail to change to darkish mode preferences (background shade, textual content shade, and many others.) inside an <mj-section> primarily based on what you outlined within the types inside the top part.

That’s why, for instance, Nicole used a right-angled bracket in her darkish mode types when defining the background shade for tables in darkish mode.

  1. .dark-mode-bg>desk {

  2. background: #1E1E1F;

  3. background-image: linear-gradient (#fec800, #fec800) !vital;

  4. }

Later, in an <mj-section>, you’d embrace the CSS class for the darkish mode background: 

<mj-section background shade="fff" css-class="dark-mode-bg"> 

When this will get parsed to HTML, the category goes right into a <div>, however the colours really get utilized to the primary <td> in order that it seems within the cells of the desk. That’s why Nicole focused desk in her darkish mode types. In any other case, it wouldn’t override the backgrounds on her tables, which implies they’d nonetheless present a lightweight mode background.

Watching the way in which different builders work is wonderful! Nicole had me rethinking the way in which I goal darkish mode. However we’ll have to avoid wasting all that for an additional episode.

Picture swapping and MJML

One other query folks have about extra superior MJML includes picture swapping. Many occasions, you’ll need a picture that’s one dimension for desktop viewing and a distinct dimension that’s optimized for cellular units.

By the way in which, Nicole takes a “cellular first” strategy to e mail growth. For picture swapping, meaning she finally ends up doing one thing that will seem to be counterintuitive.

Within the first grouping of types, she consists of something that will should be utilized inline to the tag. Nicole does this as a result of GANGA (Gmail App with Non-Google Accounts) doesn’t help media queries, that are used for focusing on totally different display sizes.

So, by making use of the next code, she will be able to inform e mail shoppers to point out a sure picture on desktop however not cellular:

  1. <mj-type inline="inline">

  2. .present {

  3. show: none;

  4. }

  5.  

  6. .disguise {

  7. mso-hide: all !vital;

  8. }

  9. </mj-style>

Nicole additionally applies these courses to the media question as you’d count on. Nevertheless, by including !vital; to the top (see beneath) it overrides something within the desktop view.

  1. @media solely display and (min-width:480px) {

  2. .present {

  3. show: block !vital;

  4. }

  5.  

  6. .disguise {

  7. show: none !vital;

  8. mso-hide: all !vital;

  9. }

  10. }

Lastly, right here’s a have a look at the MJML code within the physique of Nicole’s e mail through which she consists of each a 600 x 400 placeholder picture for desktop and a 320 x 400 placeholder picture for cellular units whereas making use of the suitable courses:

  1. <mj-section>

  2. <mj-column>

  3. <mj-image src="https://through.placeholder.com/600x400" css-class="present” />

  4. <mj-raw>

  5. <!—[if !mso]><!---->

  6. </mj-raw>

  7. <mj-image src="https://through.placeholder.com/320x400" css-class="disguise" />

  8. <mj-raw>

  9. <!--<[endif]-->

  10. </mj-raw>

  11. </mj-column>

  12. </mj-section>

When Nicole switches over to the parsed HTML, we see that the inline class is show: none. However as a result of she used show: block together with !vital; that overrides the inline setting.

Additionally, discover that Nicole makes use of the <mj-raw> tag above so as to add conditional statements within the MJML that disguise cellular content material from Outlook’s desktop shoppers for Home windows.

Overlapping content material and MJML

One other method that skilled e mail builders use recurrently is overlapping components in a design. For instance, it’s your decision reside textual content overlayed on high of a graphic. That means, the e-mail is accessible for display reader utilization, and essential copy will present up even when the recipient has photos turned off.

To make this occur in MJML, the <mj-raw> tag as soon as once more involves the rescue.

Nicole used some superior types, which e mail super-geeks Mark Robbins, Steven Sayo, and Rémi Parmentier shared with the group. You’ll be able to study extra about these strategies for overlay and absolute positioning from Steven Sayo on Medium and from a put up on Good E-mail Code by Mark Robbins.

When you’ve discovered find out how to use these code snippets to realize the type of overlapping you need, it’s so simple as inserting it into both an <mj-style> or <mj-raw> tag.

Nicole informed me she selected to make use of <mj-raw> with an everyday <type> tag for organizational functions as a result of she wished to maintain it as its personal separate string.

Let the experimentation start

Now that you simply’ve been launched to the fundamentals of this e mail framework and a few superior MJML coding methods, it’s time to begin enjoying round.

Nicole talked about just a few occasions that she did need to experiment with issues a bit to get all of this to work. However for those who ask me, that’s a part of the enjoyable of being an e mail developer.

And right here’s some excellent news… Nicole says that the MJML Group on Slack is tremendous pleasant and useful. So, as you begin making an attempt out superior MJML methods and hit roadblocks, head over there to ask questions and make connections.

Talking of connecting… we’re simply getting began with Notes from the Dev: Video Version. There are extra nice suggestions, methods, and tutorials coming your means quickly. Be sure to subscribe on YouTube so that you aren’t disregarded.

Writer: Megan Boshuyzen

Megan is a graphic designer turned e mail developer who’s labored on all points of e mail advertising and marketing. She believes good emails for good causes make a optimistic distinction on the earth. Megan is presently working as an e mail developer for Sinch E-mail. Go to her web site and study extra at megbosh.com.

Writer: Megan Boshuyzen

Megan is a graphic designer turned e mail developer who’s labored on all points of e mail advertising and marketing. She believes good emails for good causes make a optimistic distinction on the earth. Megan is presently working as an e mail developer for Sinch E-mail. Go to her web site and study extra at megbosh.com.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments