Responsive e-mail: turn table cells (td) to (cleared) blocks

为君一笑 提交于 2019-12-25 03:41:31

问题


I have this basic example that should display these three table cells as blocks (underneath each other) on mobile devices.

Yet this doesn't seem to work out on iPad, iPhone, nor on a Samsung phone. It does work on regular browsers and websites that simulate a mobile display (for web pages probably), but when viewing on actual mail clients of mobile devices, the display: block property seems to be ignored.

Is there anything that's missing? Or if the display-attribute isn't supported at all, what is the alternative?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta charset="utf-8">
    <title>Mobilize me</title>
    <style type="text/css">
        @media only screen and (max-width: 1400px) {
            table,tr,td{
                width: 100% !important;
                display: block !important;
                clear: both !important;
            }
        }
    </style>
</head>
<body>
    <table cellspacing="0" cellpadding="0" width="900">
        <tr>
            <td width="33%" bgcolor="red">
               one
            </td>
            <td width="33%" bgcolor="green">
                two
            </td>
            <td width="33%" bgcolor="blue">
                three
            </td>
        </tr>
    </table>
</body>
</html>

Remarkably the example from this thread isn't working either.


回答1:


Use three nested tables with "align=" to replicate "float" and have a media query to resize table to 100% with display:block on mobile.

I added classes to differentiate between container and block tables. Also added a couple inline styles to help.

E.G.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta charset="utf-8">
    <title>Mobilize me</title>
    <style type="text/css">
        @media only screen and (max-width: 900px) {
            .container {
                width: 100% !important;
                text-align:center !important;
            }
            .blocktable {
                width: 100% !important;
                display: block !important;
                margin: 0 auto !important;
            }
        }
    </style>
</head>
<body>
    <table cellspacing="0" cellpadding="0" border="0" width="900" class="container" style="border-collapse:collapse;">
        <tr>
        <td align="center">
            <table align="left" bgcolor="RED" cellspacing="0" cellpadding="0" border="0" width="33%" class="blocktable" style="border-collapse:collapse;">
            <tr>
            <td>one</td>
            </tr>
            </table>
             <table align="left" bgcolor="GREEN" cellspacing="0" cellpadding="0" border="0" width="34%" class="blocktable" style="border-collapse:collapse;">
             <tr>
            <td>two</td>
            </tr>
            </table>
            <table align="right" bgcolor="BLUE" cellspacing="0" cellpadding="0" border="0" width="33%" class="blocktable" style="border-collapse:collapse;">
            <tr>
            <td>three</td>
            </tr>
            </table>
        </td>
        </tr>
    </table>
</body>
</html>



回答2:


I think it's safe to conclude, at the moment of this writing, that this technique is, or has become, unsupported.

