轉載
首先需要在table中設置table-layout:fixed;
<table style="table-layout:fixed"></table>
然后在表頭th中設置每列的寬度
<table style="table-layout:fixed"> <th width="10%">Title01</th> <th width="20%">Title02</th> <!-- 其他th --> </table>
然后在需要當長度大於一定數值時用省略號表示的td上面添加樣式
<table style="table-layout:fixed"> <th width="10%">Title01</th> <th width="20%">Title02</th> <!-- 其他th --> <c:foreach items = "" var ="" varStatus = ""> <td><title01</td> <td style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis;"><title02</td> <!-- other td --> </c:foreach> </table>
bootstrap在設置表格大小時需要設置到th中,否則可能不會生效,以上在bootstrap中可用
當不使用bootstrap的時候可以使用如下樣式,網上搜索到的,比較好用
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
.mytable {
table-layout: fixed;
width: 98% border:0px;
margin: 0px;
}
.mytable tr td {
text-overflow: ellipsis; /* for IE */
-moz-text-overflow: ellipsis; /* for Firefox,mozilla */
overflow: hidden;
white-space: nowrap;
border: 1px solid;
text-align: left
}
</style>
</head>
<body>
<table width="500px" class="mytable">
<tr>
<td width="20%">再別康橋</td>
<td width="80%">
輕輕我走了,正如我輕輕地來,我揮一揮衣袖,不帶走一片雲彩
</td>
</tr>
</table>
</body>
</html>
