Skip to content

Instantly share code, notes, and snippets.

@edoves
Last active December 9, 2024 14:24
Show Gist options
  • Save edoves/cccb95b32cc40e09db0471959f8eafb0 to your computer and use it in GitHub Desktop.
Save edoves/cccb95b32cc40e09db0471959f8eafb0 to your computer and use it in GitHub Desktop.
HTML EMAIL

Responsive

<!DOCTYPE html>
<html lang="en" xmlns="https://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="x-apple-disable-message-reformatting">
    <title></title>
    <!--[if mso]> 
<style> 
table {border-collapse:collapse;border-spacing:0;border:none;margin:0;} 
div, td {padding:0;} 
div {margin:0 !important;} 
</style> 
<noscript> 
<xml> 
<o:OfficeDocumentSettings> 
<o:PixelsPerInch>96</o:PixelsPerInch> 
</o:OfficeDocumentSettings> 
</xml> 
</noscript> 
<![endif]-->
    <style>
        table, td, div, h1, p {
            font-family: Arial, sans-serif;
        }
    </style>
</head>
<body style="margin:0;padding:0;word-spacing:normal;background-color:#939297;">
    <div role="article" aria-roledescription="email" lang="en" style="text-size-adjust:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%;background-color:#939297;">
        [Scaffold goes here]
    </div>
</body>
</html>

This HTML code is a skeleton for an email template designed to be compatible with Microsoft Outlook (which often requires special handling for rendering). Let me explain the commented-out parts:

Commented-Out Code:

<!--[if mso]> 
<style> 
table {border-collapse:collapse;border-spacing:0;border:none;margin:0;} 
div, td {padding:0;} 
div {margin:0 !important;} 
</style> 
<noscript> 
<xml> 
<o:OfficeDocumentSettings> 
<o:PixelsPerInch>96</o:PixelsPerInch> 
</o:OfficeDocumentSettings> 
</xml> 
</noscript> 
<![endif]-->

Explanation:

  1. <!--[if mso]> and <![endif]-->:

    • These are conditional comments for Microsoft Office-specific environments, such as Outlook.
    • They ensure that the code inside them is only read and applied by Microsoft Office products.
  2. <style> Block:

    • This block defines CSS rules specifically for Microsoft Outlook:
      • table {border-collapse:collapse;}: Ensures that table borders collapse properly, which can prevent rendering issues in Outlook.
      • div, td {padding:0;}: Removes any padding applied to div or td elements by default.
      • div {margin:0 !important;}: Overrides any default or inherited margins for div elements.
  3. <noscript> and <xml>:

    • These tags embed Office-specific settings in the email:
      • <o:OfficeDocumentSettings>: A namespace used for Office settings.
      • <o:PixelsPerInch>96</o:PixelsPerInch>: Sets the pixel density to 96 DPI, which is a standard for web rendering. It ensures consistent sizing of elements in Outlook, especially for images.

Why This is Used:

Microsoft Outlook uses the Microsoft Word rendering engine for emails, which is notoriously inconsistent with standard HTML/CSS. These special instructions ensure proper rendering in such environments without affecting other email clients.

Impact if Removed:

  • The email might display improperly in Microsoft Outlook (e.g., incorrect table alignment, padding, margins, or element sizes).
  • Other email clients would remain unaffected since these rules are ignored outside the <!--[if mso]> block.

This setup is a common practice when designing responsive, cross-client email templates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment