伙伴們,對(duì)于省略號(hào)呢,咱們前端攻城獅的實(shí)現(xiàn)方法可就是多種多樣了,那接下來(lái)呢我就給你羅列一下如果用css書(shū)寫(xiě)這些特殊效果,來(lái)一起看看吧~~~
效果圖:
實(shí)現(xiàn)代碼:
HTML部分
<h3>使用css實(shí)現(xiàn)單行省略號(hào)</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;
/* 單行顯示省略號(hào) */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
注:此案例為京東首頁(yè)的部分切圖,此時(shí)的省略號(hào)可以用上述代碼實(shí)現(xiàn)
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)三行之后顯示省略號(hào)</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;
/* 三行顯示省略號(hào) */
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
注:此時(shí)明顯是折行后在第二行多余的部分顯示省略號(hào),那由于內(nèi)容不固定字?jǐn)?shù)不固定,所以需要?jiǎng)討B(tài)設(shè)置,那么就應(yīng)用到了上述的多行顯示省略號(hào)的方法
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)中間顯示省略號(hào)方法一</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: '......';
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)中間顯示省略號(hào)方法二</h3>
<div class="box4">
<a href="#">
<span class="span1" title="我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容">我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容</span>
<span class="span2" title="我是右側(cè)內(nèi)容我是右側(cè)內(nèi)容"></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;
}
/* 優(yōu)化兩個(gè)span中間的間距 */
.box4 {
text-align: justify;
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
綜上所述:以上四種方案是目前頁(yè)面中應(yīng)用較多的實(shí)現(xiàn)省略號(hào)的方法,僅代表個(gè)人觀點(diǎn),如您有更優(yōu)的方法歡迎聯(lián)系我們。
載鏈接:https://segmentfault.com/a/1190000020920000
在我們的日常開(kāi)發(fā)工作中,文本溢出截?cái)嗍÷允呛艹R?jiàn)的一種需考慮的業(yè)務(wù)場(chǎng)景細(xì)節(jié)。看上去 “稀松平常” ,但在實(shí)現(xiàn)上卻有不同的區(qū)分,是單行截?cái)噙€是多行截?cái)啵慷嘈械慕財(cái)嗯袛嗍腔谛袛?shù)還是基于高度?這些問(wèn)題之下,都有哪些實(shí)現(xiàn)方案?他們之間的差異性和場(chǎng)景適應(yīng)性又是如何?凡事就怕較真,較真必有成長(zhǎng)。本文試圖通過(guò)編碼實(shí)踐,給出一些答案。
核心 CSS 語(yǔ)句
優(yōu)點(diǎn)
短板
適用場(chǎng)景
Demo
<style>
.demo {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
<body>
<div class="demo">這是一段很長(zhǎng)的文本</div>
</body>
示例圖片
核心 CSS 語(yǔ)句
優(yōu)點(diǎn)
短板
適用場(chǎng)景
Demo
<style>
.demo {
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
</style>
<body>
<div class='demo'>這是一段很長(zhǎng)的文本</div>
</body>
示例圖片
優(yōu)點(diǎn)
短板
適用場(chǎng)景
Demo
當(dāng)前僅適用于文本為中文,若文本中有英文,可自行修改
<script type="text/javascript">
const text = '這是一段很長(zhǎng)的文本';
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);
// 所計(jì)算的strNum為元素內(nèi)部一行可容納的字?jǐn)?shù)(不區(qū)分中英文)
const strNum = Math.floor(lineWidth / +baseFontSize.slice(0, -2));
let content = '';
// 多行可容納總字?jǐn)?shù)
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 語(yǔ)句
優(yōu)點(diǎn)
短板
適用場(chǎng)景
Demo
<style>
.demo {
overflow: hidden;
max-height: 40px;
line-height: 20px;
}
</style>
<body>
<div class='demo'>這是一段很長(zhǎng)的文本</div>
</body>
示例圖片
核心 CSS 語(yǔ)句
優(yōu)點(diǎn)
短板
適用場(chǎng)景
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'>這是一段很長(zhǎng)的文本</div>
</body>
示例圖片
核心 CSS 語(yǔ)句
優(yōu)點(diǎn)
短板
適用場(chǎng)景
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'>這是一段很長(zhǎng)的文本</div>
</body>
示例圖片
原理講解
有 A、B、C 三個(gè)盒子,A 左浮動(dòng),B、C 右浮動(dòng)。設(shè)置 A 盒子的高度與 B 盒子高度(或最大高度)要保持一致
凡重復(fù)的,讓它單一;凡復(fù)雜的,讓它簡(jiǎn)單。
每次都要搞一坨代碼,太麻煩。這時(shí)候你需要考慮將文本截?cái)嗟哪芰Γ庋b成一個(gè)可隨時(shí)調(diào)用的自定義容器組件。市面上很多 UI 組件庫(kù),都提供了同類(lèi)組件的封裝,如基于 Vue 的 ViewUI Pro,或面向小程序提供組件化解決能力的 MinUI。
轉(zhuǎn)載鏈接:https://segmentfault.com/a/1190000020920000
時(shí)候需要控制下文字?jǐn)?shù),不然就會(huì)溢出,頁(yè)面就會(huì)變樣不美觀。這時(shí)我們就可以用css控制字?jǐn)?shù),超出部分顯示省略號(hào)。可以不換行,超出部分顯示省略號(hào),也可以可以換行,多行,超出部分顯示省略號(hào)。
1.不換行,超出部分顯示省略號(hào)
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</title> <style type="text/css"> *{margin:0;padding:0;} body{width:1000px;margin:100px auto;} .box{ width:260px; /*超出部分就隱藏*/ overflow:hidden; /*不換行設(shè)定*/ white-space:nowrap; /*超出部分的文字顯示省略號(hào)*/ text-overflow:ellipsis; } </style> </head> <body> <div class="box">用css控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> </body> </html>
效果圖如下:
2.可以換行,多行,超出部分顯示省略號(hào)
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>可以換行,多行,超出部分顯示省略號(hào)</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控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> <div class="box">2.用css控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> </body> </html>
效果圖如下:
注:此方法適用于WebKit瀏覽器及移動(dòng)端。
除注明外的文章,均為來(lái)源:湯久生博客,轉(zhuǎn)載請(qǐng)保留本文地址!
原文地址:http://tangjiusheng.com/divcss/169.html
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。