【问题标题】:Media query used for email formatting not working用于电子邮件格式的媒体查询不起作用
【发布时间】:2019-09-11 01:10:10
【问题描述】:

我似乎无法使媒体查询起作用-至少对于需要起作用的一种相关样式-padding 用于table-wrapper 类。内联填充集总是获胜。

  1. 我认为我需要为媒体查询设置!important,所以我确实有。

  2. 媒体查询位于 html 顶部

  3. 这是在 html 的顶部 - <meta name="viewport" content="width=device-width">

  4. 其余样式内联/由应用完成。

有人知道我可能会错过什么吗?

https://codepen.io/lg00/pen/LvrXVY

<html xmlns="http://www.w3.org/1999/xhtml">

<meta name="viewport" content="width=device-width">
<meta http-equiv="Content-Type" content="text/html;  charset=UTF-8">
<meta charset="UTF-8">
<title></title>

<style>
/* Responsive */
@media screen and (max-width: 700px) {
  .table-wrapper {
    width: 100% !important; 
    margin-top: 0px !important; 
    border-radius: 0px !important; 
    padding: 5px 15px !important; 
  }

  .header {
    border-radius: 0px !important; 
  }

  .header-title {
    padding-left: 20px !important; 
    padding-right: 20px !important; 
  }

  .header-title h1 {
    font-size: 25px !important; 
  }

}
</style>

<div class="email-wrapper" style="background-color:#F5F6F7">
  <table width="100%" cellpadding="0" cellspacing="0">
    <tbody>
      <tr>
        <td>
          <!-- PADDING SET HERE -->
          <table cellpadding="0" cellspacing="0" class="table-wrapper" style="margin:auto; margin-top:50px; background-color:#fff; padding:64px 100px">
            <tbody>
              <tr>
                <td class="container header" style="background-repeat:no-repeat; border-top-left-radius:6px; border-top-right-radius:6px; text-align:center; color:#000; padding-bottom:32px; display:block !important; margin:0 auto !important; clear:both !important">

                    <div class="header-title" style="font-weight:bold">Test</div>
                </td>
              </tr>

              <tr>
                <td class="content" style="border-bottom-left-radius:6px; border-bottom-right-radius:6px; margin:0 auto !important; clear:both !important">

                  <div class="header-title" style="color:#000; text-align:center">
                    <h1 style="font-family:Helvetica Neue, Helvetica, Arial, sans-serif; margin-bottom:15px; color:#47505E; margin:0px 0 10px; line-height:1.2; font-weight:200; line-height:45px; font-weight:bold; margin-bottom:30px; font-size:28px; line-height:40px; margin-bottom:32px; font-weight:400; color:#000; padding-top:0px">Test The Length of the Title - Will it Wrap</h1>
                  </div>

                    <table width="100%" cellpadding="0" cellspacing="0">
                      <tbody>
                        <tr>
                        <td class="text-center" style="text-align:center">
                          <a href="test" class="btn-primary" style="color:#5633FF; word-wrap:break-word; font-family:Helvetica Neue, Helvetica, Arial, sans-serif; text-decoration:none; background-color:#5633FF; line-height:2; max-width:100%; font-size:14px; padding:8px 40px 8px 40px; margin-bottom:40px; font-weight:600; cursor:pointer; display:inline-block; border-radius:30px; margin-left:auto; margin-right:auto; text-align:center; color:#FFF !important">Test</a>
                        </td>
                      </tr>
                    </tbody>
                  </table>

                  <table class="data-table bordered" width="100%" cellpadding="0" cellspacing="0" style="padding-top:32px; padding-bottom:32px; border-top:1px solid #DBDBDB; border-bottom:1px solid #DBDBDB">
                    <tbody>
                      <tr>
                        <td class="data-table-title" style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; font-weight:bold; font-size:20px; padding-top:0px">Test</td>
                      </tr>

                      <tr>
                        <td style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px">Test</td>
                        <td class="text-right" style="text-align:right; letter-spacing:0.6px; padding-top:5px; padding-bottom:5px">Test</td>
                      </tr>

                      <tr class="bordered" style="border-top:1px solid #DBDBDB; border-bottom:1px solid #DBDBDB">
                        <td style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; border-top:1px solid #DBDBDB">Test</td>
                        <td class="text-right" style="text-align:right; letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; border-top:1px solid #DBDBDB">test</td>
                      </tr>

                    </tbody>
                  </table>

                  <table class="data-table" width="100%" cellpadding="0" cellspacing="0" style="padding-top:32px; padding-bottom:32px">
                    <tbody>
                      <tr>
                        <td style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; vertical-align: top; ">
                          <p style="margin-top: 0px; ">
                              <span class="bold" style="font-weight:bold">Test</span><br>
                              test<br>
                          </p>
                        </td>
                        <td style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; width: 20%"></td>
                        <td style="letter-spacing:0.6px; padding-top:5px; padding-bottom:5px; vertical-align: top; ">
                          <p style="margin-top: 0px; ">
                            <span class="bold" style="font-weight:bold">Test2</span><br>
                          </p>
                        </td>
                      </tr>
                    </tbody>
                  </table>
                </td>
              </tr>
            </tbody>
          </table>
          <div class="footer" style="width:100%; text-align:center; clear:both !important">
            <p class="text-center" style="text-align:center; font-size:12px; color:#666">Test</p>
          </div>
          <br>
        </td>
      </tr>
    </tbody>
  </table>
</div>

【问题讨论】:

  • 如果你想让它适用于包括 Outlook、Thunderbird 等在内的所有电子邮件服务提供商,我建议使用内联样式。
  • 啊,是的,如果您在 html 中注意到,所有样式都是内联的。我认为不可能的媒体查询

标签: css media-queries html-email


【解决方案1】:

这是您的媒体查询中的一个特殊性问题。将.header-title 移到.table-wrapper 上方,因为填充覆盖了.table-wrapper 中设置的内容

 .header-title {
    padding-left: 20px !important; 
    padding-right: 20px !important; 
  }

  .table-wrapper {
    width: 100% !important; 
    margin-top: 0px !important; 
    border-radius: 0px !important; 
    padding: 5px 15px !important; 
  }

  .header {
    border-radius: 0px !important; 
  }

【讨论】:

  • 谢谢!直到现在我才开始尝试这个,但他就是这样!这是一个特异性问题。
【解决方案2】:

请检查 CSS 特异性,正如您所说,内联样式是最具体的选择器并“获胜”。

CSS 特性:

  1. 内联样式
  2. ID,类
  3. 伪类,属性
  4. 元素,伪元素

【讨论】:

    【解决方案3】:

    把你的css改成这个,我希望它能工作。

    <style>
    //Default styling
      .table-wrapper {
        width: 50%; 
        margin-top: 0px; 
        border-radius: 0px; 
        padding: 5px 15px; 
      }
    /* Responsive */    
    @media screen and (max-width: 700px) {
      .table-wrapper {
        width: 100%; 
        margin-top: 0px; 
        border-radius: 0px; 
        padding: 5px 15px; 
      }
    
      .header {
        border-radius: 0px; 
      }
    
      .header-title {
        padding-left: 20px; 
        padding-right: 20px; 
      }
    
      .header-title h1 {
        font-size: 25px; 
      }
    
    }
    </style>
    

    【讨论】:

    • 我会尝试,但这适用于电子邮件吗?我认为样式需要内联定义。
    猜你喜欢
    • 2014-06-30
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2018-01-16
    相关资源
    最近更新 更多