伙伴们,对于省略号呢,咱们前端攻城狮的实现方法可就是多种多样了,那接下来呢我就给你罗列一下如果用css书写这些特殊效果,来一起看看吧~~~
效果图:
实现代码:
HTML部分
<h3>使用css实现单行省略号</h3>
<div class="box1">
<a href="#" class="a1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam ipsaexplicabo quos sapiente ea error, mollitia necessitatibus animi facere rem non sed velit aperiam laboriosamdebitis. Quae deleniti doloremque nisi.
</a>
</div>
CSS部分
h3 {
padding-left: 10px;
}
.a1 {
text-decoration: none;
color: #000;
padding-left: 20px;
}
.box1 {
height: 40px;
line-height: 40px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
box-sizing: border-box;
/* 单行显示省略号 */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
注:此案例为京东首页的部分切图,此时的省略号可以用上述代码实现
效果图:
HTML部分
<h3>使用css实现三行之后显示省略号</h3>
<div class="box2">
<a href="#" class="a1">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Fugit dicta laudantium aspernatur illo id, beatae mollitia magnam, laboriosam cupiditate harum veritatis ullam delectus adipisci quasi, laborum ipsum quis est molestiae.
</a>
</div>
CSS部分
h3 {
padding-left: 10px;
}
.a1 {
text-decoration: none;
color: #000;
padding-left: 20px;
}
.box2 {
height: 60px;
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
overflow: hidden;
/* 三行显示省略号 */
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
实际案例应用场景说明:
注:此时明显是折行后在第二行多余的部分显示省略号,那由于内容不固定字数不固定,所以需要动态设置,那么就应用到了上述的多行显示省略号的方法
效果图:
HTML部分
<h3>使用css实现中间显示省略号方法一</h3>
<div class="box3">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Commodi perferendis iste sit! Et quos aspernatur suscipit ab qui? Cumque debitis fugiat ab fugit repudiandae, vel eius error nisi minus<span></span><a href="#">全文</a>
</div>
css部分
.box3 {
/* height: 120px; */
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
}
.box3 span::after {
content: '......';
}
实际案例应用场景说明:
效果图:
HTML部分
<h3>使用css实现中间显示省略号方法二</h3>
<div class="box4">
<a href="#">
<span class="span1" title="我是左侧内容我是左侧内容我是左侧内容">我是左侧内容我是左侧内容我是左侧内容</span>
<span class="span2" title="我是右侧内容我是右侧内容"></span>
</a>
</div>
css部分
.box4 {
height: 30px;
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
}
.box4 .span1 {
float: left;
width: 62%;
height: 30px;
line-height: 30px;
overflow: hidden;
}
.box4 a {
color: #000;
}
.box4 .span2::before {
content: attr(title);
width: 38%;
float: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
direction: rtl;
}
/* 优化两个span中间的间距 */
.box4 {
text-align: justify;
}
实际案例应用场景说明:
综上所述:以上四种方案是目前页面中应用较多的实现省略号的方法,仅代表个人观点,如您有更优的方法欢迎联系我们。
载链接:https://segmentfault.com/a/1190000020920000
在我们的日常开发工作中,文本溢出截断省略是很常见的一种需考虑的业务场景细节。看上去 “稀松平常” ,但在实现上却有不同的区分,是单行截断还是多行截断?多行的截断判断是基于行数还是基于高度?这些问题之下,都有哪些实现方案?他们之间的差异性和场景适应性又是如何?凡事就怕较真,较真必有成长。本文试图通过编码实践,给出一些答案。
核心 CSS 语句
优点
短板
适用场景
Demo
<style>
.demo {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
<body>
<div class="demo">这是一段很长的文本</div>
</body>
示例图片
核心 CSS 语句
优点
短板
适用场景
Demo
<style>
.demo {
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
</style>
<body>
<div class='demo'>这是一段很长的文本</div>
</body>
示例图片
优点
短板
适用场景
Demo
当前仅适用于文本为中文,若文本中有英文,可自行修改
<script type="text/javascript">
const text = '这是一段很长的文本';
const totalTextLen = text.length;
const formatStr = () => {
const ele = document.getElementsByClassName('demo')[0];
const lineNum = 2;
const baseWidth = window.getComputedStyle(ele).width;
const baseFontSize = window.getComputedStyle(ele).fontSize;
const lineWidth = +baseWidth.slice(0, -2);
// 所计算的strNum为元素内部一行可容纳的字数(不区分中英文)
const strNum = Math.floor(lineWidth / +baseFontSize.slice(0, -2));
let content = '';
// 多行可容纳总字数
const totalStrNum = Math.floor(strNum * lineNum);
const lastIndex = totalStrNum - totalTextLen;
if (totalTextLen > totalStrNum) {
content = text.slice(0, lastIndex - 3).concat('...');
} else {
content = text;
}
ele.innerHTML = content;
}
formatStr();
window.onresize = () => {
formatStr();
};
</script>
<body>
<div class='demo'></div>
</body>
示例图片
核心 CSS 语句
优点
短板
适用场景
Demo
<style>
.demo {
overflow: hidden;
max-height: 40px;
line-height: 20px;
}
</style>
<body>
<div class='demo'>这是一段很长的文本</div>
</body>
示例图片
核心 CSS 语句
优点
短板
适用场景
Demo
<style>
.demo {
position: relative;
line-height: 20px;
height: 40px;
overflow: hidden;
}
.demo::after {
content: "...";
position: absolute;
bottom: 0;
right: 0;
padding: 0 20px 0 10px;
}
</style>
<body>
<div class='demo'>这是一段很长的文本</div>
</body>
示例图片
核心 CSS 语句
优点
短板
适用场景
Demo
<style>
.demo {
background: #099;
max-height: 40px;
line-height: 20px;
overflow: hidden;
}
.demo::before{
float: left;
content:'';
width: 20px;
height: 40px;
}
.demo .text {
float: right;
width: 100%;
margin-left: -20px;
word-break: break-all;
}
.demo::after{
float:right;
content:'...';
width: 20px;
height: 20px;
position: relative;
left:100%;
transform: translate(-100%,-100%);
}
</style>
<body>
<div class='demo'>这是一段很长的文本</div>
</body>
示例图片
原理讲解
有 A、B、C 三个盒子,A 左浮动,B、C 右浮动。设置 A 盒子的高度与 B 盒子高度(或最大高度)要保持一致
凡重复的,让它单一;凡复杂的,让它简单。
每次都要搞一坨代码,太麻烦。这时候你需要考虑将文本截断的能力,封装成一个可随时调用的自定义容器组件。市面上很多 UI 组件库,都提供了同类组件的封装,如基于 Vue 的 ViewUI Pro,或面向小程序提供组件化解决能力的 MinUI。
转载链接:https://segmentfault.com/a/1190000020920000
时候需要控制下文字数,不然就会溢出,页面就会变样不美观。这时我们就可以用css控制字数,超出部分显示省略号。可以不换行,超出部分显示省略号,也可以可以换行,多行,超出部分显示省略号。
1.不换行,超出部分显示省略号
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>用css控制字数,超出部分显示省略号</title> <style type="text/css"> *{margin:0;padding:0;} body{width:1000px;margin:100px auto;} .box{ width:260px; /*超出部分就隐藏*/ overflow:hidden; /*不换行设定*/ white-space:nowrap; /*超出部分的文字显示省略号*/ text-overflow:ellipsis; } </style> </head> <body> <div class="box">用css控制字数,超出部分显示省略号用css控制字数,超出部分显示省略号</div> </body> </html>
效果图如下:
2.可以换行,多行,超出部分显示省略号
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>可以换行,多行,超出部分显示省略号</title> <style type="text/css"> *{margin:0;padding:0;} body{width:1000px;margin:100px auto;} .box{ width:260px; display: -webkit-box; -webkit-box-orient: vertical; /*2行*/ -webkit-line-clamp: 2; overflow: hidden; } </style> </head> <body> <div class="box">1.用css控制字数,超出部分显示省略号用css控制字数,超出部分显示省略号</div> <div class="box">2.用css控制字数,超出部分显示省略号用css控制字数,超出部分显示省略号</div> </body> </html>
效果图如下:
注:此方法适用于WebKit浏览器及移动端。
除注明外的文章,均为来源:汤久生博客,转载请保留本文地址!
原文地址:http://tangjiusheng.com/divcss/169.html
*请认真填写需求信息,我们会在24小时内与您取得联系。