A better approach would to work with 2 different displays: one design for desktop readers, and one for mobile readers, as demonstrated below (tested, and works):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <style type="text/css">
        <!--
        .ReadMsgBody {
            width: 100%;
        }
        .ExternalClass {
            width: 100%;
        }

        a:hover,
        a:visited,
        a:active {
            color: #ffffff;
        }

        @media screen and (max-width: 480px), screen and (max-device-width: 480px) {
            body { -webkit-text-size-adjust: none;}
            div[id=desktop] {
                display:none !important;
                width:0px !important;
                overflow:hidden !important;
            }
            div[id=mobile] {
                display:block !important;
                width:100% !important;
                height:auto !important;
                max-height:inherit !important;
                overflow:visible !important;
            }

            div[id=mobile] table{
                display: block !important;
                overflow: auto;
            }

            div[id=mobile] img{
                width: 100%;
            }
        -->
    </style>

    <style type="text/css">
        div.online p {margin:0; padding:0; margin-bottom:0;}
        div.online a:link, div.online a:visited, div.online a:hover, div.online a:active { color: #333333; }
    </style>
</head>

<body bgcolor="#e6e6e6" style="margin:0;">

    <div class="online"  style="margin:10px auto; color: #333333; font-family: verdana; font-size:11px;text-align:center;">
        <a href="http://somedomain.com/some/page.htm?lang=nl" target="_blank" style="text-decoration: none; color: #333333;">View in browser</a>
    </div>
    <div id="desktop">
        <table cellpadding="10" cellspacing="0" border="0" style="width:100%; background-color:#e6e6e6;">
            <tr>
                <td align="center">
                    <table cellpadding="0" cellspacing="0" border="0" style="width:620px; background-color:#ffffff; text-align:left;">
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="20" border="0" style="width:620px; text-align:left; background-color:#ffffff;">

                                    <tr>
                                        <td><a href="#" target="_blank"><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/header.jpg" border="0" style="display:block;" /></a></td>
                                    </tr>

                                </table>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="20" border="0">
                                    <tr>

                                        <td valign="top"><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/00f_1.gif" style="max-width: 240px;" /></td>
                                        <td valign="top">
                                            <table cellpadding="0" cellspacing="0" border="0" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">
                                                <tr>
                                                    <td style="font-size:22px; color:#fea900;">Block 1</td>
                                                </tr>
                                                <tr>
                                                    <td><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/filler12.png" /></td>
                                                </tr>
                                                <tr>
                                                    <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="20" border="0">
                                    <tr>

                                        <td valign="top"><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/0f0_1.gif" style="max-width: 240px;" /></td>
                                        <td valign="top">
                                            <table cellpadding="0" cellspacing="0" border="0" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#333333;">
                                                <tr>
                                                    <td style="font-size:22px; color:#fea900;">Block 2</td>
                                                </tr>
                                                <tr>
                                                    <td><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/filler12.png" /></td>
                                                </tr>
                                                <tr>
                                                    <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>

                        <tr>
                            <td align="center" style="border-bottom: 2px dotted #666666;"><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/filler12.png" /></td>
                        </tr>

                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div id="mobile" style="display:none; width:0px; max-height:0px; overflow:hidden;">
        <table cellpadding="10" cellspacing="0" border="0" style="width:100%; background-color:#e6e6e6; display:none;">
            <tr>
                <td align="center">
                    <table cellpadding="0" cellspacing="0" border="0" style="width:100%; background-color:#ffffff; text-align:left; display:none;">
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="0" border="0" style="width:100%; text-align:left; background-color:#ffffff; display:none;">
                                    <tr>
                                        <td><a href="#" target="_blank"><img id="header" src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/header.jpg" border="0" style="display:block;" /></a></td>
                                    </tr>
                                </table>
                            </td>
                        </tr>

                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="10" border="0" style="display:none;">
                                    <tr>
                                        <td>
                                            <table cellpadding="0" cellspacing="10" border="0" style="font-family:Arial; font-size:14px; color:#333333; display:none;">

                                                <tr>
                                                    <td><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/00f_1.gif" /></td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size:20px; color:#fea900;">Block 1</td>
                                                </tr>

                                                <tr>
                                                    <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <table cellpadding="0" cellspacing="10" border="0" style="display:none;">
                                    <tr>
                                        <td>
                                            <table cellpadding="0" cellspacing="10" border="0" style="font-family:Arial; font-size:14px; color:#333333; display:none;">

                                                <tr>
                                                    <td><img src="https://www.domain.com/x-http://somedomain.com/some/page/img/props/0f0_1.gif" /></td>
                                                </tr>
                                                <tr>
                                                    <td style="font-size:20px; color:#fea900;">Block 2</td>
                                                </tr>

                                                <tr>
                                                    <td>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</td>
                                                </tr>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                        <tr>
                            <td align="center" style="border-bottom: 2px dotted #666666;">&nbsp;</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </div>
    <div class="online" style="margin: 10px auto 25px; color: #333333; font-family: verdana; font-size:11px;text-align:center;">
        <p align="center">
            <a href="http://somedomain.com/some/page.htm?lang=nl" target="_blank" style="text-decoration: none; color: #333333;">Unsubscribe</a> -
            <a href="http://somedomain.com/some/page.htm?lang=nl" target="_blank" style="text-decoration: none; color: #333333;">Edit profile</a>
        </p>
        <p align="center" style="margin-top: 15px;"></p>
    </div>

</body>
</html>


来源:https://stackoverflow.com/questions/29389866/responsive-e-mail-turn-table-cells-td-to-cleared-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!