如何将此文本居中放在电子邮件模板中的表格行中

时间:2018-05-16 11:34:04

标签: css html-table html-email

我试图将这个 lorem ipsum 文本放在一个表格行中,但由于某种原因,它上面的图像 - 即使它们在单独的tr中 - 似乎也会影响到它。只有在我删除它们之后,文本才会居中...我怎么能阻止它们影响下面的文本放置..?

以下代码:

    @import url('https://fonts.googleapis.com/css?family=Open+Sans');

    * {
        font-family: 'Open Sans', sans-serif;
    }

    .main-text {
       /* padding-left: 130px;*/
        padding-bottom: 45px;
        margin: 0 auto;
    }

    #main {
        background: lightblue;
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
        margin-left: auto;
        margin-right: auto;
        width: 620px;
    }

    table {
        font-family: sans-serif;
    }

    .img-left {
        padding: 20px 20px 80px 40px;
    }

    .img-right {
        width: 85px;
        float: right;
        padding: 15px 30px;
        position: relative;
        right: -10px;
    }
<table id="main" border="0" cellpadding="0" cellspacing="0" id="templateColumns">
    <tr class="header-test">
        <td align="center" valign="top" width="50%" class="templateColumnContainer img-left">
            <table border="0" cellpadding="10" cellspacing="0" width="100%">
                <tr>
                    <td class="leftColumnContent" style="padding-top: 5px;">
                        <img src="http://via.placeholder.com/350x150" width="210" style="max-width:210px; position: relative; left: -15px;" class="columnImage" />
                    </td>
                </tr>
            </table>
        </td>
        <td align="center" valign="top" width="50%" class="templateColumnContainer">
            <table border="0" cellpadding="10" cellspacing="0" width="100%">
                <tr>
                    <td class="rightColumnContent">
                        <a href="#" target="_blank">
                            <img src="http://via.placeholder.com/350x150" 
                                 width="85" class="columnImage img-right" />
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <td align="left" style="width: 100%;">
            <table style="margin: 0 auto;">
                <tr>
                    <td></td>
                    <td class="main-text" style="color: #ffffff; text-align: center">
                        <p style="font-size: 12px; line-height: 1;">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Natus, veniam sint? Ducimus eius necessitatibus minima nemo ratione. Nam atque aliquam quia itaque, explicabo qui, fuga alias unde nulla quo quaerat.</p>

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

1 个答案:

答案 0 :(得分:3)

您的内部表格只有包装表格单元格的宽度,因此右侧有一个空格。将colspan="2"添加到包装td,它应该像下面的代码段一样居中。

    @import url('https://fonts.googleapis.com/css?family=Open+Sans');

    * {
        font-family: 'Open Sans', sans-serif;
    }

    .main-text {
       /* padding-left: 130px;*/
        padding-bottom: 45px;
        margin: 0 auto;
    }

    #main {
        background: lightblue;
        background-position: center;
        background-size: cover;
        background-repeat: no-repeat;
        margin-left: auto;
        margin-right: auto;
        width: 620px;
    }

    table {
        font-family: sans-serif;
    }

    .img-left {
        padding: 20px 20px 80px 40px;
    }

    .img-right {
        width: 85px;
        float: right;
        padding: 15px 30px;
        position: relative;
        right: -10px;
    }
<table id="main" border="0" cellpadding="0" cellspacing="0" id="templateColumns">
    <tr class="header-test">
        <td align="center" valign="top" width="50%" class="templateColumnContainer img-left">
            <table border="0" cellpadding="10" cellspacing="0" width="100%">
                <tr>
                    <td class="leftColumnContent" style="padding-top: 5px;">
                        <img src="http://via.placeholder.com/350x150" width="210" style="max-width:210px; position: relative; left: -15px;" class="columnImage" />
                    </td>
                </tr>
            </table>
        </td>
        <td align="center" valign="top" width="50%" class="templateColumnContainer">
            <table border="0" cellpadding="10" cellspacing="0" width="100%">
                <tr>
                    <td class="rightColumnContent">
                        <a href="#" target="_blank">
                            <img src="http://via.placeholder.com/350x150" 
                                 width="85" class="columnImage img-right" />
                        </a>
                    </td>
                </tr>
            </table>
        </td>
    </tr>
    <tr>
        <!-- add colspan="2" in the line below! -->
        <td align="left" style="width: 100%;" colspan="2">
            <table style="margin: 0 auto;">
                <tr>
                    <td></td>
                    <td class="main-text" style="color: #ffffff; text-align: center">
                        <p style="font-size: 12px; line-height: 1;">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Natus, veniam sint? Ducimus eius necessitatibus minima nemo ratione. Nam atque aliquam quia itaque, explicabo qui, fuga alias unde nulla quo quaerat.</p>

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