CSS使表格达到最大宽度的100%

时间:2013-09-27 07:50:31

标签: css html-table width html-email

给出以下电子邮件模板:

<style>
  @import url("http://fonts.googleapis.com/css?family=Open Sans");
</style>

<div style="width:100%; background:#F2F2F2">
  <table style="padding: 25px; margin: 0 auto; font-family:'Open Sans', 'Helvetica', 'Arial';">
    <tr align="center" style="margin: 0; padding: 0;">
      <td>
        <table style="border-style:solid; border-width:2px; border-color: #c3d2d9;" cellspacing="0">
          <tr style="background-color: white;">
            <td style="width: 700px; padding: 10px 15px 10px 15px; color: #000000;">
              <p>Some content here</p>
              <span style="font-weight: bold;">My Signature</span><br/>
              My Title<br/>
              My Company<br/>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</div>

表格正好是700px宽,是需要的。但是,由于其宽度完全固定,因此无法在宽度小于700px的设备上调整大小。但是如果我将td元素修改为:

<td style="max-width: 700px; width: 90%; padding: 10px 15px 10px 15px; color: #000000;">
   <p>Some content here</p>
   <span style="font-weight: bold;">My Signature</span><br/>
   My Title<br/>
   My Company<br/>
</td>

然后表只有~100px宽。

我如何重新排序CSS以使表格为700px,但随着视口变小而调整大小?

7 个答案:

答案 0 :(得分:15)

喜欢这个

<强> demo

<强> CSS

table{
    width:100%;
}

答案 1 :(得分:2)

我遇到了同样的问题,因为我有引导类&#34; hidden-lg&#34;在桌子上导致它愚蠢地变成显示:阻止!重要;

我想知道Bootstrap从未考虑过这样做:

@media (min-width: 1200px) {
    .hidden-lg {
         display: none;
    }
}

然后将元素留给以前用于其他屏幕尺寸的任何显示器......也许它太先进了,他们无法弄清楚..

无论如何:

table {
    display: table; /* check so these really applies */
    width: 100%;
}

应该有效

答案 2 :(得分:2)

您需要使用:

    table{
        width:100%;
        table-layout: fixed;
        overflow-wrap: break-word;
    }

Demo

答案 3 :(得分:0)

绝对不能很好地支持max-width。如果您要使用它,请在样式标记中的媒体查询中使用它。 ios,android和windows phone默认邮件都支持它们。 (gmail和outlook mobile没有)

http://www.campaignmonitor.com/guides/mobile/targeting/

看看底部的星巴克示例

答案 4 :(得分:0)

我为max-width: 100%的表提供了一个很好的解决方案。 只需对表单元格(标题单元格除外)使用word-break: break-all;即可将所有长文本分成几行:

<!DOCTYPE html>
<html>
<head>
<style>

table {
  max-width: 100%; 
}
table td {
  word-break: break-all;
}

</style>
</head>
<body>

<table border="1">
  <tr>
    <th><strong>Input</strong></th>
    <th><strong>Output</strong></th>
  </tr>
  <tr>
    <td>some text</td>
    <td>12b6459fc6b4cabb4b1990be1a78e4dc5fa79c3a0fe9aa9f0386d673cfb762171a4aaa363b8dac4c33e0ad23e4830888</td>
  </tr>
</table>

</body>
</html>

这将以如下方式呈现(当屏幕宽度受到限制时):

答案 5 :(得分:0)

我必须使用:

table, tbody {
    width: 100%;
}

仅靠table还是不够的,tbody也需要它来为我工作。

答案 6 :(得分:-2)

使用此:

<div style="width:700px; background:#F2F2F2">
    <table style="width:100%;padding: 25px; margin: 0 auto; font-family:'Open Sans', 'Helvetica', 'Arial';">
        <tr align="center" style="margin: 0; padding: 0;">
            <td>
                <table style="width:100%;border-style:solid; border-width:2px; border-color: #c3d2d9;" cellspacing="0">
                    <tr style="background-color: white;">
                        <td style=" padding: 10px 15px 10px 15px; color: #000000;">
                            <p>Some content here</p>
                            <span style="font-weight: bold;">My Signature</span><br/>
                            My Title<br/>
                            My Company<br/>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>