【问题标题】:Responsive scaling of images within table cells causes gaps in Chrome表格单元格内图像的响应缩放会导致 Chrome 中的间隙
【发布时间】:2014-12-16 21:57:24
【问题描述】:

我正在尝试将我公司的电子邮件设置为更适合移动设备。不幸的是,因为这是基于电子邮件的,我必须像 1999 年那样构建它们:主要是表格和图像切片。

我有设置为特定宽度的表格,没有定义高度。每张表不超过一行,通常为 2-3 列。每个表格单元格一张图片。目前,我没有定义表格单元格或图像的尺寸。

这是我正在处理的新响应代码:

@media (max-width: 670px) { 
    body { 
        background-color: red !important; /* for testing */
    }
    table {
        width: 100% !important;
        height: auto !important;
    }
    table img {
        width: 100% !important;
        height: auto !important;
    }
}
@media (max-width: 480px) { 
    body { 
        background-color: blue !important; /* for testing */
    }
}

http://codepen.io/BevansDesign/pen/CnAfB

正如你在我的 Codepen 页面上看到的,如果你缩小窗口的宽度,图形会相应地缩小。然而在 Chrome 中,由于基于像素的缩放有点奇怪,我在图像(绿线)之间得到了很多间隙。 (在 FF 中运行良好,当然在 IE8 中根本无法运行,我还没有在其他任何东西上测试过。)

有谁知道我怎样才能消除这些差距?我尝试将图像高度设置为 100%,但这只会使图像保持原始高度并水平挤压它们。

我知道我可以通过手动设置所有维度来解决此问题,但我正在寻找一种调整尽可能少的解决方案,因为我每周会发送 2-3 次这些电子邮件。

感谢您的帮助!

