時候需要控制下文字數,不然就會溢出,頁面就會變樣不美觀。這時我們就可以用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
例
下面這個 HTML 表單帶有最大長度分別是 85 和 55 個字符的兩個輸入字段:
<form action="ybb.php" method="get">
<p>Name: <input type="text" name="fullname" maxlength="85" /></p>
<p>Email: <input type="text" name="email" maxlength="55" /></p>
<input type="submit" value="Submit" />
</form>
親自試一試
maxlength 屬性規定輸入字段的最大長度,以字符個數計。
maxlength 屬性與 <input type="text"> 或 <input type="password"> 配合使用。
<input maxlength="value">
值描述characters輸入字段中允許的最大字符數。
另外還有
、將富文本html內容轉換為純文本
formatrichtext = (richtext, len = 0) => {
let content = richtext.replace(/<.+?>/g, '');
content = content.replace(/ /ig, ''); /* 去除 */
content = content.replace(/\s/ig, ''); /* 去除空格 */
return content;
}
2、限制展示的文本長度
*請認真填寫需求信息,我們會在24小時內與您取得聯系。