【问题标题】:HTML Table Formatting - Dynamic Column Widths based on contentHTML 表格格式 - 基于内容的动态列宽
【发布时间】:2021-03-16 18:26:38
【问题描述】:

我正在处理一个 HTML 文件,我将使用它作为电子邮件的附件和基于 HTML 的电子邮件消息的基础,该电子邮件消息有一个包含 13 列的表格。

我希望根据内容自动调整列的大小,以便具有较短标题和值的列不会占用与较长值列一样多的空间。现在我看到所有列的大小都是均匀的。

这是我当前的 CSS 和表格代码 sn-ps

     /* What it does: Stops Outlook from adding extra spacing to tables. */
     table, td {
         mso-table-lspace: 0pt !important;
         mso-table-rspace: 0pt !important;
     }
 
     /* What it does: Fixes webkit padding issue. Fix for Yahoo mail table alignment bug. Applies table-layout to the first 2 tables then removes for anything nested deeper. */
     table {
         border-spacing: 0 !important;
         border-collapse: collapse !important;
         table-layout: fixed !important;
         margin: 0 auto !important;
     }
 
     table table table {
         table-layout: auto;
     }
<table width="100%" border="1" style="font-size:10pt;table-layout: fixed;">
       <thead style="background-color:#10163C;color:#FFFFFF">
         <tr>
            <th>Semester</th>
            <th>Evaluation End Date<br/>(11:59 PM)</th>
            <th>Days<br/>Remaining</th>
            <th>Subject</th>
            <th>Catalog</th>
            <th>Section</th>
            <th>Course Name</th>
            <th>Level</th>
            <th>Trait</th>
            <th>Instructor</th>
            <th>Responses</th>
            <th>Enrollment</th>
            <th>Response<br/>Rate</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Fall 2020</td>
            <td>December 9, 2020</td>
            <td>5</td>
            <td>SUBJ</td>
            <td>1234</td>
            <td>MWE</td>
            <td>Class_Long NAme Goes Here</td>
            <td>UGRD</td>
            <td>Online</td>
            <td>Y Teacher</td>
            <td>0</td>
            <td>0</td>
            <td>0.00%</td>
        </tr>
    </tbody>

在示例中,Semester、Days Remaining、Subject、Catalog、Section、Level、Trait、Responses、Enrollment、Response Rate 列应该更窄,因为它们的值很短。

而评估结束日期和课程名称应该更宽,因为它们的值更长。

一个限制是我无法使用 JavaScript/JQuery,因为我使用电子邮件发送这些信息。

【问题讨论】:

    标签: html html-table css-tables


    【解决方案1】:

    这是一种方法。

    您可以在th 上应用min-width 并将宽度设置为100%,如下所示:

    th {
      width: 100%;
      min-width: 125px;
    }
    

    此外,我删除了固定布局规范,因为它会阻止某些样式应用于表格。

    /* What it does: Stops Outlook from adding extra spacing to tables. */
    
    table,
    td {
      mso-table-lspace: 0pt !important;
      mso-table-rspace: 0pt !important;
    }
    
    
    /* What it does: Fixes webkit padding issue. Fix for Yahoo mail table alignment bug. Applies table-layout to the first 2 tables then removes for anything nested deeper. */
    
    table {
      border-spacing: 0 !important;
      border-collapse: collapse !important;
      width: 100%;
      margin: 0 auto !important;
    }
    
    th,
    td {
      text-align: left;
    }
    
    th {
      width: 100%;
      min-width: 125px;
    }
    <div>
      <table border="1" style="font-size:10pt;">
        <thead style="background-color:#10163C;color:#FFFFFF">
          <tr style="background-color:#10163C;color:#FFFFFF">
            <th>Semester</th>
            <th>Evaluation End Date<br/>(11:59 PM)</th>
            <th>Days<br/>Remaining</th>
            <th>Subject</th>
            <th>Catalog</th>
            <th>Section</th>
            <th>Course Name</th>
            <th>Level</th>
            <th>Trait</th>
            <th>Instructor</th>
            <th>Responses</th>
            <th>Enrollment</th>
            <th>Response<br/>Rate</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Fall 2020</td>
            <td>December 9, 2020</td>
            <td>5</td>
            <td>SUBJ</td>
            <td>1234</td>
            <td>MWE</td>
            <td>Class_Long NAme Goes Here</td>
            <td>UGRD</td>
            <td>Online</td>
            <td>Y Teacher</td>
            <td>0</td>
            <td>0</td>
            <td>0.00%</td>
          </tr>
        </tbody>
      </table>
    </div>

    如果您不想设置宽度,这是另一种解决方案:

    /* What it does: Stops Outlook from adding extra spacing to tables. */
    
    table,
    td {
      mso-table-lspace: 0pt !important;
      mso-table-rspace: 0pt !important;
    }
    
    
    /* What it does: Fixes webkit padding issue. Fix for Yahoo mail table alignment bug. Applies table-layout to the first 2 tables then removes for anything nested deeper. */
    
    table {
      border-spacing: 0 !important;
      border-collapse: collapse !important;
      width: 100%;
      margin: 0 auto !important;
    }
    
    th,
    td {
      text-align: left;
    }
    
    th {
      word-wrap: break-word;
    }
    <div>
      <table border="1" style="font-size:10pt;">
        <thead style="background-color:#10163C;color:#FFFFFF">
          <tr style="background-color:#10163C;color:#FFFFFF">
            <th>Semester</th>
            <th>Evaluation End Date<br/>(11:59 PM)</th>
            <th>Days<br/>Remaining</th>
            <th>Subject</th>
            <th>Catalog</th>
            <th>Section</th>
            <th>Course Name</th>
            <th>Level</th>
            <th>Trait</th>
            <th>Instructor</th>
            <th>Responses</th>
            <th>Enrollment</th>
            <th>Response<br/>Rate</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Fall 2020</td>
            <td>December 9, 2020</td>
            <td>5</td>
            <td>SUBJ</td>
            <td>1234</td>
            <td>MWE</td>
            <td>Class_Long NAme Goes Here</td>
            <td>UGRD</td>
            <td>Online</td>
            <td>Y Teacher</td>
            <td>0</td>
            <td>0</td>
            <td>0.00%</td>
          </tr>
        </tbody>
      </table>
    </div>

    我所做的只是将word-wrap: break-word; 添加到th 元素。

    【讨论】:

    • 当我尝试您的解决方案时,列没有调整大小(第一列除外,如果我扩大窗口,该列只会调整更大)
    • 上面的sn-p你跑了吗?
    • 是的,即使在代码 sn-p 中,它也只会调整第一列的大小。
    • 在我的原始答案下方还包括了一个替代解决方案。您可以将word-wrap: break-word; 添加到th 元素中。
    • 仍然没有变化。我什至减少了列数,它没有改变。
    猜你喜欢
    • 2011-03-04
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多