【问题标题】:Float table elements using only html/css仅使用 html/css 的浮动表格元素
【发布时间】:2015-06-22 04:41:15
【问题描述】:

我目前正在尝试编写一个响应式电子邮件模板,因此仅限于使用 html 表格和内联 css(没有 javascript 或媒体查询的帮助)以支持尽可能多的电子邮件客户端。

具体来说,我想浮动一些元素(整个表格或内部列),以便它们均匀地填充可用宽度,但当可用宽度不足以容纳所有文本时,它们也会下降到下一行.

这是我第一次尝试在可用宽度内均匀地分隔元素:http://jsfiddle.net/aerospatiale/noy7ur7a/

table { 
    width: 100%;
    max-width: 600px;
    background-color: #eeeeee;
    text-align: justify; 
}
table:after {
    display: inline-block !important;
}
table th { 
    vertical-align:top; 
    text-align: center;
    font-weight:normal; 
}

<table align="center">
    <tr>
        <th>One</th>
        <th>Two</th>
        <th>Three</th>
        <th>Four</th>
        <th>Five</th>
        <th>Six</th>
        <th>Seven</th>
        <th>Eight</th>
        <th>Nine</th>
        <th>Ten</th>
    </tr>
</table>

这是我的第二次尝试,它允许元素在必要时下降到下一行,但不会在可用的整个宽度内均匀分布元素:http://jsfiddle.net/5qsjn2ys/

table { 
    width: 100%;
    max-width: 600px;
    background-color: #eeeeee;
    text-align: justify; 
}
table th { 
    display: inline-block !important;
    vertical-align:top; 
    text-align: center;
    font-weight:normal; 
}

<table align="center">
    <tr>
        <th>One</th>
        <th>Two</th>
        <th>Three</th>
        <th>Four</th>
        <th>Five</th>
        <th>Six</th>
        <th>Seven</th>
        <th>Eight</th>
        <th>Nine</th>
        <th>Ten</th>
    </tr>
</table>

是否可以结合这两种效果来均匀分布元素,同时在可用宽度不足时允许它们下降到下一行?

注意:我意识到使用 th 元素有点不正统,但这是为了对抗 Android 怪癖。

【问题讨论】:

    标签: html css responsive-design html-email


    【解决方案1】:

    请尝试更改为display:block 以获得较小的屏幕尺寸。

    http://jsfiddle.net/afelixj/noy7ur7a/2/

    【讨论】:

    • 嘿@afelixj,这绝对是我追求的效果,但是我需要尽可能不使用 float 属性来实现它,因为 Outlook newsignature.com/articles/… 不支持它。
    • 可以拆分成不同的表吗?
    • 是的,当然!我仅限于表,但不介意拆分发生在表级别还是列级别。
    • 在一个表中使用两个th 进行测试。 http://jsfiddle.net/afelixj/noy7ur7a/3/.. 现在需要用于中心对齐的包装表...但是 table align="center" 无论如何都没有得到广泛支持。
    • 嗨@afelixj,感谢您的帮助,但该解决方案似乎仍然依赖浮动来工作。
    【解决方案2】:

    您可以使用表格来做到这一点,并记住将所有内容都设为内联 css,因为 gmail 去除了头部样式。我已经在我制作的一些响应式模板中这样做了。这可以在所有 42 个最常见的电子邮件/网络客户端中使用。

    演示: https://jsfiddle.net/aerospatiale/noy7ur7a/

    <table cellpadding="0" cellspacing="0" border="0" align="center" style="width: 100%;max-width: 600px;background-color: #eeeeee;">
    <tr>
        <td>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        1
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        2
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        3
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        4
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        5
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        6
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        7
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        8
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        9
                    </td>
                </tr>
            </table>
            <table align="left" cellpadding="0" cellspacing="0" border="0">
                <tr>
                    <td width="60" align="center">
                        10
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-02
      • 2011-11-06
      • 2021-12-14
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      • 2010-10-03
      • 1970-01-01
      相关资源
      最近更新 更多