css实现省略过长文字

display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis;
实现文字过长出现省略号的效果,可通过设置容器的样式来达成。以下是基本的CSS样式设置: ```css .container { width: 200px; /* 容器宽度 */ overflow: hidden; /* 超出部分隐藏 */ white-space: nowrap; /* 不换行 */ text-overflow: ellipsis; /* 超出部分显示省略号 */ } ``` 上述代码中,`width`属性用于设置容器的宽度,`overflow: hidden`将超出容器宽度的部分隐藏,`white-space: nowrap`使文字不换行,`text-overflow: ellipsis`让超出部分显示为省略号[^1]。 还可以通过以下示例代码实现类似效果: ```html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>text-overflow</title> </head> <body> <style type="text/css"> .test_demo_clip{ text-overflow:clip; overflow:hidden; white-space:nowrap; width:200px; background:#ccc; } .test_demo_ellipsis{ text-overflow:ellipsis; overflow:hidden; white-space:nowrap; width:200px; background:#ccc; } </style> <h2>text-overflow : clip </h2> <div class="test_demo_clip"> 不显示省略标记,而是简单的裁切条</div> <h2>text-overflow : ellipsis </h2> <div class="test_demo_ellipsis"> 当对象内文本溢出时显示省略标记</div> </body> </html> ``` 此代码中,`.test_demo_ellipsis`类的元素在文本溢出时会显示省略标记,而`.test_demo_clip`类的元素则简单裁切溢出部分,不显示省略标记[^2]。 另外,也可以将样式封装成类,方便复用: ```css .text-overflow{ white-space:nowrap; overflow:hidden; text-overflow:ellipsis; } ``` 在HTML中,只需给需要应用该效果的元素添加`text-overflow`类即可[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值