使用CSS将表格的第一列更改为行标题外观。

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

Change first column of table to a row header appearance using CSS

问题

抱歉,你提供的内容中已经没有需要翻译的部分了。如果你有任何其他需要翻译的文本,请随时提出。

英文:

I'm kind of thinking this might not be possible using only CSS, but I just wanted to ask: Is it possible using only CSS to change the first <td> to appear as if it was a header for the entire column?

Sadly, I'm not able to easily change the html, and I can't easily run javascript to change the DOM, so I'm stuck with the following html structure:

<table>
 <thead>
  <tr>
   <th>Hide this</th>
   <th>text</th>
   <th>value</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>term a</td>
   <td>Value of a</td>
   <td>123</td>
  </tr>
  <tr>
   <td>term b</td>
   <td>Value of b</td>
   <td>345</td>
  </tr>
 </tbody>
</table>

And I would love for it resemble a output like:

使用CSS将表格的第一列更改为行标题外观。

Some similar question, but which doesn't quite match my request:

答案1

得分: 1

如果您被迫采用一个不太规范的解决方案,可以重新编写一些 CSS 类,以便在 HTML 中使用网格(grid)而不是表格进行设置。
由于 HTML 只是数据包装器,而 CSS 是样式,理论上您应该可以仅使用 CSS 来解决大多数样式问题。

这种不太规范的解决方案会覆盖所有的 trtdth 元素,以使用网格(grid)代替。

我们可以通过以下方式重新定义 tr 内容的宽度,使其具有两列:

tr {
  grid-template-columns: 1fr 1fr;
}

之后,我们可以重新定义 tr 的第一个子元素,使其具有容器的全宽度。在这个示例中,跨越了网格的第一个和第二个元素。

tbody tr td:first-child {
  background: lightblue;
  grid-column-start: 1;
  grid-column-end: 3;
  text-align: center;
}

这将使您的渲染看起来像这样,只使用 CSS 来重新定义 HTML。

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

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

tr,
td,
th {
  display: grid;
}

table {
  border: 1px solid gray;
}

td {
  border: 1px solid gray;
}

tr {
  grid-template-columns: 1fr 1fr;
}

thead tr th:first-child {
  display: none;
}

tbody tr td:first-child {
  background: lightblue;
  grid-column-start: 1;
  grid-column-end: 3;
  text-align: center;
  font-weight: bold;
}

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

<table>
  <thead>
    <tr>
      <th>隐藏此列</th>
      <th>文本</th>
      <th>数值</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>项目 A</td>
      <td>项目 A 的值</td>
      <td>123</td>
    </tr>
    <tr>
      <td>项目 B</td>
      <td>项目 B 的值</td>
      <td>345</td>
    </tr>
  </tbody>
</table>

<!-- end snippet -->

再次强调,这应该是最后的尝试,因为您将覆盖许多基本样式,并迫使数据呈现更为静态。

英文:

If you are locked into creating a hacky solution, you can rewrite some css classes to use grid instead of using the table setup in the html.
Since html is just an data wrapper and css is the styling, you should theoretically solve most styling issues with css only.

This hacky soution overwrites all tr, td, and th elements to use grid instead.

we can restyle the tr width of the content to have 2 colums using

tr {
  grid-template-columns: 1fr 1fr;
}

After that we can get the first child of an tr and style that to have full width of the container. in this example spanning from the first and the second element of the grid.

tbody tr td:first-child {
  background: lightblue;
  grid-column-start: 1;
  grid-column-end: 3;
  text-align: center;
}

This will give you an rendering looking like this, using only css to restyle the html.

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

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

tr,
td,
th {
  display: grid;
}

table {
  border: 1px solid gray;
}

td {
  border: 1px solid gray;
}

tr {
  grid-template-columns: 1fr 1fr;
}

thead tr th:first-child {
  display: none;
}

tbody tr td:first-child {
  background: lightblue;
  grid-column-start: 1;
  grid-column-end: 3;
  text-align: center;
  font-weight: bold;
}

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

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Hide this&lt;/th&gt;
      &lt;th&gt;text&lt;/th&gt;
      &lt;th&gt;value&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;term a&lt;/td&gt;
      &lt;td&gt;Value of a&lt;/td&gt;
      &lt;td&gt;123&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;term b&lt;/td&gt;
      &lt;td&gt;Value of b&lt;/td&gt;
      &lt;td&gt;345&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Again this should be a last effort since you will overwrite lots of basic styling, and force a more static rendering of the data.

huangapple
  • 本文由 发表于 2023年1月8日 22:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048693.html
匿名

发表评论

匿名网友

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

确定