【问题标题】:Two Rows in thead, Can't Set Column Widths in Second前两行,第二次无法设置列宽
【发布时间】:2018-07-20 18:37:42
【问题描述】:

我有一个包含两行标题的表格。我想设置第二行中 some 列的宽度。我设置了一个Codepen 来测试它。我发现如果我删除第一行标题,它会毫无问题地采用我指定的列宽。但是对于第一行标题,它会忽略第二行中列的宽度。 :(

请注意,我在th 中有div,所以我可以设置边框样式。

这是代码:

body {
  padding: 10px;
}

.data-table {
  width: 100%;
  border-spacing: 0 4px;
  border-collapse: separate;
  table-layout: fixed;
}/* Remove For SCSS*/
  .title-11, .title-21 {
    width: 40px;
  }
  .title-22 {
    width: 100px;
  }
  .title-24 {
    width: 100px;
  }

  thead tr th {
    border-collapse: separate; 
    border-spacing: 0 5px;
  }
   .title {
     /*
     background: linear-gradient(to right, transparent 0%, #bbb 0%,
       #bbb calc(100% - 10px), 
       transparent calc(100% - 10px)) 0 99% / 100% 1px 
       no-repeat; 
     */
     text-align: left; 
     padding-right: 10px;
  }

  .div-title {
    border-bottom: 1px solid #bbb;
  }
  .hdr-field {
    width: 150px;
  }
  tr .title:last-child {
    padding-right: 0;
  }
  .side-title {
    transform: rotate(-90deg);
    width: 25px;
  }
  .fieldname {
    width: 130px;
  }
  .fieldvalue.dest-data input[type=text] {
    width: 100%;
  }
  .bodySection {
    border-bottom: 10px solid #bbb;
    margin-bottom: 10px;
  }
  tr {
    // border-bottom: 10px solid #bbb;
  }
/*}*//* For SCSS*/
<table class="data-table">
  <thead>
    <tr>
      <th class="title-11"></th>
      <th colSpan="2" class="title title-12">
        <div class="div-title">Source</div>
      </th>
      <th colSpan="2" class="title title-13">
        <div class="div-title">Destination</div>
      </th>
    </tr>
    <tr>
      <th class="title-21"></th>
      <th colSpan="1" class="fieldname title title-22">
        <div class="div-title hdr-field">Field</div>
      </th>
      <th colSpan="1" class="title title-23">
        <div class="div-title">Value</div>
      </th>
      <th colSpan="1" class="fieldname title title-24">
        <div class="div-title hdr-field">Field</div>
      </th>
      <th colSpan="1" class="title title-25">
        <div class="div-title">Value</div>
      </th>
    </tr>
  </thead>
  <tbody class="bodySection">
    <tr>
      <td rowSpan="2" class="side-title">Name</td>
      <td class="fieldname src-data">Short Name</td>
      <td class="fieldvalue src-data"My Store/td>
      <td class="fieldname dest-data">Short Name</td>
      <td class="fieldvalue dest-data"><input type="text" value="My Store" /></td>
    </tr>
    <tr>
      <td class="fieldname src-data">Long Name</td>
      <td class="fieldvalue src-data"My Store/td>
      <td class="fieldname dest-data">Long Name</td>
      <td class="fieldvalue dest-data"><input type="text" value="My Store" /></td>
    </tr>
  </tbody>
  <tbody class="bodySection">
    <tr>
      <td rowSpan="2" class="side-title">Name2</td>
      <td class="fieldname src-data">Short Name2</td>
      <td class="fieldvalue src-data"My Store/td>
      <td class="fieldname dest-data">Short Name2</td>
      <td class="fieldvalue dest-data"><input type="text" value="My Store2" /></td>
    </tr>
    <tr>
      <td class="fieldname src-data">Long Name2</td>
      <td class="fieldvalue src-data"My Store/td>
      <td class="fieldname dest-data">Long Name2</td>
      <td class="fieldvalue dest-data"><input type="text" value="My Store2" /></td>
    </tr>
  </tbody>
</table>

所以只是澄清一下:问题是如何使字段列固定宽度,同时让值列找到自己的级别。

谢谢。

【问题讨论】:

    标签: html-table column-width


    【解决方案1】:

    我不太明白为什么如何计算表格布局,因为the algorythm seems pretty complex

    但是,您可以使用良好的&lt;colgroup&gt; 来实现所需的布局。 在HTML4 spec | Calculating column width 中有一些不太难理解的关于如何使用它们的信息。请注意,这也适用于新规范(不推荐使用 col 和 colgroup)。

    把这个放在&lt;table&gt;&lt;thead&gt; 之间,放在你的小提琴里:

    <colgroup>
      <col>
      <col width="150">
      <col>
      <col width="150">
      <col>
    </colgroup>
    

    演示工作:

    body {
      padding: 10px;
    }
    
    .data-table {
      width: 100%;
      border-spacing: 0 4px;
      border-collapse: separate;
      table-layout: fixed;
    }/* Remove For SCSS*/
      .title-11, .title-21 {
        width: 40px; /* Wont be applied */
      }
      .title-22 {
        width: 100px; /* Wont be applied */
      }
      .title-24 {
        width: 100px; /* Wont be applied */
      }
    
      thead tr th {
        border-collapse: separate; 
        border-spacing: 0 5px;
      }
       .title {
         /*
         background: linear-gradient(to right, transparent 0%, #bbb 0%,
           #bbb calc(100% - 10px), 
           transparent calc(100% - 10px)) 0 99% / 100% 1px 
           no-repeat; 
         */
         text-align: left; 
         padding-right: 10px;
      }
    
      .div-title {
        border-bottom: 1px solid #bbb;
      }
      .hdr-field {
        width: 150px;
      }
      tr .title:last-child {
        padding-right: 0;
      }
      .side-title {
        transform: rotate(-90deg);
        width: 25px;
      }
      .fieldname {
        width: 130px;
      }
      .fieldvalue.dest-data input[type=text] {
        width: 100%;
      }
      .bodySection {
        border-bottom: 10px solid #bbb;
        margin-bottom: 10px;
      }
      tr {
        // border-bottom: 10px solid #bbb;
      }
    /*}*//* For SCSS*/
    <table class="data-table">
      <colgroup>
        <col width="40">
        <col width="100">
        <col>
        <col width="100">
        <col>
      </colgroup>
      <thead>
        <tr>
          <th class="title-11"></th>
          <th colSpan="2" class="title title-12">
            <div class="div-title">Source</div>
          </th>
          <th colSpan="2" class="title title-13">
            <div class="div-title">Destination</div>
          </th>
        </tr>
        <tr>
          <th class="title-21"></th>
          <th class="fieldname title title-22">
            <div class="div-title hdr-field">Field</div>
          </th>
          <th class="title title-23">
            <div class="div-title">Value</div>
          </th>
          <th class="fieldname title title-24">
            <div class="div-title hdr-field">Field</div>
          </th>
          <th class="title title-25">
            <div class="div-title">Value</div>
          </th>
        </tr>
      </thead>
      <tbody class="bodySection">
        <tr>
          <td rowSpan="2" class="side-title">Name</td>
          <td class="fieldname src-data">Short Name</td>
          <td class="fieldvalue src-data"My Store/td>
          <td class="fieldname dest-data">Short Name</td>
          <td class="fieldvalue dest-data"><input type="text" value="My Store" /></td>
        </tr>
        <tr>
          <td class="fieldname src-data">Long Name</td>
          <td class="fieldvalue src-data"My Store/td>
          <td class="fieldname dest-data">Long Name</td>
          <td class="fieldvalue dest-data"><input type="text" value="My Store" /></td>
        </tr>
      </tbody>
      <tbody class="bodySection">
        <tr>
          <td rowSpan="2" class="side-title">Name2</td>
          <td class="fieldname src-data">Short Name2</td>
          <td class="fieldvalue src-data"My Store/td>
          <td class="fieldname dest-data">Short Name2</td>
          <td class="fieldvalue dest-data"><input type="text" value="My Store2" /></td>
        </tr>
        <tr>
          <td class="fieldname src-data">Long Name2</td>
          <td class="fieldvalue src-data"My Store/td>
          <td class="fieldname dest-data">Long Name2</td>
          <td class="fieldvalue dest-data"><input type="text" value="My Store2" /></td>
        </tr>
      </tbody>
    </table>

    【讨论】:

    • 好吧,字段列现在是固定宽度的。但是我失去了 Field 和 Value 列之间的空间。不过,我认为这是一场胜利。谢谢@Tyblitz。 (为什么他们把桌子设计得这么难?)
    • 没关系,我想通了。问题是长度不一致。差距又回来了!再次感谢@Tyblitz;很好的解决方案。我会阅读您指出的文档。
    猜你喜欢
    • 1970-01-01
    • 2017-01-21
    • 2020-02-04
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多