Use Tailwind CSS to design HTML emails.

  • By Soheil Rashidi
  • Last update: Jan 1, 2023
  • Comments: 7

Mailwind

Use Tailwind CSS to design HTML emails.

Tailwind makes it very easy to design websites, why not use it to design HTML emails? I've been using this for volt.fm and pikaso.me and it has made my life a lot easier.

Install

npm install -g mailwind

Usage

Design your HTML email using the Tailwind utility classes like you normally would for the web.

Then run the following command to generate the corresponding CSS file:

mailwind --input-html email.html --output-css style.css

Or run this command to generate an inlined HTML file:

mailwind --input-html email.html --output-html email-inlined.html

Options

--input-css

The path to your base CSS file. Use this if you need to write custom CSS. Defaults to style.css.

--input-html

The path to your HTML email file.

--output-css

The path to the CSS file that will be generated.

--output-html

The path to the inlined HTML file that will be generated.

--tailwind-config

The path to your custom Tailwind configuration file. Defaults to tailwind.config.js.

Note

In the provided default config file, all the units are changed to pixel which is probably what you want for HTML emails.

Example

Given an email.html file with this content:

<html>
  <body>
    <p class="font-bold text-lg">Welcome</p>
  </body>
</html>

running this command:

mailwind \
  --input-html email.html \
  --output-css style.css \
  --output-html email-inlined.html

will generate the following CSS and inlined HTML files:

.text-lg {
  font-size: 18px
}

.font-bold {
  font-weight: 700
}
<html>
  <body>
    <p class="font-bold text-lg" style="font-size: 18px; font-weight: 700;">Welcome</p>
  </body>
</html>

Version History

  • 2.0
    • New design
    • Upgrade to Tailwind CSS v3
  • 1.0
    • Initial release

Author

Soheil Rashidi

Copyright and License

Copyright 2022 Soheil Rashidi.

Licensed under the The MIT License (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.opensource.org/licenses/mit-license.php

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Github

https://github.com/soheilpro/mailwind

Comments(7)

  • 1

    fix: resolve tailwind cli correctly

    The expected tailwind cli might not be at the exact location when mailwind is installed locally or with pnpm. It should be resolved in node.js native way.

  • 2

    Instructions on how to add mailwind as a local dependency

    Hi there,

    I am using mailwind as part of my CI pipeline for a project and I think other devs would be interested in having it run as a local dependency as well.

    I've added some additional instructions on the README.md for such a purpose.

    Hopefully this is okay with you.

    • JP
  • 3

    Update dependencies version and fix missing tailwind directives

    With this PR, I have updated the package dependencies to their latest versions. Furthermore, I noticed that many styles were not included (maybe this is the cause for #11) that is why I have updated style.css as per the latest tailwind specifications.

  • 4

    How to make this work?

    Thanks a lot for the package but the usage instructions are not clear at all. I have Juice installed, but then how do I pass the css file into it to render the email?

    const TestMail = () => {
      return (
        <div>
          <head>
            <link rel="stylesheet" href="index.css" />
          </head>
          <h1 className="text-xl font-bold border-b">You have a new Test Email on Productlane</h1>
          <p className="border-b">Something something</p>
          <a href="https://productlane.io/feedback" className="bg-purple-600">
            Open
          </a>
        </div>
      )
    }
    
    export function testMailer({ to }: IParams) {
      const emailHtml = ReactDOMServer.renderToStaticMarkup(<TestMail />)
      const processedHtml = juice(emailHtml, {
        webResources: {
          relativeTo: "../app/core/styles/",
        },
      })
    
    // ...
    
  • 5

    Allow the usage of conditional breakpoint styling

    I want to use breakpoint styling in my mail templates, like this:

    <div class="flex w-full flex-col items-start gap-3 md:flex-row md:justify-between">
    

    ... but mailwind generates following snippet from this line:

    <div class="flex w-full flex-col items-start gap-3 md:flex-row md:justify-between" style="box-sizing: border-box; border-width: 0; border-style: solid; border-color: #e5e7eb; --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgb(59 130 246 / 0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; display: flex; width: 100%; flex-direction: column; align-items: flex-start; gap: 0.75rem;">
    

    So I wonder if tailwind can also build these styles correctly?

  • 6

    Currently requires all of the project's PostCSS plugins to be installed globally... why?

    I found out about this utility today on StackShare's daily email and instantly wanted to try it out! Got it all setup in my project and ran into some errors running it until I installed all of the PostCSS plugins my project was using globally.

    yarn global add postcss autoprefixer
    

    Note, I was running the mailwind command from a node.js script making a call to child_process.execSync(), so I could generate all the inlined email templates in one swoop, so I don't know if that makes a difference or not to whether the dependencies need to be installed globally.

  • 7

    Custom Font CSS from tailwind.config.js

    I want to create custom font css im importing from another file, but that wont work unfortunately. There are no .font-{custom fontname} classes in my css.