Home Web Front-end CSS Tutorial 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)

Oct 20, 2018 pm 02:36 PM

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
-->
Copy after login

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>
Copy after login
  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>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles