txc*_*hou 22 html css html-table
是否有可能使表(具有动态创建的文本)具有例如:
表格宽度100%的屏幕5列最后一列是空的()前四列有文本,宽度是动态的,最后一列是宽度的剩余部分而没有压缩前四列中的文本
到目前为止我有这个并且它压缩了我正试图避免的文本:
<table style="width:100%;" border="1">
<tr>
<td>One Two Three</td>
<td>Two Three Four</td>
<td>Three Four Five</td>
<td>Four Five Six</td>
<td style="width:100%;"> </td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
我不能使用"white-space:nowrap",因为如果有足够的文本,我希望文本包装.
编辑:"我只是喜欢[前四]列,如果我从表格标签中删除宽度属性,它们将是宽度.但仍然有表格的100%宽度."
有任何想法吗?CSS解决方案更好!
您可以使用flexbox来完成此任务
table {
width:100%;
border: 1px solid black;
border-collapse: collapse;
}
td + td {
border-left: 1px solid black;
}
tr {
display: flex;
align-items: stretch;
}
td:last-child {
flex: 1;
background: yellow;
display:inline-block;
/* to keep IE happy */
}
Run Code Online (Sandbox Code Playgroud)
FIDDLE(调整浏览器窗口大小以查看此操作)
table {
width: 100%;
border: 1px solid black;
border-collapse: collapse;
}
td + td {
border-left: 1px solid black;
}
td:last-child {
background: yellow;
}Run Code Online (Sandbox Code Playgroud)
<table>
<tr>
<td>One Two Three</td>
<td>Two Three Four</td>
<td>Three Four FiveThree Four FiveThree Four FiveThree Four FiveThree Four Five</td>
<td>Four Five Six</td>
<td> f</td>
</tr>
</table>Run Code Online (Sandbox Code Playgroud)
IE 10-11有一个问题是Inline元素(以及显然也是表格单元格)不被视为flex-items.
这个问题由Phillip Walton 在此记录,解决方法(来自上面的帖子)是:
通过向项添加非内联显示值可以避免此问题,例如块,内联块,弹性等.
td实际上并没有填满剩余的空间,而是占用了它自己的自然空间 - 这是OP不想要的.
只需添加
table-layout: fixed;
Run Code Online (Sandbox Code Playgroud)
到你的桌子,你就完成了.
假设您在其他列中的文本不强制表格大于100%,则您的最后一列将始终拉伸,因此无需在<td>上应用任何宽度.
这对我有用(在 FireFox、Chrome 和旧的 12.x Opera 中)。我想要 100% 宽度的表格和填充其余部分的最后一列有类tableList,这里是 CSS:
.tableList { width: 100%; }
.tableList td { width: 1px; } /* min width, actually: this causes the width to fit the content */
.tableList td:last-child { width: 100%; } /* well, it's less than 100% in the end, but this still works for me */
Run Code Online (Sandbox Code Playgroud)
下面,有一个片段可以尝试:
.tableList { width: 100%; }
.tableList td { width: 1px; } /* min width, actually: this causes the width to fit the content */
.tableList td:last-child { width: 100%; } /* well, it's less than 100% in the end, but this still works for me */
Run Code Online (Sandbox Code Playgroud)
.tableList { width: 100%; }
.tableList td { width: 1px; } /* min width, actually: this causes the width to fit the content */
.tableList td:last-child { width: 100%; background-color: yellow; } /* well, it's less than 100% in the end, but this still works for me */Run Code Online (Sandbox Code Playgroud)
就像 Rahat 注意到的那样,如果除第一列之外的列的内容超过一个单词,则它变为多行(每个单词一行):
<table class="tableList">
<tr><td>game's</td><td>not</td><td>over</td></tr>
<tr><td>one</td><td>two</td><td>three and more</td></tr>
</table>Run Code Online (Sandbox Code Playgroud)
.tableList { width: 100%; }
.tableList td { width: 1px; } /* min width, actually: this causes the width to fit the content */
.tableList td:last-child { width: 100%; background-color: yellow; } /* well, it's less than 100% in the end, but this still works for me */Run Code Online (Sandbox Code Playgroud)
但他也提出了一种解决方法 - 添加white-space: nowrap:
<table class="tableList">
<tr><td>game's</td><td>not</td><td>over</td></tr>
<tr><td>one</td><td>two plus plus</td><td>three and more</td></tr>
</table>Run Code Online (Sandbox Code Playgroud)
.tableList { width: 100%; }
.tableList td { width: 1px; white-space: nowrap; } /* min width, actually: this causes the width to fit the content */
.tableList td:last-child { width: 100%; background-color: yellow; } /* well, it's less than 100% in the end, but this still works for me */Run Code Online (Sandbox Code Playgroud)