如何在表头 th 中设置输入,而不增加列宽?

huangapple go评论72阅读模式
英文:

How to set an input in a table-header th, without increasing the column-widths?

问题

以下是翻译的HTML和CSS代码:

<table class="my-table">
  <thead>
    <tr>
      <th>
        <div>
          ID
        </div>
      </th>
      <th>
        <div>
          Name
        </div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1245</td>
      <td>Michael Knight</td>
    </tr>
    <tr>
      <td>1247</td>
      <td>Devon Miles</td>
    </tr>
  </tbody>
</table>
.my-table {
  border: 1px solid;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: separate;
  box-sizing: border-box;
}
.my-table thead th {
   border-bottom: 1px solid;
   border-right: solid 1px;
}
.my-table tbody td {
  border-top: none;
  border-left: none;
  border-bottom: 1px dotted;
  border-right: 1px solid;
}

希望这有所帮助!

英文:

Here's some table html-code, where the header-widths fit to their contents:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.my-table {
  border: 1px solid;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: separate;
  box-sizing: border-box;
}
.my-table thead th {
   border-bottom: 1px solid ;
   border-right: solid 1px;
}
.my-table tbody td {
  border-top: none;
  border-left: none;
  border-bottom: 1px dotted;
  border-right:  1px solid;
}

<!-- language: lang-html -->

&lt;table class=&quot;my-table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;div&gt;
          ID
        &lt;/div&gt;
      &lt;/th&gt;
      &lt;th&gt;
        &lt;div&gt;
          Name
        &lt;/div&gt;
      &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1245&lt;/td&gt;
      &lt;td&gt;Michael Knight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1247&lt;/td&gt;
      &lt;td&gt;Devon Miles&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Now I want to add text-inputs below the headers, but the input should not increase the header-widths. Does anyone know how to achieve this?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.my-table {
  border: 1px solid;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: separate;
  box-sizing: border-box;
}
.my-table thead th {
   border-bottom: 1px solid ;
   border-right: solid 1px;
}
.my-table tbody td {
  border-top: none;
  border-left: none;
  border-bottom: 1px dotted;
  border-right:  1px solid;
}

<!-- language: lang-html -->

&lt;table class=&quot;my-table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;div&gt;
          ID
        &lt;/div&gt;
        &lt;div&gt;
          &lt;input type=&quot;text&quot;&gt;
        &lt;/div&gt;
      &lt;/th&gt;
      &lt;th&gt;
        &lt;div&gt;
          Name
        &lt;/div&gt;
        &lt;div&gt;
          &lt;input type=&quot;text&quot;&gt;
        &lt;/div&gt;
      &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1245&lt;/td&gt;
      &lt;td&gt;Michael Knight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1247&lt;/td&gt;
      &lt;td&gt;Devon Miles&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Thanks!

答案1

得分: 3

width设置为0,然后将min-width设置为100%。width将其缩小到零尺寸,然后,由于min-width会覆盖width(参见MDN),它会扩展以适应内容。将box-sizing设置为border-box以容纳填充。

.my-table {
  border: 1px solid;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: separate;
  box-sizing: border-box;
}
.my-table thead th {
  border-bottom: 1px solid;
  border-right: 1px solid;
}
.my-table tbody td {
  border-top: none;
  border-left: none;
  border-bottom: 1px dotted;
  border-right: 1px solid;
}
.my-table input[type="text"] {
  width: 0;
  min-width: 100%;
  box-sizing: border-box;
}
<table class="my-table">
  <thead>
    <tr>
      <th>
        <div>
          ID
        </div>
        <div>
          <input type="text">
        </div>
      </th>
      <th>
        <div>
          Name
        </div>
        <div>
          <input type="text">
        </div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1245</td>
      <td>Michael Knight</td>
    </tr>
    <tr>
      <td>1247</td>
      <td>Devon Miles</td>
    </tr>
  </tbody>
</table>
英文:

Set the width to 0 then min-width to 100%. Width will shrink it down to no size then, as min-width overrides width (see MDN) it then expands to fit the content. Set box-sizing to border box to accomodate the padding.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.my-table {
  border: 1px solid;
  border-spacing: 0;
  table-layout: fixed;
  border-collapse: separate;
  box-sizing: border-box;
}
.my-table thead th {
   border-bottom: 1px solid ;
   border-right: solid 1px;
}
.my-table tbody td {
  border-top: none;
  border-left: none;
  border-bottom: 1px dotted;
  border-right:  1px solid;
}
.my-table input[type=&quot;text&quot;] {
  width:0;
  min-width:100%;
  box-sizing: border-box;
}

<!-- language: lang-html -->

&lt;table class=&quot;my-table&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;
        &lt;div&gt;
          ID
        &lt;/div&gt;
        &lt;div&gt;
          &lt;input type=&quot;text&quot;&gt;
        &lt;/div&gt;
      &lt;/th&gt;
      &lt;th&gt;
        &lt;div&gt;
          Name
        &lt;/div&gt;
        &lt;div&gt;
          &lt;input type=&quot;text&quot;&gt;
        &lt;/div&gt;
      &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;1245&lt;/td&gt;
      &lt;td&gt;Michael Knight&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;1247&lt;/td&gt;
      &lt;td&gt;Devon Miles&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月11日 17:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定