【发布时间】:2015-11-27 06:24:21
【问题描述】:
我要显示下表:
+----+------+-------------+---------+
| id | name | description | actions |
+----+------+-------------+---------+
| .. | .. | .. | .. |
我想要那个
- 第一列 (id) 的宽度尽可能最小 (white-space:nowrap)
- 其他列(名称、描述)应在必要时分词(自动换行:break-word)
- 最后一列也有可能的最小宽度(空白:nowrap)
我有什么:
<table class="table_standard" style="word-wrap:break-word; table-layout: fixed;">
但使用此代码,所有列的宽度都相同。
我该如何解决这个问题?
@thomasfuchs
这就是它在极低分辨率下的样子:
如您所见,如果分辨率太低,我希望浏览器打断单词,但它只是不显示完整的表格。
这是我当前的代码:
<style>
col.id { width: 1%; }
col.name { width: 20%; }
col.description { width: 30%; }
col.users { width: 30%; }
col.games { width: 9%; }
col.functions { width: 9%; }
col.actions { width: 1%; }
table td:nth-child(2) { word-wrap: break-word; }
table td:nth-child(3) { word-wrap: break-word; }
table td:nth-child(4) { word-wrap: break-word; }
</style>
<table class="table_standard" style="word-wrap: break-word">
<colgroup>
<col class="id">
<col class="name">
<col class="description">
<col class="users">
<col class="games">
<col class="functions">
<col class="actions">
</colgroup>
<tr>
<th class="th_titlebar" colspan="7">Alle Gruppen</th>
</tr>
<tr>
<th class="th_subtitle">ID</th>
<th class="th_subtitle">Name</th>
<th class="th_subtitle">Description</th>
<th class="th_subtitle">Members</th>
<th class="th_subtitle">Games</th>
<th class="th_subtitle">Functions</th>
<th class="th_subtitle">Actions</th>
</tr>
//loop with data from the db
</table>
【问题讨论】:
-
你需要
table-layout:fixed吗?如果你删除它,并给列(<td>元素宽度为1%, 30%, auto, 1%或类似的东西,表格应该按照你想要的格式进行格式化。 -
@thomasfuchs 实际上,如果他必须在表格列上执行并且 inline 是必要的,那么您只需要在第一行(通常是第 th 元素)上执行此操作
-
样式必须内联吗?
-
不。我现在注意到一个问题:如果我删除
table-layout: fixed,表格的最新列将无法再看到,因为窗口在那里结束。 -
table: 固定,设置为等宽,无论内容如何,都应该做 width: 100%