【问题标题】:min-width for fluid column in fixed width table固定宽度表中流体列的最小宽度
【发布时间】:2014-11-14 00:01:29
【问题描述】:

我正在尝试创建一个表格,其中流体列具有最小宽度。 所有其他列的宽度都是固定的,不应变宽或变薄。

我可以让流体列正确地增长和收缩,以便它占用容器中将最大宽度设置为 900 像素的剩余空间,但是我无法让它采用最小宽度。

这意味着当窗口和容器被压扁时,流体柱被覆盖,而不是此时表现得像固定柱。

对 th 和/或 td 应用最小宽度不会做任何事情。 对流体 td 内的 div 应用 min-wdith 确实意味着文本具有最小宽度,但是它不会阻止列缩小到小于此最小值,因此文本位于下一列文本的下方。

有什么想法吗?

HTML:

<div class="container">
<table>
    <thead>
        <tr>
            <th class="fixed">fixed</th>
            <th class="fixed">fixed</th>
            <th class="fixed">fixed</th>
            <th class="fluid">fluid</th>
            <th class="fixed">fixed</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>fixed</td>
            <td>fixed</td>
            <td>fixed</td>
            <td class="fluid"><div align="left">Some text here which gets truncated, however should have min width</div></td>
            <td>fixed</td>
        </tr>  
    </tbody>            
</table>
</div>

CSS:

.container {
    max-width: 900px;
}

table {
    table-layout: fixed;
    width: 100%;
    border: 1px solid #333;
    border-collapse: separate;
    border-spacing: 0;
}

th.fixed {
    width: 100px;
}

th.fluid {
    min-width: 100px;
}

td.fluid div {
    width: auto;
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis;
}

td.fluid {
    background-color: #aaa;
    min-width: 100px;
}

td {
    background-color: #ddd;
    border-right: 1px solid #333;
}

tr td {
     text-align: center;
}

table th, table td {
    border-top: 1px solid #333;
}

JSfiddle: http://jsfiddle.net/ajcfrz1g/14/

【问题讨论】:

  • 设置表格的最小宽度以阻止其减小

标签: html css html-table fixed-width


【解决方案1】:

DEMO

.container {
    max-width: 900px;
}

table {
    table-layout: fixed;
    width: 100%;
     min-width: 500px;
    border: 1px solid #333;
    border-collapse: separate;
    border-spacing: 0;
}


th.fixed {
    width: 100px;


}

th.fluid {
    min-width: 100px;
}

td.fluid div {
    width: 100%;
    min-width:100px;
    white-space: nowrap; 
    overflow: hidden;
    text-overflow: ellipsis;
}

td.fluid {
    background-color: #aaa;
    min-width: 100px;
    width: 100%;
}

td {
    background-color: #ddd;
    border-right: 1px solid #333;
}
 tr td {
     text-align: center;
}
 }

table th, table td {
    border-top: 1px solid #333;
}

【讨论】:

  • 这很好用,谢谢!似乎只需要提到的表上的 min-width 属性。 width: 100% 而不是 auto 等以获得良好的表单/跨浏览器一致性?
【解决方案2】:

我正在努力解决您的问题。在此您的 fluid 没有最小宽度,因为这是表结构。但你可以给宽度。

看这个例子

.container {
    max-width: 900px;
}

table {
    table-layout: fixed;
    width: 100%;
    border: 1px solid #333;
    border-collapse: separate;
    border-spacing: 0;
}

th.fixed {
    width: 100px;
}

th.fluid {
    min-width: 100px;
}

td.fluid div {
	width: auto;
	overflow: hidden;
    text-overflow: ellipsis;
}

td.fluid {
    background-color: #aaa;
    min-width: 100px;
    white-space: nowrap;
    overflow: hidden;
}

td {
    background-color: #ddd;
    border-right: 1px solid #333;
}
 tr td {
     text-align: center;
}
 }

table th, table td {
    border-top: 1px solid #333;
}
<div class="container">
    <table>
        <thead>
            <tr>
                <th class="fixed">fixed</th>
                <th class="fixed">fixed</th>
                <th class="fixed">fixed</th>
                <th class="fluid">fluid</th>
                <th class="fixed">fixed</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>fixed</td>
                <td>fixed</td>
                <td>fixed</td>
                <td class="fluid"><div align="left">Some text here which gets truncated, however should have min width</div></td>
                <td>fixed</td>
            </tr>  
        </tbody>            
    </table>
</div>

【讨论】:

  • 不知道为什么你移动了空白:nowrap 和溢出:隐藏到 td.fluid,也不起作用。与原始行为相同。不过还是谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-12-22
  • 1970-01-01
  • 2011-11-30
  • 2011-12-22
  • 2023-03-27
  • 2012-08-28
  • 2020-09-10
  • 2012-07-15
相关资源
最近更新 更多