整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

css控制字数,超出部分就显示省略号

时候需要控制下文字数,不然就会溢出,页面就会变样不美观。这时我们就可以用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

标签是默认是自动换行的,因此设置好宽度之后,能够较好的实现效果,但是最近的项目中发现,使用ajax加载数据之后,p标签内的内容没有换行,导致布局错乱,于是尝试着使用换行样式,虽然解决了问题,但是并没有发现本质原因,本质在于,我当时获取的数据是一长串的数字,浏览器应该是对数字和英文单词处理方式相近,不会截断。

先给出各种方式,再具体介绍每一个属性。

强制不换行

p { white-space:nowrap; }

自动换行

p { word-wrap:break-word; }

强制英文单词断行

p { word-break:break-all; }

*注意:设置强制将英文单词断行,需要将行内元素设置为块级元素。

超出显示省略号

p{text-overflow:ellipsis;overflow:hidden;}

white-space: normal|pre|nowrap|pre-wrap|pre-line|inherit;

white-space 属性设置如何处理元素内的空白

normal 默认。空白会被浏览器忽略。

pre 空白会被浏览器保留。其行为方式类似 HTML 中的 pre 标签。

nowrap 文本不会换行,文本会在在同一行上继续,直到遇到 br 标签为止。

pre-wrap 保留空白符序列,但是正常地进行换行。

pre-line 合并空白符序列,但是保留换行符。

inherit 规定应该从父元素继承 white-space 属性的值。

word-wrap: normal|break-word;

word-wrap 属性用来标明是否允许浏览器在单词内进行断句,这是为了防止当一个字符串太长而找不到它的自然断句点时产生溢出现象。

normal: 只在允许的断字点换行(浏览器保持默认处理)

break-word:在长单词或URL地址内部进行换行

word-break: normal|break-all|keep-all;

word-break 属性用来标明怎么样进行单词内的断句。

normal:使用浏览器默认的换行规则。

break-all:允许再单词内换行

keep-all:只能在半角空格或连字符处换行

举例看起区别:

<!doctype html>

<html lang="en">

<head>

<!--网站编码格式,UTF-8 国际编码,GBK或 gb2312 中文编码-->

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<meta name="Keywords" content="关键词一,关键词二">

<meta name="Description" content="网站描述内容">

<meta name="Author" content="Yvette Lau">

<title>Document</title>

<!--css js 文件的引入-->

<style>

.word{background:#E4FFE9;width:250px;margin:50px auto;padding:20px;font-family:"microsoft yahei";}

/* 强制不换行 */

.nowrap{white-space:nowrap;}

/* 允许单词内断句,首先会尝试挪到下一行,看看下一行的宽度够不够,

不够的话就进行单词内的断句 */

.breakword{word-wrap: break-word;}

/* 断句时,不会把长单词挪到下一行,而是直接进行单词内的断句 */

.breakAll{word-break:break-all;}

/* 超出部分显示省略号 */

.ellipsis{text-overflow:ellipsis;overflow:hidden;}

</style>

</head>

<body>

<div class = "word">

<p class = "nowrap">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfs</p>

<p class = "breakword">wordwrap:break-word;absavhsafhuafdfbjhfvsalguvfaihui</p>

<p class = "breakAll">wordwrap:break-word;absavhsafhuafdfbjhfvsalguvfaihuivf</p>

<p class = "normal">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfsa</p>

<p class = "ellipsis">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfsab</p>

</div>

</body>

</html>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

效果如下:

伙伴们,对于省略号呢,咱们前端攻城狮的实现方法可就是多种多样了,那接下来呢我就给你罗列一下如果用css书写这些特殊效果,来一起看看吧~~~

1.单行文本超出显示省略号

效果图:

实现代码:

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;
     }


注:此案例为京东首页的部分切图,此时的省略号可以用上述代码实现

2.多行显示省略号

效果图:

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;
    }

实际案例应用场景说明:

注:此时明显是折行后在第二行多余的部分显示省略号,那由于内容不固定字数不固定,所以需要动态设置,那么就应用到了上述的多行显示省略号的方法

3.内容中间显示省略号(方法一)

效果图:

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: '......';
       }

实际案例应用场景说明:

4.内容中间显示省略号(方法二)

效果图:

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;
      }

实际案例应用场景说明:

综上所述:以上四种方案是目前页面中应用较多的实现省略号的方法,仅代表个人观点,如您有更优的方法欢迎联系我们。