css强制不换行,超出用省略号显示

DIV下的解决方案:

<html>
<head>
<title>Style5.cn</title>
<style type="text/css">
/* 公共样式 */
body { font-size: 14px; font-family:"宋体"; }
a { color: Black; text-decoration: none; }
a:hover { color: Blue; }
/* 截取文字的盒子 */
.autocut {
width:200px;
border:1px solid #333;
overflow:hidden;
white-space:nowrap;
text-overflow:ellipsis;
-o-text-overflow:ellipsis;
-icab-text-overflow: ellipsis;
-khtml-text-overflow: ellipsis;
-moz-text-overflow: ellipsis;
-webkit-text-overflow: ellipsis;
}
</style>
</head>
<body>
<div class="autocut">
<a href="http://www.style5.cn" title="TOP10热榜在图片上的半透明阴影效果">TOP10热榜在图片上的半透明阴影效果</a><br />
<a href="http://www.style5.cn" title="说说最近读的书:无懈可击的Web设计">说说最近读的书:无懈可击的Web设计</a><br />
</div>
</body>
</html>
上面的这种方案,在 IE7 和 Opera 中应该能够完美的体现出他的效果了。但是 Firefox 目前并不支持 CSS3 中的 text-overflow:ellipsis,所以这种方法还是有缺陷的,算是不完全版。

另外,在连接的 title 标签里加入完整的文章标题,这样想知道具体内容的浏览者就可以将鼠标放在连接上,看到完整的标题了。

解释一下起到关键作用的几行代码:



overflow:hidden;
/* 隐藏超出容器范围的文字 */
white-space:nowrap;
/* 强制文字将不自动换行 */
text-overflow:ellipsis;
/* 如果文字超出范围,将使用省略号标示出来 */
-o-text-overflow:ellipsis;
/* Opera 的私有属性,功能同上 */
-icab-text-overflow: ellipsis;
-khtml-text-overflow: ellipsis;
-moz-text-overflow: ellipsis;
/* 目前这段代码无效,为 Firefox 预留 */
-webkit-text-overflow: ellipsis;




TD下的解决方案(一):





1.只需要将表格宽度固定,然后在表格的Css定义中加入:table {
width:484px;
table-layout:fixed;
}2.然后在表格单元格的Css定义中加入:td {
overflow:hidden;
text-overflow:ellipsis;
}
TD下的解决方案(二):
[b]解决了在IE7下,JS调整宽度后,TD下的解决方案(一)失效的问题。[/b]
<table width="200" border="1" style="table-layout:fixed;">
<tr>
<td width="80" style="width:80px;overflow: hidden;text-overflow:ellipsis">
<nobr>我是一个兵,来自老百姓</nobr></td>
<td> </td>
<td> </td>
</tr>
</table>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值