CSS多行文本省略 私有属性和伪元素

私有属性:
优点:简洁明了
缺点:私有属性 有比较大的局限性

.box{
	width: 300px;
	height: 90px;
	border:1px solid #ccc;
	font-size: 16px;
	overflow:hidden; // 多余隐藏
	display:-webkit-box; // 自适应布局 和flexbox有点像 可以设置盒子内部元素的排列方向
	-webkit-box-orient:vertical; // 文本从上至下 垂直方向
	-webkit-line-clamp:4; // 设置第几行开始省略
}

伪元素1:
优点:css规范 不用担心私有属性跨浏览器的问题
缺点:如果背景不是纯色 就比较难处理

.box{
	height: 90px;
	width: 300px;
	border:1px solid #ccc;
	font-size: 16px;
	overflow: hidden;
	position: relative;
	padding-right: 1em;
	text-align: justify;
}
.box::before{
	content: "...";
	position: absolute;
	right: 0;
	bottom: 0;
}
.box::after{
	content: "";
	background-color: #fff;
	width: 1em;
	height: 1em;
	position: absolute;
	display: inline;
	right: 0;
	margin-top: 0.5em;
}

伪元素2: 相比上面那种 这种比较有创意
优点:css规范 不用担心私有属性跨浏览器的问题
缺点:如果背景不是纯色 就比较难处理

.box{
	height: 90px;
	width: 300px;
	border:1px solid #ccc;
	font-size: 16px;
	overflow: hidden;
	position: relative;
	text-align: justify;
}
.box::after{
	content: "";
	position: absolute;
	height: 1.2em;
	width: 20%;
	background: linear-gradient(to right, rgba(255,255,255,0),blue 80%);
	right: 0;
	bottom: 0;
	margin-bottom: 0.2em;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值