Ask me anything
Ithy Logo
Sign in to save

Unlocking Full Viewport Height in MJML: Challenges and Workarounds

Discover why true full-height emails are tricky in MJML and explore the most effective techniques available.

mjml-full-viewport-height-yuhbsxva

Achieving a layout where the main body of your email stretches to fill the entire height of the user's viewport (screen) is a common desire for web design, lending a modern, app-like feel. However, when working with MJML, a framework designed specifically for crafting responsive emails, replicating this exact behavior presents significant challenges. This guide explores why full viewport height is difficult in the email world and details the practical workarounds you can use within MJML.

Essential Insights

  • Direct full viewport height (100vh) for mj-body is unreliable: Due to inconsistent CSS support across various email clients (like Outlook, Gmail, Apple Mail), using viewport units for height often fails.
  • MJML lacks a native solution: The framework prioritizes responsive width and reliable rendering across clients, and doesn't offer a specific attribute to force mj-body to the viewport height.
  • Workarounds focus on visual effects: Practical solutions involve techniques like background colors/images on full-width sections or using fixed heights, rather than dynamically scaling the body to the viewport.

The Core Challenge: Email Client Limitations

Why Standard Web Techniques Fall Short

Unlike web browsers, email clients are notoriously inconsistent in how they render HTML and CSS. This is the primary reason why achieving full viewport height is problematic:

Inconsistent CSS Support

Many email clients, particularly older versions of Outlook and even some webmail clients, have limited or buggy support for modern CSS properties essential for viewport-based sizing:

  • Viewport Units (vh): Support for vh (viewport height) units is patchy. Many clients ignore it completely or interpret it incorrectly.
  • Height on <body> and <html>:** Setting height: 100% or min-height: 100vh on the root <html> or <body> elements (which MJML eventually compiles to) is often stripped out or ignored by email clients.

MJML's Table-Based Structure

MJML compiles down to complex, nested HTML tables to ensure maximum compatibility across different email clients. While excellent for responsive width and layout consistency, this table structure makes applying vertical height constraints challenging, as table cell heights behave differently from standard block elements in CSS.

Screenshot showing MJML code side-by-side with the rendered email preview

MJML simplifies responsive email creation but operates within email client constraints.


Attempted CSS Approaches (and Why They Often Fail)

Exploring Direct Styling Methods

While you can inject custom CSS into MJML using <mj-style> or <mj-attributes>, direct approaches to set full height typically encounter the limitations mentioned above.

Using min-height: 100vh

The most common web technique is setting min-height: 100vh;. In MJML, you might try applying this via <mj-style>:

<mjml>
  <mj-head>
    <mj-style>
      /* Attempting to style the MJML body wrapper */
      .mjml-body {
        min-height: 100vh !important; 
      }
      /* Targeting potential underlying body/html tags */
      html, body {
        min-height: 100vh !important;
        height: 100% !important; /* Often needed for % heights to work */
        margin: 0;
        padding: 0;
      }
    </mj-style>
    <!-- Some sources mention targeting [data-gjs-type="mj-body"] for specific editors, 
         but this is editor-specific and not reliable for final email rendering -->
  </mj-head>
  <mj-body background-color="#f0f0f0">
    <mj-section>
      <mj-column>
        <mj-text>Content...</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>

Limitations:

  • Client Support: As established, vh units are unreliable in many major email clients (especially Outlook).
  • Style Stripping: Email clients might strip min-height or height styles applied to <body> or wrapper divs.
  • Selector Specificity: Targeting the correct element generated by MJML can be tricky, and generic body styles might not apply as expected due to MJML's structure.

Using height: 100%

Setting height: 100% requires the parent element to also have a defined height. In the context of an email, the <html> and <body> elements often don't have a height defined in a way that email clients respect, rendering height: 100% ineffective for child elements trying to fill the viewport.


Practical Workarounds in MJML

Achieving a Full-Height *Visual* Effect

Since true dynamic viewport height isn't reliably feasible, focus on techniques that create a similar *visual* appearance.

Full-Width Sections with Backgrounds

You can make an <mj-section> span the full width of the email container and apply a background color or image that extends edge-to-edge. If you apply the same background to multiple sections or the <mj-body>, you can create the illusion of a continuous, full-height background.

  • Use full-width="full-width" on <mj-section> to make the background color/image span the entire width (beyond the standard 600px content area).
  • Ensure the <mj-body> also has a matching background color if needed.
  • This doesn't change the actual height, but makes the background visually fill the space.
<mjml>
  <mj-body background-color="#cccccc">
    <mj-section full-width="full-width" background-color="#cccccc" padding="0">
      <mj-column>
        <mj-text>Top content</mj-text>
      </mj-column>
    </mj-section>
    <!-- More sections with the same background -->
    <mj-section full-width="full-width" background-color="#cccccc" padding="0">
      <mj-column>
        <mj-text>Bottom content</mj-text>
      </mj-column>
    </mj-section>
  </mj-body>
</mjml>
Example of a responsive email template design

Visual continuity can be achieved with consistent background colors across sections.

Using mj-hero Component

The <mj-hero> component is designed for prominent header images. While it has modes like fixed-height and fluid-height, these relate more to how the content inside behaves relative to the background image height, rather than forcing the entire component to the viewport height. Using a tall background image within mj-hero can contribute to a visually tall element, but it won't dynamically adapt to the viewport height.

