For creating email templates, we use MJML, a responsive email framework that simplifies writing HTML emails. MJML allows developers to write clean, reusable components that get compiled into fully responsive HTML emails.
- Responsive by Default – MJML abstracts the complexity of email responsiveness.
- Simple Syntax – Reduces the need for manually writing complex, nested HTML tables.
- Cross-Email Client Compatibility – Works across Gmail, Outlook, Yahoo, and Apple Mail.
- Reusable Components – Custom components speed up development and ensure consistency.
- Fast Development – The built-in compiler instantly converts MJML into production-ready HTML.
MJML can be used through an online editor, CLI, or Node.js.
You can try MJML in the browser without installation HERE
Install MJML globally using pnpm:
pnpm i -g mjml
Now, you can convert MJML to HTML using:
mjml input.mjml -o output.html
Install MJML as a local dependency:
pnpm i mjml
Use it in a JavaScript:
const mjml = require('mjml')
const htmlOutput = mjml(`
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>Hello World!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`)
// Output compiled HTML
console.log(htmlOutput.html)
Each MJML file follows a simple structure, and provides built-in components that make email development faster. Here' basic example:
<mjml>
<mj-head>
<mj-title>My Email</mj-title>
</mj-head>
<mj-body>
<mj-section>
<mj-column>
<mj-text>Hello World!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
Main parts
Tag | Description |
---|---|
<mjml> |
Root tag for the MJML template. |
<mj-head> |
Contains metadata like <mj-title> and <mj-preview> . |
<mj-body> |
The main body of the email. |
<mj-section> |
Creates a full-width row inside <mj-body> . |
<mj-column> |
A column inside a section (used for structuring content). |
<mj-group> |
Groups multiple columns together (useful for nested layouts). |
<mj-divider> |
Creates a horizontal divider. |
Typography Components
Tag | Description |
---|---|
<mj-text> |
Adds text content. |
<mj-title> |
Sets the email title (appears in the browser tab). |
<mj-preview> |
Provides a short preview of the email (seen in inboxes). |
Media components
Tag | Description |
---|---|
<mj-image> |
Adds an image with responsive scaling. |
<mj-carousel> |
Creates an image carousel. |
Button component
Used to create call-to-action buttons.
<mj-button href="https://infinum.com">
Click Me!
</mj-button>
Table component
<mj-table>
<tr>
<td>Tag</td>
<td>Description</td>
</tr>
<tr>
<td>`<mj-table>`</td>
<td>Useful for tabular data.</td>
</tr>
</mj-table>
MJML allows inline styles and attributes for customization.
<mj-text font-size="18px" color="#555" padding="10px">
Styled Text
</mj-text>
To apply global styles, use <mj-style>
in the <mj-head>
:
<mj-head>
<mj-style inline="inline">
.custom-class { color: red; font-size: 18px; }
</mj-style>
</mj-head>
<mj-text class="custom-class">This is red text.</mj-text>
-
Use sections & columns for Layouts
- Always wrap content inside and to ensure responsiveness.
-
Limit the width of Emails
- Emails should be 600px wide to look good on most email clients.
-
Use MJML-Specific attributes
- Example:
padding
,border-radius
, andtext-align
instead of writing CSS manually.
- Example:
-
Ensure Accessibility
- Use alt attributes for
<mj-image>
.
- Use alt attributes for
-
Use Inline Styles Sparingly
- MJML helps with styling, so avoid excessive inline CSS.
-
Use the MJML Online Editor for quick testing
- MJML Playground is useful for previewing templates.
Issue | Solution |
---|---|
Email looks broken in Outlook | Use mj-wrapper to ensure proper rendering. |
Gmail clips email | Keep HTML size under 102KB. |
Custom fonts not loading | Use Web-safe fonts (Arial, Helvetica, sans-serif). |
Images not showing | Use absolute URLs (e.g., https://example.com/image.png ). |