Creating responsive HTML emails that look great on all devices can be challenging. MJML (Mailjet Markup Language) simplifies this process significantly. This guide will walk you through generating MJML code for a specific layout: a single column that displays at a fixed width of 700px on desktop screens but responsively adjusts to 63% of the screen width on mobile devices.
Choosing the right email layout structure is key for readability and engagement.
<mj-section>
and <mj-column>
, forming the foundation of your layout.<mj-style>
tags, particularly media queries to target different screen sizes.MJML is engineered to streamline responsive email development. It uses a semantic syntax that compiles down to complex, presentation-table-based HTML optimized for email client compatibility. Its core philosophy is "mobile-first," meaning the default rendering is tailored for smaller screens.
Sections act as rows in your email layout. They are block-level elements that contain columns. You can control background colors, padding, and importantly, the max-width
of a section to constrain your content area, especially on wider screens.
Columns reside within sections and hold your actual content (text, images, buttons, etc.). By default, if a section contains multiple columns, they appear side-by-side on desktop and stack vertically on mobile. For a single-column layout, it simply occupies the space defined by its parent section and its own width settings.
To achieve the desired 700px desktop width and 63% mobile width for a single column, we'll leverage MJML's structure combined with custom CSS styles defined within the <mj-head>
.
MJML helps ensure layouts adapt smoothly from desktop to mobile.
While MJML components have attributes for basic styling, precise responsive control often requires custom CSS. We will:
max-width
on the containing section to prevent unwanted stretching on very large displays.Following the mobile-first principle, we define the styles for smaller screens directly. We'll use a CSS class (e.g., responsive-column
) and set its width
to 63%. This ensures that on mobile devices, the column takes up slightly less than two-thirds of the screen width.
A CSS media query is used to detect screen sizes typical of desktop displays. A common breakpoint is min-width: 480px
or min-width: 600px
. Inside this query, we override the column's width to the desired fixed value of 700px
. The !important
flag is often necessary in email CSS to ensure these styles take precedence over default MJML or email client styles.
When the column's width is less than 100% (like our 63% on mobile), it won't automatically center within the section. We need to explicitly add margin: 0 auto;
to the column's styles for the mobile view to center it horizontally.
Here is the complete MJML code implementing the single-column layout with the specified responsive widths:
<mjml>
<mj-head>
<!-- Define Title and Preview -->
<mj-title>Responsive Single Column Layout</mj-title>
<mj-preview>This email demonstrates a responsive column.</mj-preview>
<!-- Custom Styles for Responsiveness -->
<mj-style inline="inline">
/* Base mobile style: 63% width and centered */
.responsive-column {
width: 63% !important;
max-width: 63%; /* Robustness */
margin: 0 auto !important; /* Centering */
}
/* Desktop override: 700px width */
@media only screen and (min-width: 480px) {
.responsive-column {
width: 700px !important;
max-width: 700px; /* Robustness */
}
}
</mj-style>
<!-- Optional: Define default attributes for components -->
<mj-attributes>
<mj-text padding="10px 25px" font-size="16px" line-height="1.5" color="#333333" />
<mj-section padding="20px 0" />
</mj-attributes>
</mj-head>
<mj-body background-color="#f0f0f0">
<!-- Section containing the column -->
<!-- max-width ensures the section background/padding doesn't exceed 700px -->
<mj-section max-width="700px" background-color="#ffffff">
<!-- The single column with the CSS class applied -->
<mj-column css-class="responsive-column">
<!-- Your email content goes here -->
<mj-text>
<strong>Welcome to our Responsive Email!</strong>
</mj-text>
<mj-text>
This column demonstrates specific width requirements. On desktop screens (typically wider than 480px), this content area will be exactly 700px wide.
</mj-text>
<mj-image src="https://via.placeholder.com/600x200?text=Placeholder+Image" alt="Placeholder Image" padding="10px 0" />
<mj-text>
On mobile devices, the column adapts to be 63% of the screen width, ensuring readability and a good user experience. It's also centered horizontally.
</mj-text>
<mj-button href="#" background-color="#388278" color="#ffffff">
Learn More
</mj-button>
</mj-column>
</mj-section>
</mj-body>
</mjml>
<mj-head>
: Contains metadata and styles.
<mj-title>
& <mj-preview>
: Set the email title (browser tab) and preview text (inbox snippet).<mj-style inline="inline">
: Defines the custom CSS. The inline="inline"
attribute helps MJML inline styles where possible for better compatibility, though media queries remain in a style block.
.responsive-column {...}
: Sets the base mobile style (63% width, centered).@media only screen and (min-width: 480px) {...}
: Contains the desktop override, setting the width to 700px for screens wider than 480px.<mj-attributes>
: (Optional) Sets default styles for components like mj-text
and mj-section
.<mj-body>
: The main container for the visible email content.<mj-section max-width="700px">
: The row holding the column. max-width="700px"
limits the section's width, aligning with the desktop column width.<mj-column css-class="responsive-column">
: The content column. The css-class
attribute links it to our custom styles defined in <mj-style>
.<mj-text>
, <mj-image>
, <mj-button>
): Placeholder content within the column. Remember, columns must contain content.Achieving the desired layout involves balancing several factors. The radar chart below compares using direct MJML attributes versus the recommended approach (CSS class with media queries) across key aspects of responsive email design.
As illustrated, using CSS classes and media queries provides more precise control over both desktop and mobile widths, facilitates easier centering on mobile, and offers better maintainability and flexibility, although direct attributes might seem simpler initially.
This mindmap outlines the core concepts involved in creating this responsive MJML layout.
The mindmap highlights how MJML components, CSS styles within the head (especially media queries), and core principles like mobile-first design interact to produce the final responsive HTML email.
While MJML significantly improves compatibility, email clients render HTML and CSS differently.
Desktop versions of Outlook (especially older ones) are notorious for poor CSS support, particularly with media queries. While the fixed 700px width might work via MJML's table structure, the mobile-specific styles (like 63% width) might be ignored. Always test!
Most modern mobile clients (iOS Mail, Gmail App) and webmail clients (Gmail, Yahoo Mail) have good support for media queries, so the responsive behavior should work as intended.
Never assume your email will render perfectly everywhere. Use email testing platforms like Litmus or Email on Acid to preview your MJML-generated HTML across dozens of different email clients and devices before sending.
Remember that MJML columns are containers. They must contain other MJML components like <mj-text>
, <mj-image>
, or <mj-button>
to render correctly. An empty column might collapse or cause layout issues.
There are different ways to approach width control in MJML. Here's a comparison focusing on achieving specific responsive widths:
Method | Mobile Width Setting | Desktop Width Setting | Centering on Mobile | Pros | Cons |
---|---|---|---|---|---|
CSS Class + Media Query (Recommended) | Via CSS class in <mj-style> (e.g., width: 63% !important; ) |
Via media query in <mj-style> (e.g., width: 700px !important; ) |
Easy via CSS (margin: 0 auto !important; ) |
Precise control, clear separation of concerns, good maintainability. | Requires understanding CSS and media queries. |
MJML Attributes Only | Set on <mj-column width="63%"> |
Cannot set a different *fixed* px width directly for desktop via attributes alone. MJML's default responsiveness takes over. | Requires wrapper sections/columns or complex structures; not straightforward. | Simple for basic percentage-based layouts. | Limited control over specific fixed widths on desktop vs. percentage on mobile; difficult to achieve the exact requirement. |
Manual HTML/CSS Edits (Post-Compilation) | Manually add CSS rules to compiled HTML. | Manually add CSS rules to compiled HTML. | Manually add CSS rules to compiled HTML. | Ultimate flexibility. | Defeats the purpose of using MJML, error-prone, hard to maintain. |
The recommended approach using CSS classes within <mj-style>
offers the best balance of control, compatibility, and maintainability for achieving specific responsive width requirements like the one requested.
For a broader understanding of building responsive emails with MJML, including foundational concepts and practical examples, consider watching a comprehensive tutorial. This video covers the fundamentals of MJML, using its tools, and email design principles.
Watching tutorials like this can solidify your understanding of MJML's syntax, components, and how to effectively structure emails for optimal responsiveness across various platforms.
!important
in the CSS?Email clients often have their own default styles, and MJML itself generates inline styles and embedded styles. Using !important
increases the specificity of your custom styles, making it more likely they will override any conflicting styles from the email client or MJML's compilation process, ensuring your intended widths are applied.
Yes, you can adjust the `min-width` value in the media query (e.g., `@media only screen and (min-width: 600px)`). The optimal breakpoint depends on your design and target audience. 480px is a common starting point to differentiate smaller mobile screens from larger ones and tablets/desktops, but testing is key to determine what works best for your specific layout.
While 700px is achievable, standard email widths are often closer to 600px for maximum compatibility and readability, especially considering preview panes in desktop clients. If 700px causes issues or feels too wide after testing, consider reducing it (e.g., to 640px or 600px). The `max-width="700px"` on the `mj-section` helps contain the visual structure.
Including `max-width` alongside `width` (e.g., `width: 63% !important; max-width: 63%;`) adds robustness. While `width` should typically suffice, `max-width` can prevent potential issues in certain clients where the element might try to expand beyond the intended percentage or pixel width. It's a good defensive coding practice in email development.