<mjml>
  <mj-body>
    <mj-hero
      mode="fluid-height" 
      background-url="your-tall-background.jpg"
      background-width="600px" /* Match body width or use 100% carefully */
      background-height="800px" /* Example fixed height */
      padding="100px 0px">
      <mj-text color="#ffffff" font-size="20px">
        Hero Content
      </mj-text>
    </mj-hero>
  </mj-body>
</mjml>

Setting Fixed or Minimum Heights

You can apply a fixed height or min-height in pixels (e.g., height="800px") to specific <mj-section> elements. This ensures a section occupies a certain vertical space, which might be sufficient if you're targeting a common screen size. However, this is not responsive to the actual viewport height and might result in excessive scrolling on small screens or empty space on large ones.

Employing mj-spacer

Strategically placing <mj-spacer height="Xpx" /> components can add vertical space, pushing content down and increasing the overall perceived height of the email body. This is a manual way to ensure minimum height but doesn't adapt dynamically.


Comparing Full Height Techniques: Compatibility & Effectiveness

A Visual Analysis

The following radar chart provides an estimated comparison of different techniques based on common email client behaviors. Note that 'Compatibility' refers to how likely the technique is to render without breaking across major clients (Gmail, Outlook, Apple Mail, Webmail), and 'Visual Effect' refers to how well it approximates the desired full-height appearance. 'Responsiveness' considers adaptation to different screen sizes (true viewport adaptation is low for all).


Understanding the Constraints

A Mindmap Overview

This mindmap summarizes the key factors involved when attempting to achieve full viewport height in MJML emails.

mindmap root["Goal: MJML Body
Full Viewport Height"] id1["Challenges"] id1a["Email Client CSS Support"] id1a1["Inconsistent 'vh' Units"] id1a2["Height/min-height Ignored on Body/HTML"] id1a3["Outlook Rendering Quirks"] id1b["MJML Structure"] id1b1["Table-Based Layout"] id1b2["Focus on Responsive Width"] id2["CSS Attempts (Unreliable)"] id2a["min-height: 100vh"] id2a1["Often Ignored/Stripped"] id2b["height: 100%"] id2b1["Requires Parent Height"] id3["Practical Workarounds (Visual)"] id3a["Full-Width Section Backgrounds"] id3b["Fixed/Min Heights (px) on Sections"] id3c["mj-hero with Tall Background"] id3d["mj-spacer for Vertical Space"] id4["Key Consideration"] id4a["Cross-Client Testing is Crucial!"]

CSS Support in Email Clients

A Quick Reference Table

This table provides a general overview of CSS support relevant to height manipulation in major email rendering engines. Support can vary significantly even between versions of the same client (e.g., Outlook on different platforms).

CSS Property / Unit Gmail (Web/Mobile) Apple Mail (iOS/macOS) Outlook (Windows Desktop) Outlook.com / Office 365 Yahoo Mail
vh units Partial/Inconsistent Good Support No Support Partial/Inconsistent Partial/Inconsistent
min-height Good Support Good Support Partial (often buggy on tables/cells) Good Support Good Support
height: 100% (on body/containers) Often Ignored Often Ignored Often Ignored Often Ignored Often Ignored
background-image (on sections/body) Good Support Good Support Partial (issues with VML rendering) Good Support Good Support

Note: This is a simplified summary. Always test thoroughly using tools like Email on Acid or Litmus.


Learning MJML Fundamentals

Building Responsive Emails

Understanding the core concepts of MJML and responsive email design is crucial for working effectively within its constraints. While this video doesn't specifically solve the full-height problem, it provides a solid foundation in creating emails with MJML, which helps in understanding *why* certain CSS properties behave differently in the email context compared to the web.

This tutorial covers the basics of building responsive emails using the MJML framework.


Frequently Asked Questions (FAQ)

Can I force the mj-body to take the full viewport height (100vh) in MJML?

Unfortunately, there is no reliable method to force the mj-body or the resulting HTML email body to consistently take up exactly 100% of the viewport height across all major email clients. CSS properties like min-height: 100vh have inconsistent support and are often ignored or misinterpreted by email clients, especially Outlook.

What's the best alternative for achieving a "full-screen" visual effect in MJML?

The most practical approaches focus on creating a visual illusion of full height:

  • Consistent Backgrounds: Apply the same `background-color` or `background-url` to the `mj-body` and use `full-width="full-width"` on `mj-section` elements with the same background. This creates a seamless vertical appearance.
  • Tall Hero Component: Use `mj-hero` with a tall background image and potentially a fixed height (`background-height`).
  • Fixed Section Heights: Manually set large `height` values (in pixels) for your main sections, accepting that this won't be dynamically responsive to the viewport.
Does `full-width="full-width"` on `mj-section` affect the section's height?

No, the `full-width="full-width"` attribute on an `mj-section` only affects the width of the section's *background*. It makes the background color or image extend to the edges of the email body, rather than being constrained to the standard content width (typically 600px). It does not influence the section's height, which is determined by its content or any explicitly set `height` attribute.

Why doesn't `height: 100%` work reliably in MJML emails?

For `height: 100%` to work in CSS, the parent element must have a defined height. In the context of an HTML email, the containing `` and `` tags often do not have a height that email clients consistently recognize or respect. Without a defined parent height to reference, `height: 100%` on child elements effectively means 100% of an undefined value, causing it to collapse or behave unpredictably.


Recommended Reading


References

documentation.mjml.io
MJML Documentation - MJML
dev.mailjet.com
MJML

Last updated May 4, 2025

AI-assisted research. Sources, methodology and corrections · Source-checked guides and tools · Explore published answers

Ask Ithy AI