Home  >  Article  >  Web Front-end  >  CSS, j's single-line, multi-line text overflow displays ellipsis

CSS, j's single-line, multi-line text overflow displays ellipsis

WBOY
WBOYOriginal
2016-09-13 08:45:582127browse

In the project, due to too much actual description text, the vertical length of the initial page is too long, which also reduces the utilization of the remaining information; therefore, when there is too much text, it is necessary to initialize the number of lines.

1. CSS single line text overflows and ellipses are displayed

<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
    我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出,我要溢出。
</span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

2. CSS multi-line text overflows and displays ellipses

<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="overflow:hidden;display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:3;"</span><span style="color: #0000ff;">></span><span style="color: #000000;">
    我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本,我要溢出,多行文本。
</span><span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

The above two are suitable for situations where there is only text in the label, and sometimes we will encounter such a situation

<span style="color: #0000ff;"><</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>我是内容<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>我是内容<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>我是内容<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>我是内容<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>

3. The third way is to solve the problems encountered in the above situation

I have seen many examples on the Internet, and each has its own advantages. The one I use is a fixed line-height. The line breaks are also at a fixed height for each line, such as: line-height:20px;, and a blank line is added each time. Also let the blank line be 20px, so that we can use multiples of 20 to reasonably display the number of lines and content to be displayed

<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="text"</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        你不想要改变世界,但是你想要看日出?想要体验下传说中的晨跑?想在晨跑中偶遇男神女神?想要看下早上5点图书馆或训练场是否有人?想呼吸下还没被汽车尾气过度渲染的空气?想给自己给爱人做个早餐?你得早起呀!昨晚事情没做完?考研单词还差几页?赶飞机、赶火车?你得早起呀!你不想上班也得早起请假不是嘛~
    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span><span style="color: #000000;">
        欢迎大家参加早起的鸟儿有虫吃-21天早起计划。一日之计在于晨,在沐浴晨光的过程中,我们祝愿大家开始美好的一天!
    </span><span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">class</span><span style="color: #0000ff;">="points"</span><span style="color: #0000ff;">></span>...<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #800000;">.text</span>{<span style="color: #ff0000;">color</span>:<span style="color: #0000ff;">#707070</span>;<span style="color: #ff0000;">padding</span>:<span style="color: #0000ff;">0 12px</span>;<span style="color: #ff0000;">position</span>:<span style="color: #0000ff;">relative</span>;<span style="color: #ff0000;">line-height</span>:<span style="color: #0000ff;">20px</span>;<span style="color: #ff0000;">overflow</span>:<span style="color: #0000ff;">hidden</span>;<span style="color: #ff0000;">
margin-bottom</span>:<span style="color: #0000ff;">12px</span>;<span style="color: #ff0000;">max-height</span>:<span style="color: #0000ff;">60px</span>;}<span style="color: #800000;">
.points</span>{<span style="color: #ff0000;">content</span>:<span style="color: #0000ff;">"..."</span>;<span style="color: #ff0000;">position</span>:<span style="color: #0000ff;">absolute</span>;<span style="color: #ff0000;">bottom</span>:<span style="color: #0000ff;">0</span>;<span style="color: #ff0000;">right</span>:<span style="color: #0000ff;">16px</span>;<span style="color: #ff0000;">padding-left</span>:<span style="color: #0000ff;">40px</span>;<span style="color: #ff0000;">font-size</span>:<span style="color: #0000ff;">18px</span>;<span style="color: #ff0000;">letter-spacing</span>:<span style="color: #0000ff;">3px</span>;<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> -webkit-linear-gradient(left, transparent, #fff 55%)</span>;<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> -o-linear-gradient(right, transparent, #fff 55%)</span>;<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> -moz-linear-gradient(right, transparent, #fff 55%)</span>;<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> linear-gradient(to right, transparent, #fff 55%)</span>;}<span style="color: #800000;">
.text p</span>{<span style="color: #ff0000;">margin</span>:<span style="color: #0000ff;">0</span>;}<span style="color: #800000;">
.text p+p</span>{<span style="color: #ff0000;">margin-top</span>:<span style="color: #0000ff;">20px</span>;}
<span style="color: #008000;">//</span><span style="color: #008000;"> 字体显示</span>
    <span style="color: #0000ff;">var</span> showBoo = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    $(</span>'.de-box .text').on('click',<span style="color: #0000ff;">function</span><span style="color: #000000;">(){
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(showBoo){
            console.log(</span>1<span style="color: #000000;">);
            $(</span>'.de-box .text').css('maxHeight','none'<span style="color: #000000;">);
            $(</span>'.de-box .points'<span style="color: #000000;">).hide();
            showBoo </span>= <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            $(</span>'.de-box .text').css('maxHeight','60px'<span style="color: #000000;">);
            $(</span>'.de-box .points'<span style="color: #000000;">).show();
            showBoo </span>= <span style="color: #0000ff;">true</span><span style="color: #000000;">;
        };
    });</span>

In this case, line breaks will appear and blank lines will be displayed. I will leave this problem for now. I am a little tired today

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn