【问题标题】:HTML table: columns with minimum width and break-wordHTML 表格:具有最小宽度和断字的列
【发布时间】: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

这是 1920x1080 显示器的外观:

这是屏幕宽度较小时的外观:

这就是它在极低分辨率下的样子:

如您所见,如果分辨率太低,我希望浏览器打断单词,但它只是不显示完整的表格。

这是我当前的代码:

<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吗?如果你删除它,并给列(&lt;td&gt; 元素宽度为1%, 30%, auto, 1% 或类似的东西,表格应该按照你想要的格式进行格式化。
  • @thomasfuchs 实际上,如果他必须在表格列上执行并且 inline 是必要的,那么您只需要在第一行(通常是第 th 元素)上执行此操作
  • 样式必须内联吗?
  • 不。我现在注意到一个问题:如果我删除table-layout: fixed,表格的最新列将无法再看到,因为窗口在那里结束。
  • table: 固定,设置为等宽,无论内容如何,​​都应该做 width: 100%

标签: html css


【解决方案1】:

在 HTML5 中,use the colgroup element, which contains col elements that define the desired widths

<colgroup>
  <col class="id">
  <col class="name">
  <col class="description">
  <col class="actions">
</colgroup>

table 元素下直接插入colgroup 元素。

然后您可以只使用 CSS 来定义列宽:

col.id          { width: 100px; }
col.name        { width: 200px; }
col.description { width: 300px; }
col.actions     { width: 100px; }

等等。这是针对具有fixed 布局的表。如果您希望浏览器动态调整表格的内容(如果您愿意,您的问题不清楚),您可以省略table-layout 属性(或将其设置为auto),并使用上面的colgroup定义宽度。这一次,它基本上只是向浏览器“提示”如何格式化:

col.id          { width: 1%; }
col.name        { width: 30%; }
col.actions     { width: 1%; }

省略描述列应分配该列可用的其余宽度。

希望这会有所帮助!

【讨论】:

  • 好吧,这看起来不错,但我怎样才能包含word-wrap: break-word
  • 我会为你的&lt;td&gt; 元素设置它,例如:table td:first-child { word-wrap: break-word; } 如果你想在第一列中使用word-wrap: break-word
  • 我编辑了我的第一个帖子/问题并添加了一些我的问题的图片。我还添加了我目前使用的代码。
  • @Bernd:您必须指定表格宽度,例如width:100% 在表格元素上
  • @Bernd:关于这个主题还有其他一些 SO 问题/答案,例如stackoverflow.com/questions/6666532/…
【解决方案2】:

看看这里,它以动态方式工作 -

<table class="table_standard" border='1' style='position : absolute; top : 0px left : 0px; word-wrap:break-word; text-align:center; '>
        <tr>
            <th> ID </th>
            <th> Name </th>
            <th> Description </th>
            <th> Action </th>
        </tr>
        <tr>
            <td>...</td>
            <td>........</td>
            <td>................</td>
            <td>.....</td>
        </tr>        
    </table>

如果您不想要动态列宽,请尝试每个固定列 - 以下方式

table, th, td {
    border: 1px solid black;
    text-align:center; 
    word-wrap:break-word;
}
<table>
  <tr>
    <th style='width:30px'>ID</th>
    <th style='width:80px'>Name</th>
    <th style='width:150px'>Description</th>
    <th style='width:75px'>Action</th>
  </tr>
  <tr>
    <td>..</td>
    <td>..</td>
    <td>..</td>
    <td>..</td>
  </tr>
</table>

【讨论】:

  • 您不必像那样制作 col 元素,您实际上可以将宽度放在 th 上,它应该没问题....
  • 你实际上应该把它放到样式标签中......因为html5不再支持宽度......但是你也不应该使用一般的内联样式...... 耸肩
  • 我真的不想在列上设置绝对宽度(如 80px 或 10%),因为 id 可以达到 10000,然后绝对宽度不再适合。当我删除 table-layout: fixed 时,这些词不再被破坏。表格的宽度为 100%
  • @Daemede 或者我们如何在样式标签中做到这一点,它只会为所有th 取一个值。
  • 这就是你使用样式标签的方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 2018-04-24
  • 2014-10-31
相关资源
最近更新 更多