Home  >  Article  >  Web Front-end  >  css forces line breaks and exceeds hiding to achieve single and multiple lines (code example)

css forces line breaks and exceeds hiding to achieve single and multiple lines (code example)

云罗郡主
云罗郡主forward
2018-10-20 14:36:232309browse

The content this article brings to you is about css forced line wrapping and beyond hiding to achieve single and multi-line (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

<!--
white-space: normal|pre|nowrap|pre-wrap|pre-line|inherit;
white-space:设置如何处理元素内的空白;
word-break: normal|break-all|keep-all;
word-break:用来标明怎么样进行单词内的断句
word-wrap: normal|break-word;
word-wrap:用来标明是否允许浏览器在单词内进行断句,这是为了防止当一个字符串太长而找不到它的自然断句点时产生溢出现象。
text-overflow:ellipsis; 让多出的内容以省略号...来表达。但是这个属性主要用于IE等浏览器,Opera浏览器用-o-text-overflow:ellipsis; 而Firefox浏览器没有这个功能,多出的内容只能隐藏起来。
display:-webkit-box; //将对象作为弹性伸缩盒子模型显示。
-webkit-box-orient:vertical; //从上到下垂直排列子元素(设置伸缩盒子的子元素排列方式)
-webkit-line-clamp:2; //这个属性不是css的规范属性,需要组合上面两个属性,表示显示的行数。
/* autoprefixer: off */ /* autoprefixer: on */解决-webkit-box-orient:vertical
-->

Popular explanation:

word-break:break-all; only works in English, using letters as the basis for line breaks
word-wrap:break-word; only works in English It works in English, using words as the basis for line breaks
white-space:pre-wrap; It only works in Chinese, it forces line breaks
white-space:nowrap; It works in all cases if it forces no line breaks
white-space :nowrap; overflow:hidden; text-overflow:ellipsis; no line breaks, the excess part is hidden and appears in the form of ellipses (supported by some browsers)

Case: There are comments in the case,

<!DOCTYPE html>

<html lang="en">



<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Document</title>

    <style>
        body {
            width: 100%;
            margin: 200px 20%;
        }
        .indexBox1 {
            width: 60%;
            height: 100px;
            font-size: 14px;
            color: #ffffff;
            display: flex;
            justify-content: space-around;
        }
          /* 单行 */
        .indexBox1 .box_text1 {
            width: 100px;
            height: 98px;
            background: gray;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            /* word-break: normal;
             word-wrap: none; */
        }
        /* 多行 */
        .indexBox1 .box_text4 {
            width: 100px;
            height: 98px;
            background: black;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            /* Firefox */
            display: -moz-box;
            /* autoprefixer: off */
            -moz-box-orient: vertical;
            /* autoprefixer: on */
            /* Safari、Opera 以及 Chrome */
            display: -webkit-box;
            /* autoprefixer: off */
            /*解决下面这个属性不显示*/
            -webkit-box-orient: vertical;
            /* autoprefixer: on */
            /*解决上面这个属性不显示*/
            /* 控制在第几行多出的内容省略 */
            -webkit-line-clamp: 5;

        }
        .indexBox1 .box_text5 {
            /* word-break: normal|break-all|keep-all; */
            word-break: break-all;
        }
        .indexBox1 .box_text6 {
            /* word-wrap: normal|break-word; */
            word-wrap: break-word;
        }
    </style>
</head>

<body>
    <!-- 单行 -->
    <p class="indexBox1">
        <p class="box_text1">
            我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃 我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃 我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃
        </p>
        <p class="box_text1">
            we are come from a world,we have us dream,wo never give up us deram;
        </p>
        <p class="box_text1">
            we are come from a world,we have us dream,wo never give up us deram;
        </p>
    </p>
    <!-- 多行 -->
    <p class="indexBox1">
        <p class="box_text4">
            我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃 我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃 我们来自同一个世界,我们都有自己的梦想,我们都不曾放弃
        </p>
        <p class="box_text4 box_text5">
            we are come from a world,we have us dream,wo never give up us deram;
        </p>
        <p class="box_text4 box_text6">
            we are come from a world,we have us dream,wo never give up us deram;
        </p>
    </p>
</body>

</html>
  1. -webkit-line-clamp is used to limit the number of lines of text displayed in a block element. In order to achieve this effect, it needs to be combined with other WebKit properties. Commonly combined properties:

  2. display:-webkit-box; must be combined to display the object as a flexible box model.

  3. -webkit-box-orient must be combined with the attribute to set or retrieve the arrangement of the child elements of the flex box object.

But in order to be compatible with the Firefox browser, it is implemented by combining css and js;

The following is the implementation process:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<style>
    .content {
        margin: 0 auto;
        width: 400px;
        line-height: 25px;
        margin-top: 100px;
        position: relative;
        height:auto;
        overflow: auto;
    }
    .contentHide{
        height: 50px;
        overflow:hidden;
    }
    .contentHide::after {
        content: "...";
        position: absolute;
        bottom: 0;
        right: 0;
        padding-left: 20px;
        /* background: -webkit-linear-gradient(left, transparent, #fff 55%);
        background: -o-linear-gradient(right, transparent, #fff 55%);
        background: -moz-linear-gradient(right, transparent, #fff 55%); */
        background: linear-gradient(to right, transparent, #fff 55%);
    }
</style>

<body>
    <p class="content">我们来自同一个世界我们都有自己的梦想,我们不曾放弃;我们不曾放弃;我们不曾放弃;我们不曾放弃;我们不曾放弃;我们不曾放弃</p>
    <p class="content">我们来自同一个世界我们都有自己的梦想,我们不曾放弃;我们不曾放弃</p>
    <p class="content">我们来自同一个世界我们都有自己的梦想,我们不曾放弃</p>
</body>
<script>
    let ct=document.querySelectorAll(".content");
    for(let i=0;i<ct.length;i++){  //通过高度判断是否要添加超出隐藏的class名
        if(ct[i].offsetHeight>50)
        {
            ct[i].classList.add(&#39;contentHide&#39;);
        }
    }
    
</script>
</html>

The above is A full introduction to css forced line breaks and beyond hiding to achieve single and multiple lines (code examples). If you want to know more about CSS video tutorial, please pay attention to the PHP Chinese website.


The above is the detailed content of css forces line breaks and exceeds hiding to achieve single and multiple lines (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete