How do I write a media query for Gmail?
Stefan Bogdanescu
Founder & Senior Architect
Mastering Responsive Email Design: How to Handle Media Queries in Gmail
Developing responsive HTML emails—especially ones targeting clients like Gmail—is notoriously tricky. While web browsers handle CSS media queries seamlessly, email clients are notorious for their inconsistent rendering capabilities. This often leads developers into complex dead ends when trying to implement responsive layouts using standard CSS techniques.
The issue you are encountering with your nested table structure and media queries is a common pitfall in email development. The behavior differences between a dedicated preview tool (like SendGrid's builder) and the actual Gmail client stem from how these clients interpret, or refuse to support, modern CSS features like @media rules within the email context.
As a senior developer, I can tell you that relying solely on complex media queries targeting custom attributes often leads to unpredictable results in email marketing. We need a more robust, table-centric approach.
The Limitation of Media Queries in Email Clients
The fundamental problem lies with client support. Most major email clients, including Gmail, Outlook, and Apple Mail, have notoriously limited or non-existent support for the full suite of CSS media queries. When you attempt to use them, especially when trying to target custom attributes like table[table-view=mobile], the rendering engine often ignores these rules entirely, leading to elements defaulting to their standard display state regardless of the viewport size.
In your specific example, applying styles based on screen width inside a <style> block is often ineffective for controlling table visibility across all clients. The preview tool works because it uses a more modern rendering engine that supports these features better than the final client environment.
A Robust Strategy: Structural HTML Over CSS Magic
Instead of fighting the email client with complex CSS rules, the most reliable method for achieving responsiveness in email is to structure the HTML so that the layout itself changes based on width constraints, relying purely on table properties and careful nesting. This approach minimizes reliance on non-supported CSS features.
For responsive emails, we pivot from using media queries to employing conditional table structures. If you need content to stack vertically on mobile, you design two separate, stacked versions of your layout—one for desktop and one for mobile—and use CSS or HTML to conditionally show/hide the appropriate structure.
Implementing Conditional Display via Table Structure
For nested footers, a more reliable pattern is to ensure that the visibility control is managed by manipulating the display property directly on the container tables, ensuring they are set in inline styles where possible, as this offers the best compatibility with email clients.
Here is a conceptual shift from using complex media queries to simpler structural controls:
<style>
/* Base styles for hiding/showing based on width */
.mobile-only {
display: none; /* Default hide everything */
}
</style>
<!-- Desktop View (Visible on large screens) -->
<table class="module desktop-view" table-view="desktop" role="module" border="0" cellpadding="0" cellspacing="0" width="100%" style="display: table;" role="module" data-type="code" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="display: table;">
<!-- Desktop content here -->
</td>
</tr>
</table>
<!-- Mobile View (Visible on small screens) -->
<table class="module mobile-view" table-view="mobile" role="module" data-type="code" border="0" cellpadding="0" cellspacing="0" width="100%" style="display: table;" role="module" data-type="code" border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="display: table;">
<!-- Mobile content here -->
</td>
</tr>
</table>
By setting the initial display state and relying on conditional logic (often handled by external tools or more advanced, client-specific CSS hacks if absolutely necessary), you move the complexity out of the fragile media query environment. This principle of structuring complex data logically is crucial in building robust applications, much like how we approach architecture when developing systems using frameworks like Laravel, where structure and clear separation are paramount.
Conclusion
When designing for email clients like Gmail, shift your focus from CSS-based responsiveness to structural HTML responsiveness. Avoid relying heavily on @media queries within the <style> block for critical layout changes. Instead, prioritize table nesting, proper use of display: table, and careful testing across various clients. This pragmatic approach ensures that your carefully crafted design renders consistently, providing a much better experience for all your subscribers.