【发布时间】:2020-02-13 09:54:56
【问题描述】:
我正在尝试在 Angular 中创建一个具有动态列数和行数的表。通过执行以下操作,我发现使用 HTML 表格元素很容易完成:
<table class="html-table">
<tr class="headers">
<th>Name</th>
<th>Age</th>
<th>Sex</th>
<th *ngIf="columns == 4">Species</th>
</tr>
<tr>
<td>Ryan</td>
<td>30</td>
<td>M</td>
<td *ngIf="columns == 4">Human</td>
</tr>
</table>
在此示例中,可能有一些按钮可以在 3 和 4 之间切换 columns 的值。
我在网上看到的每一个解释都涉及更改 CSS 变量,这似乎是一种完成本应简单的事情的有点老套的方法。有什么方法可以让我指定某个东西应该是 CSS 网格中的新列,而不是必须指定 grid-template-columns 中的列数?
【问题讨论】:
-
坦率地说,CSS-Grid 并不是
table的理想替代品。使用有效且语义上正确的方法。 -
这是我推荐使用表格的少数场景之一
-
CSS 变量不是 hacky : developer.mozilla.org/en-US/docs/Web/CSS/… 可能不容易高效使用,但不是 hacky 因为 drafts.csswg.org/css-variables/#using-variables ,只是没有最终确定 ;)
-
对于 css-grid 你要求的是 grid-auto-columns: auto;连同 grid-auto-flow: column;希望这会有所帮助。
-
@RudyLister 太棒了!那么如何告诉子元素是行还是新列呢?
标签: html css html-table flexbox css-grid