default
menu home
Icon library Sign up Login favorite_border message help_outline
Online notes - online bookmark

CSS学习心得:多行文本两端对齐,同时溢出部分自动截断并添加省略号...

styleTags: All CSS html javascript php/other IT
by: fanshome  2023-06-05 08:01(UTC)

Demo

在卡片列表式页面展示文章内容时,除文章标题外,经常需要显示文章简介。简单粗暴的做法就是只显示文章的开始部分内容,这时就会要求简介部分固定高度,不能超出,同时尤其是中文,不能轻易使用字符串截断,最好是能够使用CSS来截断,同时添加省略号...,以便在不同屏幕上,卡片宽度自适应的同时,简介部分的内容也实现自适应。

注意:-webkit-line-clamp 不规范的CSS属性,使用时请注意有浏览器兼容性问题。

另外,如果是单行文本截断并加省略号的话,要加white-space: nowrap;

CSScontent_copyCopy codes
.cText{
	width: 100%;
	height: 60px;
	color: #666;
	box-sizing: border-box;
	line-height: 20px;
	overflow: hidden;
	text-overflow: ellipsis;/*截断并显示省略号*/
	word-break: break-word;
	display: -webkit-box;
	-webkit-line-clamp: 3;/*行数*/
	-webkit-box-orient: vertical;
}
.breakAll{word-break: break-all;}
.justified{text-align: justify;}/*两端对齐*/
.boxWrap{
	margin: 8px auto;
	width: 80%;
	box-sizing: border-box;
	display: grid;
	grid-auto-flow: row;
	gap: 16px 0.5%;
	grid-template-columns: repeat(3,33%);
}
@media (max-width:599.9px){.boxWrap{grid-template-columns:repeat(1,100%);}}
.box{
	position: relative;
	overflow: hidden;
	box-sizing: border-box;
	padding: 20px;
	margin: 8px;
	min-width: 200px;
	border-radius: 6px;
	box-shadow: 0 3px 1px -2px rgb(0 0 0 / 20%), 0 2px 2px 0 rgb(0 0 0 / 14%), 0 1px 5px 0 rgb(0 0 0 / 12%);
}
htmlcontent_copyCopy codes
<div class='boxWrap'>
	<div class='box'>
		<div class='cText'>电脑浏览器可以调整窗口大小看自适应效果</div>
	</div>
	<div class='box'>
		<div class='cText'>三行或以上,特别长的内容,会溢出而自动截断。三行或以上,特别长的内容,会溢出而自动截断。三行或以上,特别长的内容,会溢出而自动截断。</div>
	</div>
	<div class='box'>
		<div class='cText'>break-word效果。Some English word is especially long like <strong>"supercalifragilisticexpiadocious"</strong>.</div>
	</div>
	<div class='box'>
		<div class='cText justified'>break-word效果。两端对齐。Some English word is especially long like <strong>"supercalifragilisticexpiadocious"</strong>.</div>
	</div>
	<div class='box'>
		<div class='cText breakAll'>break-all效果。Some English word is especially long like <strong>"supercalifragilisticexpiadocious"</strong>.</div>
	</div>
	<div class='box'>
		<div class='cText breakAll justified'>break-all效果。两端对齐。Some English word is especially long like <strong>"supercalifragilisticexpiadocious"</strong>.</div>
	</div>
	</div>
</div>

visibility 1768


- for heavy web user Online notes
adimg
logo Post a comment

captcha
Please check the captcha code
Cancel