英文:
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:
Some similar question, but which doesn't quite match my request:
答案1
得分: 1
如果您被迫采用一个不太规范的解决方案,可以重新编写一些 CSS 类,以便在 HTML 中使用网格(grid)而不是表格进行设置。
由于 HTML 只是数据包装器,而 CSS 是样式,理论上您应该可以仅使用 CSS 来解决大多数样式问题。
这种不太规范的解决方案会覆盖所有的 tr
、td
和 th
元素,以使用网格(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 -->
<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>
<!-- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论