Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
伙伴們,對于省略號呢,咱們前端攻城獅的實現方法可就是多種多樣了,那接下來呢我就給你羅列一下如果用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小時內與您取得聯系。