【问题讨论】:

    标签: html css email responsive-design


    【解决方案1】:

    也许对你有帮助

    source

    选项 1.) 将 style="display:block" 添加到您的图片中

    <img src="http://www.test.com/test.jpg" style="display:block">
    

    选项 2.) 如果您的图片高度大于 16 像素,请将图片中的“对齐”属性设置为以下任意一项:absmiddle、middle、left、right、absbottom、texttop 或 top

    <img src="http://www.test.com/test.jpg" align="absbottom">
    

    选项 3.) 将图像放置在样式为“line-height:10px”(或更低)的块元素中 示例:

    <div style="line-height:10px"><img src="http://www.test.com/test.jpg"></div>
    

    选项 4.) 将图像放置在 style="font-size:2px"(或更低)的块元素中

    <div style="font-size:2px"><img src="http://www.test.com/test.jpg"></div>
    

    选项 5.) 添加 style="float:left" 或 style="float:right"

    <img src="http://www.test.com/test.jpg" style="float:left">
    

    【讨论】:

    • 谢谢,当电子邮件中出现多余的空格时,我已经尝试过其中的一些(这就是为什么我将所有内容都放在一个单行表中,每个图像都在自己的单元格中) .但是,仅当我尝试缩放图像时才会出现此问题。
    • 如果我将表格缩小到 57%(只是为了随机选择一个数字),这会使我的 650 像素宽的表格变成 370.5 像素宽,尽管 Chrome 会将其四舍五入到最接近的像素。所以 3 张图像(200x344、213x344 和 217x344)最终具有不同的高度(114x196、121x195、124x197)。所以我正在寻找一种方法来告诉浏览器将它们全部缩放到表格的高度。
    【解决方案2】:

    我发现这个问题的最佳解决方案是使用背景图像,并将切片保存为透明 GIF 网格。间隙仍然出现,但不可见。

    在下面的示例中,第一个表使用常规切片,当您在浏览器中缩放它时,您会看到出现 1 或 2 个像素的狭窄间隙。 Chrome 似乎是最糟糕的,但它也发生在其他浏览器中。

    第二个表格使用了我的背景和 GIF 技术。响应式样式将图像切换为适合 Retina 的高分辨率版本。

    <!DOCTYPE html>
    <html>
    <head>
    <title>Slices</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    
        <style type="text/css">
            body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
            table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
            img { -ms-interpolation-mode: bicubic; }
            img { border: 0; line-height: 100%; outline: none; text-decoration: none; }
            table { border-collapse: collapse !important; }
            body { height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }
    
    
        @media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
          .switch-background {
            background-image: url(http://www.interactiveemail.co.uk/DB/slices/full_image_hi.jpg) !important; 
            background-repeat: no-repeat !important; 
            background-size: 100% !important; 
          }
    }
    
    
            @media screen and (max-width: 640px) {
                table { width: 100% !important; max-width: 414px !important; }
                table img { width: 100% !important; height: auto !important; }
          .switch-background {
            background-image: url(http://www.interactiveemail.co.uk/DB/slices/full_image_hi.jpg) !important; 
            background-repeat: no-repeat !important; 
            background-size: 100% !important; 
          }
    
    
    
            div[style*="margin: 16px 0;"] { margin: 0 !important; }
        </style>
    
    </head>
    
    <body style="background-color: #ffffff; margin: 0 !important; padding: 0 !important;">
    
    <!-- 100% table start -->
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td align="center" bgcolor="#ffffff">
    
                <!-- Regular slices -->
                <table width="600" border="0" cellpadding="0" cellspacing="0" align="center">
                    <tr>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_01.jpg" alt="Slice 1" width="201" height="106" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_02.jpg" alt="Slice 2" width="266" height="106" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_03.jpg" alt="Slice 3" width="133" height="106" style="display: block;"></td>
                    </tr>
                    <tr>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_04.jpg" alt="Slice 4" width="201" height="172" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_05.jpg" alt="Slice 5" width="266" height="172" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_06.jpg" alt="Slice 6" width="133" height="172" style="display: block;"></td>
                    </tr>
                    <tr>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_07.jpg" alt="Slice 7" width="201" height="123" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_08.jpg" alt="Slice 8" width="266" height="123" style="display: block;"></td>
                      <td><img src="http://www.interactiveemail.co.uk/DB/slices/slice_09.jpg" alt="Slice 9" width="133" height="123" style="display: block;"></td>
                    </tr>
                </table>            
    
                <br><br>
    
                <!-- Transparent slices -->
                <table width="600" border="0" cellpadding="0" cellspacing="0" align="center" background="http://www.interactiveemail.co.uk/DB/slices/full_image_lo.jpg" class="switch-background">
                  <tr>
                    <td><!--[if gte mso 9]>
                        <v:rect xmlns:v="urn:schemas-microsoft-com:vml" fill="true" stroke="false" style="width:600px;height:401px;">
                        <v:fill type="tile" src="http://www.interactiveemail.co.uk/DB/slices/full_image_lo.jpg" color="#ffffff" />
                        <v:textbox inset="0,0,0,0">
                        <![endif]-->
                        <div><table width="600" border="0" cellspacing="0" cellpadding="0">
                          <tr>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_01.gif" alt="Slice 1" width="201" height="106" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_02.gif" alt="Slice 2" width="266" height="106" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_03.gif" alt="Slice 3" width="133" height="106" style="display: block;"></td>
                          </tr>
                          <tr>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_04.gif" alt="Slice 4" width="201" height="172" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_05.gif" alt="Slice 5" width="266" height="172" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_06.gif" alt="Slice 6" width="133" height="172" style="display: block;"></td>
                          </tr>
                          <tr>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_07.gif" alt="Slice 7" width="201" height="123" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_08.gif" alt="Slice 8" width="266" height="123" style="display: block;"></td>
                            <td><img src="http://www.interactiveemail.co.uk/DB/slices/blank_slice_09.gif" alt="Slice 9" width="133" height="123" style="display: block;"></td>
                          </tr>       
                        </table></div>
                        <!--[if gte mso 9]>
                        </v:textbox>
                        </v:rect>
                        <![endif]-->
                  </td>
                 </tr>
                </table>    
    
            </td>
        </tr>
    </table>
    <!-- 100% table end -->
    
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 2013-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多