I could just write inline CSS, it’s the same thing!However, utility classes in Tailwind are much more powerful and allow you to:
- style responsive breakpoints
- style pseudos like
:hover - do both of the above with one class
- style for dark mode, print, reduced motion and more
- stay on-brand by using a design system in your team
tailwindcss-email-variants that allow you to target email clients just by using a class like gmail:hidden and you can quickly see why utility-first CSS with Tailwind CSS is such a powerful tool for HTML emails.
For most of the time, you won’t be writing CSS anymore 😎
Workflow
To use Tailwind CSS in your HTML emails, simply add the@tailwind directives to a <style> tag in your Layout’s <head>:
layouts/main.html
css/tailwind.css
@import that instead:
layouts/main.html
<link> tags? Maizzle supports that too:
layouts/main.html
<link> tags, you must make sure they include:
- the
rel="stylesheet"attribute - an
inlineorexpandattribute
Utility-first
Simply write your HTML markup and use Tailwind CSS utility classes to style elements. Instead of writing something like this:emails/example.html
Components
If you’re repeating the same utility classes over and over again, you can extract them to a Component so you’ve got a single source of truth and can make changes in one place.Custom classes
You may also extract utility classes to a custom CSS class, by using Tailwind’s@apply directive:
css/components.css
<style> tag or in a CSS file that you reference through a <link> tag.
The @layer components directive ensures that the .button-danger class is added to the components layer in Tailwind, which allows us to override it with a utility class if needed.
CSS files
You may organize your CSS into files if you prefer, and then@import them in a <style> tag or through a <link> in your Layout’s <head>.
For example, let’s import that css/components.css file we just created:
layouts/main.html
When importing CSS files you need to use the path relative to the root of your project’s directory.
Shorthand CSS
This section refers to writing shorthand CSS inside
<style> tags. For inline CSS shorthand, see the Shorthand CSS Transformer docs.padding, margin, and border CSS properties in shorthand-form, when possible.
Because utility classes map one-to-one with CSS properties, this normally doesn’t have any effect with Tailwind CSS. However, in the context of <style> tags, it’s useful when you extract utilities to components, with Tailwind’s @apply.
Consider this template:
emails/example.html
@apply to compose a col class by extracting two padding utilities:
layouts/main.html
For shorthand CSS to work with
padding or margin, you need to specify property values for all four sides. For borders, keep reading.Shorthand borders
To get shorthand-form CSS borders, you need to specify all these:- border-width
- border-style
- border-color
@apply, that means you can do something like this:
border-b border-solid border-black will not be shorthanded and Outlook can only apply individual borders when you write them in shorthand.
So you can do this:
Plugins
To use a Tailwind CSS plugin, simplynpm install it and follow its instructions to add it to plugins: [] in your tailwind.config.js:
tailwind.config.js
tailwind.config.js
tailwindcss-email-variants is already included in our Tailwind CSS preset, you don’t need to install it separately.Use in template
You may use Tailwind CSS, including directives like@apply, @layer, and even nested syntax, right inside a Template. You simply need to use the stack/push pattern to inject a <style> tag into the Layout being extended.
First, add a <stack name="head" /> inside your Layout’s <head> tag:
layouts/main.html
push to that stack from a Template:
emails/example.html
Prevent inlining
You can prevent CSS inside a<style> tag from being inlined:
emails/example.html
data-embedno-inlineembed
Email client targeting
Maizzle comes with tailwindcss-email-variants, a Tailwind CSS plugin that makes it easy to style your HTML emails for certain email clients. It adds custom variants that you may use to style elements only for certain email clients.Gmail
Use thegmail variant to style elements in Gmail’s webmail:
As you can see, this also requires that you add a
body class on your <body> tag.Apple Mail (10+)
Theapple-mail variant will target Apple Mail 10 and up:
iOS Mail (10+)
Use theios variant to target iOS Mail 10 and up:
iOS Mail (15)
Use theios-15 variant if you need to target iOS Mail 15 specifically:
Outlook.com dark mode
Use theogsc and ogsb variants to change color and background-color of elements in Outlook.com dark mode.
Open-Xchange
Use theox variant to target webmail clients that are powered by Open-Xchange.
Some of these email clients include Comcast, Libero, 1&1 MailXchange, Network Solutions Secure Mail, Namecheap Email Hosting, Mailbox.org, 123-reg Email, acens Correo Professional, Home.pl Cloud Email Xchange, Virgin Media Mail, and Ziggo Mail.
Outlook CSS
Outlook and Office 365 on Windows support proprietarymso- CSS properties.
Maizzle includes tailwindcss-mso, allowing you to use Outlook-only utilities:
If you’re looking show/hide content in/from Outlook, have a look at the available Outlook tags.