css - 流体设计最大宽度在中断时更改为 100%(HTML 电子邮件)

标签 css html-email fluid-layout hybrid

我发现这个相关问题有一个很好的解决方案,其中有一个两列设计在达到 320 像素的阈值时堆叠。这很棒;但是,我很好奇是否有一种方法可以使用宽度属性来复制两列堆叠的这种行为,但不是将宽度限制为 320px,而是在彼此堆叠后它们变为宽度 100%。问题可以在这里找到:How to make html email responsive in gmail?

代码可以在下面找到

<!doctype html>
<html>
    <body style="margin:0;">
        <center>
            <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">




                <!-- // 2-COLUMN SCAFFOLD [CENTERING, FLUID] -->
                <tr>
                    <td align="center" height="100%" valign="top" width="100%">
                        <!--[if mso]>
                        <table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
                        <tr>
                        <td align="center" valign="top" width="660">
                        <![endif]-->
                        <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:660px;">
                            <tr>
                                <td align="center" valign="top" style="font-size:0;">
                                    <!--// DEVELOPER NOTES:
                                        1. Setting font-size:0; is necessary to ensure
                                           that there is no extra spacing introduced
                                           between the centering divs that wrap each
                                           of the columns. //-->

                                    <!--[if mso]>
                                    <table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
                                    <tr>
                                    <td align="left" valign="top" width="330">
                                    <![endif]-->
                                    <div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;">
                                        <!--// DEVELOPER NOTES:
                                            1. To have each column center upon stacking,
                                               wrap them in individual divs, set the same
                                               max-width and width as the table within it,
                                               and set display to inline-block; using
                                               vertical-align is optional.

                                            2. Setting min-width determines when the two
                                               columns of this block will wrap; in this
                                               case, when the total available width is
                                               less than or equal to 480px. //-->

                                        <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;">
                                            <tr>
                                                <td align="center" valign="top">

                                                    <!-- // REPLACE WITH BLOCK -->
                                                    <p style="background-color:#2BAADF; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">LEFT</p>
                                                    <!-- REPLACE WITH BLOCK // -->

                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    <!--[if mso]>
                                    </td>
                                    <td align="left" valign="top" width="330">
                                    <![endif]-->
                                    <div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;">
                                        <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;">
                                            <tr>
                                                <td align="center" valign="top">

                                                    <!-- // REPLACE WITH BLOCK -->
                                                    <p style="background-color:#51BBE5; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">RIGHT</p>
                                                    <!-- REPLACE WITH BLOCK // -->  

                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    <!--[if mso]>
                                    </td>
                                    </tr>
                                    </table>
                                    <![endif]-->
                                </td>
                            </tr>
                        </table>
                        <!--[if mso]>
                        </td>
                        </tr>
                        </table>
                        <![endif]-->
                    </td>
                </tr>
                <!-- 2-COLUMN SCAFFOLD [CENTERING, FLUID] // -->




            </table>
        </center>
    </body>
</html>

我建议对其进行测试,看看我指的是什么。任何建议将不胜感激,谢谢!

最佳答案

我相信这就是您要找的。您可以使用媒体查询来定位具有类的特定 HTML 元素。我给出了 FullWidth 类,当屏幕尺寸达到 480 像素时, block 将是屏幕的全宽。

一些注意事项:

  • 此代码将适用于最新的 Gmail 应用、iOS(通用电子邮件客户端)和 Android 4.4 及以下版本(通用电子邮件客户端)。
  • 将媒体查询移到 HTML 的头部。

@media only screen and (max-width:480px) {
.FullWidth{width:100% !important;max-width:100% !important;}
}
<!doctype html>

<html>
<body style="margin:0;">
        <center>
            <table border="0" cellpadding="0" cellspacing="0" height="100%" width="100%">
                <!-- // 2-COLUMN SCAFFOLD [CENTERING, FLUID] -->
                <tr>
                    <td align="center" height="100%" valign="top" width="100%">
                        <!--[if mso]>
                        <table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
                        <tr>
                        <td align="center" valign="top" width="660">
                        <![endif]-->
                        <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:660px;">
                            <tr>
                                <td align="center" valign="top" style="font-size:0;">
                                    <!--// DEVELOPER NOTES:
                                        1. Setting font-size:0; is necessary to ensure
                                           that there is no extra spacing introduced
                                           between the centering divs that wrap each
                                           of the columns. //-->

                                    <!--[if mso]>
                                    <table align="center" border="0" cellspacing="0" cellpadding="0" width="660">
                                    <tr>
                                    <td align="left" valign="top" width="330">
                                    <![endif]-->
                                    <div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;" class="FullWidth">
                                        <!--// DEVELOPER NOTES:
                                            1. To have each column center upon stacking,
                                               wrap them in individual divs, set the same
                                               max-width and width as the table within it,
                                               and set display to inline-block; using
                                               vertical-align is optional.

                                            2. Setting min-width determines when the two
                                               columns of this block will wrap; in this
                                               case, when the total available width is
                                               less than or equal to 480px. //-->

                                        <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;" class="FullWidth">
                                            <tr>
                                                <td align="center" valign="top">

                                                    <!-- // REPLACE WITH BLOCK -->
                                                    <p style="background-color:#2BAADF; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">LEFT</p>
                                                    <!-- REPLACE WITH BLOCK // -->

                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    <!--[if mso]>
                                    </td>
                                    <td align="left" valign="top" width="330">
                                    <![endif]-->
                                    <div style="display:inline-block; max-width:50%; min-width:240px; vertical-align:top; width:100%;" class="FullWidth">
                                        <table align="left" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width:330px;" class="FullWidth">
                                            <tr>
                                                <td align="center" valign="top">

                                                    <!-- // REPLACE WITH BLOCK -->
                                                    <p style="background-color:#51BBE5; color:#FFFFFF; font:16px Helvetica, sans-serif, normal; margin:0 !important; padding:10px;">RIGHT</p>
                                                    <!-- REPLACE WITH BLOCK // -->  

                                                </td>
                                            </tr>
                                        </table>
                                    </div>
                                    <!--[if mso]>
                                    </td>
                                    </tr>
                                    </table>
                                    <![endif]-->
                                </td>
                            </tr>
                        </table>
                        <!--[if mso]>
                        </td>
                        </tr>
                        </table>
                        <![endif]-->
                    </td>
                </tr>
                <!-- 2-COLUMN SCAFFOLD [CENTERING, FLUID] // -->




            </table>
        </center>
    </body>
</html>

有问题可以私信我

干杯

关于css - 流体设计最大宽度在中断时更改为 100%(HTML 电子邮件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241888/

相关文章:

css - 使并排表格匹配高度

css - 如何将静态和流体布局与 Twitter Bootstrap 相结合?

html - 具有固定像素间距的流体网格布局

javascript - 如何仅使用 javascript 在短时间内更改按钮文本?

javascript - Angularjs 等到 div 背景图像显示

HTML 电子邮件移动响应

image - 像素值是否更适合约束照片容器?

css - FireBug(或任何其他方法)-如何从 DIV 和子 div 复制所有 CSS 样式?

html - Bootstrap 仅显示移动菜单

java - 如何使用 Java Mail API 通过电子邮件发送带有外部 css 和 js 的 HTML 文件?