Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to hide tables in HTML e-mail in inline css - only to show them later via a media query

I'm working on my first mobile template for an E-mail, using media queries. So far so good, media queries is cool and I'll be surely using this in my web design projects from now on.

However, I'm running into one difficulty; I have a pretty complicated header design (background image with Facebook & youtube logos on it that link to the corresponding pages), so it's sliced in a particular way. This makes it not too easy to make into a mobile version, and I thought maybe I could fix it in a different way: use two different header tables, one shwowing on large devices, and one showing on small devices.

The mobile part is no problem, since they interpret embedded CSS quite well.

But I can't seem to hide a complete table for other mail clients. I tried display:none, position:absolute with top and left -9999px, etc...

Does anyone have a solution for this? It would save me a lot of time.

like image 508
blobkat Avatar asked Aug 21 '12 08:08

blobkat


People also ask

How do you hide a table in HTML?

The hidden attribute hides the <table> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <table> element is not visible, but it maintains its position on the page.

Do CSS media queries work in email?

Media Queries and Inline Styles So, the email's normal CSS needs to be inlined and the media query CSS needs to override those styles once its triggered. You can leave the media query styles in the <head> of your email, as clients that support media queries don't strip out the <head> or <style> areas.

Can we hide a table row in HTML?

A hidden attribute on a <tr> tag hides the table row. Although the table row is not visible, its position on the page is maintained.

Can you use media queries in HTML emails?

Since media queries naturally live at the top of an HTML document, any inline styles applied to the content of the email will take precedence. Therefore, you need a way to override those inline styles.


3 Answers

OK, I seem to have found the solution myself:

Inline, on the elements inside the table to be hidden in the normal, non-mobile version, I put the following properties:

line-height:0px;
font-size:0px;
height:0px;
margin:0;
padding:0;

I'm testing it right now with a table with one tr, one td and one p inside the td.

I put these properties on almost every element:

<table cellpadding="0" cellspacing="0" border="0" align="center" id="hidden">
  <tr class="showmobile" style="line-height:0px;font-size:0px;height:0px;margin:0;padding:0;">
    <td class="showmobile" style="line-height:0px;font-size:0px;height:0px;margin:0;padding:0;">
       <p class="showmobile" style="color:White; line-height:0px;font-size:0px;height:0px;margin:0;padding:0;">Testing this shiznit</p>
    </td>
  </tr>
</table>

I also give these elements the class "showmobile", which I manipulate like this in the internal stylesheet in the head:

tr[class="showmobile"]
        {
         line-height:125% !important;
         font-size:16px !important;
         height:30px !important;   
        }

td[class="showmobile"]
        {
         line-height:125% !important;
         font-size:16px !important;
         height:30px !important;   
        }

p[class="showmobile"]
        {
         line-height:125% !important;
         font-size:16px !important;
         height:30px !important;   
        }

This way of selecting the classes, I got from the awesome html email boilerplate @ http://htmlemailboilerplate.com/ -> linking to this article: http://www.campaignmonitor.com/blog/post/3457/media-query-issues-in-yahoo-mail-mobile-email/ -> basically, it makes Yahoo mail play nice and not apply the CSS in the media query when it doesn't need to.

CONCLUSION: I've tested it on four platforms: GMail, Outlook 2010, Lotus Notes (I know!), and an iPhone 3G. They all give the expected result, except for Outlook, which still shows a 1 pixel line. With some smart content (coloring the text to go up in the background), this is very well hideable.

I will keep you guys up to date if I bump into any problems with certain mail clients.

like image 148
blobkat Avatar answered Oct 14 '22 14:10

blobkat


Place the table in a div html tag and add its style as display:none !important in the media queries.

like image 1
Teena Thomas Avatar answered Oct 14 '22 15:10

Teena Thomas


After quite a battle I was able to come up with this.

  • You can't use height. Gmail replaces it with min-height.
  • Don't exactly match the bg color and text
  • images need to be made 1px height and width and then enlarged w/ the media queries
  • you still might get some elements displaying on the screen in gmail - try and make them as unnoticable as possible

The CSS:

@media all and (max-width: 480px) {
  .mobile-hide {
     width:1px !important; 
     display:none !important; 
     color:#fff;
  }
  .mobile-show {
     display:block !important;
     line-height:125% !important;
     font-size:14px !important;
     height:auto !important; 
     color:#666 !important;
  }
  .mobile-image {
     width:350px !important;
     height:446px !important;
  }
  .w800 {width:400px !important;}
}

The HTML:

<table class="w800" width="800">
...
<tr><td>
<a href="http://mylink"><img src="image.jpg" width="300" height="300" border="0" alt="My Sweet Image" /></a>
</td>

<!-- a big fat image on the right for big screens only -->
<td class="mobile-hide">
<img class="mobile-hide" src="image2.jpg" width="450" height="500" border="0" alt="" />
<span class="mobile-hide">
My Text with a <a href="http://mylink" class="mobile-hide">link</a></span>
</td></tr>
...
</table>

<!-- a new table at the bottom for small screens only, "one column" -->
<table width="400" style="width:400px; margin:0 auto;">
<tr><td class="mobile-show" style="line-height:0px;font-size:0px;height:0px;margin:0;padding:0;">
<img class="mobile-image" src="image2.jpg" width="1" height="1" border="0" alt="" />
<span class="mobile-show" style="font-size:1px; color:#ffe;">
My Text with a <a href="http://mylink" style="text-decoration:none; color:#ffe;" class="mobile-show">link</a></span>
</td></tr>
</table>
like image 1
squarecandy Avatar answered Oct 14 '22 16:10

squarecandy