Html email and fluid table with changing borders

109 Views Asked by At

I have some design requirements for an email template where I have two "challenges":

  1. two columns need to flip to one column
  2. some visible border lines need to be switched from vertical to horizontal

The following shows how it should look (2 columns on the left for desktop, 1 column on the right for mobile):

layout

The whole email is based on responsive tables and the two-column part is implemented as follows right now:

<table cellpadding="5" cellspacing="0"
    style="background-color:#F6F6F6; font-size: 14px; color:#58595b; width:100%; border-collapse:collapse;">
    <tr><td align="center" valign="top" height="10" colspan=2 style="line-height: 10px; font-size: 10px;">&nbsp;</td></tr>
  <tr>
    <td valign="top" style="border-right: 1.5px solid; border-color: #d0d0d0; padding-right:40px; text-align:right; width:42%; vertical-align:top;">
      Start point
    </td>
    <td valign="top" style="padding-left:40px; vertical-align:top;">
      <strong>Fri, January 12, 2023 12:00</strong>
      <br />
      Harbour, Seatown
    </td>
  </tr>
  <tr><td align="center" valign="top" height="10" colspan=2 style="line-height: 10px; font-size: 10px;">&nbsp;</td></tr>
  <tr>
    <td valign="top" style="border-right: 1.5px solid; border-color: #d0d0d0; padding-right:40px; text-align:right; width:42%; vertical-align:top;">
      End point
    </td>
    <td valign="top" style="padding-left:40px; vertical-align:top;">
      <strong>So, January 18, 2023 10:00</strong>
      <br />
      Central Station, Capital
    </td>
  </tr>
  <tr><td align="center" valign="top" height="10" colspan=2 style="line-height: 10px; font-size: 10px;">&nbsp;</td></tr>
</table>

I tried the approach with having a left and a right table (explained here) but the problem is that I do not use fixed widths.

How could I achieve the required design with a responsive mail template?

1

There are 1 best solutions below

3
Nathan On

You will need to use the technique as outlined in the link, but if you want to use percentages instead of fixed widths, then use width="50%".

This is because the technique works on the basic fundamentals of HTML, that if a block doesn't fit in the space available, it will automatically shift underneath.

So to enable the stacking without a fixed pixel width, you will need to add a @media query to force the stacking (otherwise it would not stack).

e.g.

@media (max-width: 620px)
.table-left,.table-right {
    width: 100% !important;
}

(The article you link to is a bit outdated: don't use [class=...], just write it out normally. Gmail may strip the entire <style> section if it doesn't like something in it, and this is one of those things it doesn't like.)

I prefer an override (max-width, and !important) because you want everything possible inline, and only to use embedded styles where strictly necessary.

But that's also why it's best to use a fixed pixel width, because some email clients do not respect your embedded styles (styles in the <head>). GANGA emails (one form of Gmail account) fall into this category. Those email clients would not stack even though they may need to, if you fully rely on the @media query for the stacking.

To override the border, put a style on the <td>, and reference it in the @media query, e.g.

@media (max-width: 620px)
.border {
    border-right:none!important;
}
.border-top {
    border-bottom:1.5px solid #d0d0d0 !important;
}

As one doesn't have the same border structure (they don't both need border-bottom), one of the <td>s will need a different class. Here, I'm expecting the first one, i.e. <td class="border border-top">.