【发布时间】:2014-08-16 06:43:37
【问题描述】:
我在 JSFiddle 中有以下内容:http://jsfiddle.net/X37V7/
HTML
<caption>Table 1 Example</caption>
<table id="fx">
<tr>
<th class="a75">Values</th>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
</tr>
<tr>
<th class="a75">Initial value</th>
<td class="a25">One</td>
<td class="a25">Two</td>
<td class="a25">Three</td>
<td class="a25">Four</td>
</tr>
</table>
<caption>Table 2 Example</caption>
<table id="fx2">
<tr>
<th class="a75">Values</th>
<td class="a25 rotate overflow he rr">Long Text Example</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
<td class="a25 rotate overflow he rr">1</td>
</tr>
<tr>
<th class="a75">Initial value</th>
<td class="a25">One</td>
<td class="a25">Two</td>
<td class="a25">Three</td>
<td class="a25">Four</td>
</tr>
CSS
body {
width:750px;
}
table {
display:block;
border-collapse:collapse;
width:100%;
overflow:hidden;
border-style:solid;
border-width:1px;
}
tr,th,td {
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
word-wrap:break-word;
border-style:solid;
border-width:1px;
}
th {
width:5%;
}
.over {
overflow:hidden;
}
.a24 {
width:10%;
}
.a75 {
width:60%;
}
.he {
height:130px;
}
.rotate {
-webkit-transform:rotate(270deg);
}
.rr {
height:-1px;
width:130px;
margin-left:-30px;
}
#fx,#fx2 {
table-layout:fixed;
overflow:hidden;
}
有两个表格示例。两者都设置为固定宽度。
第一个表格正确显示,因为单元格中的文本小于其尺寸。
第二个表格显示了当插入更长的文本时会发生什么 - 列变得更宽。
有没有办法强制列/单元格保持其设置的大小,而不管其内容如何?
【问题讨论】:
-
您的 css 有一个
a24类,但您的表格标记使用的是a25类。 -
问题是你有
white-space: nowrap,它可以防止文本换行到另外两行。按照设计,表格单元格将始终扩展其高度和宽度以显示其内容。 -
非环绕文本是一个小问题。在您的示例中,您正在旋转 label/th 字段以将它们垂直放置,然后您会遇到一些问题,即表格单元格在旋转后没有缩小以匹配较小的宽度。这个问题还有更多,然后首先出现!
-
对a24错误表示歉意,已纠正但未解决问题。
-
@MarcAudet - 是的,它确实给我带来了问题。无论旋转如何,我都不相信可以实际强制单元格或列保持其大小而不管其内容如何。