首页 > 解决方案 > 使用 Thymeleaf 设置表格中的列宽

问题描述

我有一个表格,其中第一列的宽度是固定的,其余列的宽度将平均占用剩余的速度。问题是列是使用生成th:each的,列数会有所不同。

如何将第一列设置为固定宽度,并将剩余空间平均分配给未知数量的列?

    <thead>
        <tr>
            <th style="width: 25%">
                Fixed width
            </th>
            <th th:each="instance: ${headers}">
                Dynamic width
            </th>   
        </tr>
    </thead>

标签: htmlcssthymeleaf

解决方案


您可以像这样计算宽度:

<thead th:with="fixed_width=25, dynamic_width=${(100 - fixed_width) / (#lists.size(headers) - 1)}">
  <tr>
    <th th:each="instance, status: ${headers}" th:style="|width: ${status.first ? fixed_width : dynamic_width}%|">
      Dynamic width
    </th>   
  </tr>
</thead>

推荐阅读