纯css实现文本隐藏(全兼容版本和非全兼容版本)cv就能用!!!

文章介绍了如何在HTML和CSS中实现浮动元素的定位以及内容的可展开/收起功能,使用了两种不同的方法:一种是基于Flexbox的现代浏览器兼容方式,另一种是使用CSS3专有属性`-webkit-line-clamp`实现,展示了兼容不同浏览器的解决方案。
摘要由CSDN通过智能技术生成

先贴效果图:

兼容版本:

	<!DOCTYPE html>
	<html lang="zh-CN">
		<head>
			<meta charset="UTF-8">
			<meta name="viewport" content="width=device-width, initial-scale=1.0">

			<title>Document</title>
		</head>

		<body>
			<div class="wrap">
				<input type="checkbox" id="exp" style="display: none;">
				<div class="text">
					<label class="btn" for="exp"></label>
					浮动元素是如何定位的
					正如我们前面提到的那样,当一个元素浮动之后,它会被移出正常的文档流,然后向左或者向右平移,一直平移直到碰到了所处的容器的边框,或者碰到另外一个浮动的元素。
				</div>
			</div>
		</body>

		<style>
	* {
		padding: 0;
		margin: 0;
		border: none;
		line-height: 1;
	}

	.text {
		/* // display: -webkit-box; */
		/* // -webkit-line-clamp: 3; */
		/* // -webkit-box-orient: vertical; */
		line-height: 1.5;
		max-height: 4.5em;
        /* 要n行文本隐藏就max-height写 n*1.5 em */
		overflow: hidden;
		position: relative;
	}

	.btn {
		float: right;
		clear: both;
		position: relative;
		margin-left: 20px;
		font-size: 16px;

		background: #3F51B5;
		border-radius: 4px;
		color: #fff;
		cursor: pointer;
		border: 0;
		overflow: visible !important;
	}

	.text::before {
		content: '';
		float: right;
		width: 0;
		/* // background: red */
		height: calc(100% - 20px);
		/* // height: 100%; */
		/* // margin-bottom: -20px; */
	}

	.wrap {
		display: flex;
		/* //解决.text::before 高度100%不生效问题 */
	}

	.btn::before {
		content: '...';
		position: absolute;
		left: -18px;
		color: #333;
		/* // transform: translateX(-100%) */
	}

	#exp:checked + .text {
		/* -webkit-line-clamp: 999; */
		max-height: none;
		/* // 设置一个足够大的行数就可以了 */
	}

	.btn::after {
		content: '展开'
			/*采用content生成*/
	}

	#exp:checked + .text .btn::after {
		content: '收起'
	}

	#exp:checked + .text .btn::before {
		visibility: hidden;
		/*在展开状态下隐藏省略号*/
	}

	.text {
		transition: .3s max-height;
	}

	#exp:checked + .text {
		max-height: 500px;
		/*超出最大行高度就可以了 或者直接设置为none。 max-height: none;*/
	}

    /* 下面是解决如果文本不足行数也就是不省略时候,展开按钮依旧存在的问题 */
	.text::after {
		content: '';
		width: 100%;
		height: 100%;
		position: absolute;
		background: #fff;
	}

	#exp:checked + .text::after {
		visibility: hidden;
	}
		</style>
	</html>

非兼容版本:

	<!DOCTYPE html>
	<html lang="zh-CN">
		<head>
			<meta charset="UTF-8">
			<meta name="viewport" content="width=device-width, initial-scale=1.0">

			<title>Document</title>
		</head>

		<body>
			<div class="wrap">
				<input type="checkbox" id="exp" style="display: none;">
				<div class="text">
					<label class="btn" for="exp"></label>
					浮动元素是如何定位的
					正如我们前面提到的那样,当一个元素浮动之后,它会被移出正常的文档流,然后向左或者向右平移,一直平移直到碰到了所处的容器的边框,或者碰到另外一个浮动的元素。
				</div>
			</div>
		</body>

		<style>
	* {
		padding: 0;
		margin: 0;
		border: none;
		line-height: 1;
	}

	.text {
		font-size: 20px;
		overflow: hidden;
		text-overflow: ellipsis;
		text-align: justify;
		/* display: flex; */
		display: -webkit-box;
		-webkit-line-clamp: 3;
		-webkit-box-orient: vertical;
		position: relative;
	}

	.btn {
		float: right;
		clear: both;
		position: relative;
		margin-left: 20px;
		font-size: 16px;

		background: #3F51B5;
		border-radius: 4px;
		color: #fff;
		cursor: pointer;
		border: 0;
		overflow: visible !important;
	}

	.text::before {
		content: '';
		float: right;
		width: 0;
		/* // background: red */
		height: calc(100% - 16px);
		/* // height: 100%; */
		/* // margin-bottom: -20px; */
	}

	.wrap {
		display: flex;
		/* //解决.text::before 高度100%不生效问题 */
	}

	.btn::before {
		content: '展开';
	}

	#exp:checked + .text .btn::before {
		content: '收起'
	}

	#exp:checked + .text::after {
		visibility: hidden;
	}

	#exp:checked + .text {
		-webkit-line-clamp: 999;
		/*设置一个足够大的行数就可以了*/
	}

/* 下面是解决如果文本不足行数也就是不省略时候,展开按钮依旧存在的问题 */
	.text::after {
		content: '';
		width: 100%;
		height: 100%;
		position: absolute;
		background: #fff;
	}
	
	.exp:checked+.text::after{
	    visibility: hidden;
	}
		</style>
	</html>

思路来源于阅文前端团队 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柑橘乌云_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值