【问题标题】:Wrap a text within only two lines inside div在 div 内仅在两行内换行文本
【发布时间】:2012-08-12 22:38:12
【问题描述】:

我想在特定宽度的 div 内仅两行内换行文本。如果文本超出两行的长度,那么我想显示省略号。 有没有办法使用 CSS 做到这一点?

例如

Sample text showing wrapping
of text in only two line...

【问题讨论】:

    标签: html css


    【解决方案1】:

    以下对我有用:

    CSS:

    .longWordBreakParent {
      display: flex;
    }
    .longWordBreak {
      word-break: break-all;
    }
    

    HTML:

    <div className="longWordBreakParent">
        <span className='longWordBreak'>someverylongtextthatcannotbefitinoneline</span>
    </div>
    

    【讨论】:

      【解决方案2】:

      仅 CSS

          overflow: hidden;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
      

      【讨论】:

      【解决方案3】:

      在所有这些示例中,假设我们有:

      #someDiv {
        width: 250px;
        overflow: hidden;
      }
      

      【讨论】:

      • 如何确保只显示 2 行?它只处理 div 的宽度。
      【解决方案4】:

      @Asiddeen bn Muhammad 的解决方案对我的 css 进行了一些修改

          .text {
       line-height: 1.5;
        height: 6em; 
      white-space: normal;
      overflow: hidden;
      text-overflow: ellipsis;
      display: block;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
       }
      

      【讨论】:

        【解决方案5】:

        Webkit 的纯 CSS 解决方案

        // Only for DEMO
        $(function() {
        
          $('#toggleWidth').on('click', function(e) {
        
            $('.multiLineLabel').toggleClass('maxWidth');
        
          });
        
        })
        .multiLineLabel {
          display: inline-block;
          box-sizing: border-box;
          white-space: pre-line;
          word-wrap: break-word;
        }
        
        .multiLineLabel .textMaxLine {
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
          overflow: hidden;
        }
        
        
        /* Only for DEMO */
        
        .multiLineLabel.maxWidth {
          width: 100px;
        }
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="multiLineLabel">
          <span class="textMaxLine">This text is going to wrap automatically in 2 lines in case the width of the element is not sufficiently wide.</span>
        </div>
        <br/>
        <button id="toggleWidth">Toggle Width</button>

        【讨论】:

        • 这是最佳解决方案,必须标记为答案。谢谢。
        • 谢谢,我真的在找这个!
        【解决方案6】:

        通常单行截断非常简单

        .truncate-text {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
        

        两行截断有点棘手,但可以用 css 来完成,这个例子在 sass 中。

        @mixin multiLineEllipsis($lineHeight: 1.2rem, $lineCount: 2, $bgColor: white, $padding-right: 0.3125rem, $width: 1rem, $ellipsis-right: 0) {
          overflow: hidden; /* hide text if it is more than $lineCount lines  */
          position: relative; /* for set '...' in absolute position */
          line-height: $lineHeight; /* use this value to count block height */
          max-height: $lineHeight * $lineCount; /* max-height = line-height * lines max number */
          padding-right: $padding-right; /* place for '...' */
          white-space: normal; /* overwrite any white-space styles */
          word-break: break-all; /* will break each letter in word */
          text-overflow: ellipsis; /* show ellipsis if text is broken */
        
          &::before {
            content: '...'; /* create the '...'' points in the end */
            position: absolute;
            right: $ellipsis-right;
            bottom: 0;
          }
        
          &::after {
            content: ''; /* hide '...'' if we have text, which is less than or equal to max lines and add $bgColor */
            position: absolute;
            right: 0;
            width: $width;
            height: 1rem * $lineCount;
            margin-top: 0.2rem;
            background: $bgColor; /* because we are cutting off the diff we need to add the color back. */
          }
        }
        

        【讨论】:

          【解决方案7】:

          试试这样的:http://jsfiddle.net/6jdj3pcL/1/

          <p>Here is a paragraph with a lot of text ...</p>
          
          p {
              line-height: 1.5em;
              height: 3em;
              overflow: hidden;
              width: 300px;
          }
          
          p::before {
             content: '...';
             float: right;
             margin-top: 1.5em;
          }
          

          【讨论】:

          • 我觉得这个解决方案并不理想,因为即使文本没有溢出或者文本适合一行,它也会添加省略号。
          【解决方案8】:

          另一个简单快捷的解决方案

          .giveMeEllipsis {
             overflow: hidden;
             text-overflow: ellipsis;
             display: -webkit-box;
             -webkit-box-orient: vertical;
             -webkit-line-clamp: N; /* number of lines to show */
             line-height: X;        /* fallback */
             max-height: X*N;       /* fallback */
          }
          

          原问答参考here

          【讨论】:

          • 惊人的解决方案!我还没有完全测试过,但第一次尝试,效果很好
          • @vinesh 这个解决方案着火了! ?
          • 似乎box-orientdeprecated or obsolete
          • 有效!在 flexbox container 中的 mat-card-content 中对此进行了测试
          • 对于 Firefox(和 firefox Android)box-orient 可以通过设置 layout.css.prefixes.webkit = true 并将 CSS 更改为 -moz-webkit-box 来启用
          【解决方案9】:

          我见过的最好的只有 CSS 和响应式的来自 Mobify Developer Blog - CSS Ellipsis: How to Manage Multi-Line Ellipsis in Pure CSS

          JS Fiddle Example

          CSS:

          html, body, p { margin: 0; padding: 0; font-family: sans-serif;}
          
          .ellipsis {
              overflow: hidden;
              height: 200px;
              line-height: 25px;
              margin: 20px;
              border: 5px solid #AAA; }
          
          .ellipsis:before {
              content:"";
              float: left;
              width: 5px; height: 200px; }
          
          .ellipsis > *:first-child {
              float: right;
              width: 100%;
              margin-left: -5px; }        
          
          .ellipsis:after {
              content: "\02026";  
          
              box-sizing: content-box;
              -webkit-box-sizing: content-box;
              -moz-box-sizing: content-box;
              float: right; position: relative;
              top: -25px; left: 100%; 
              width: 3em; margin-left: -3em;
              padding-right: 5px;
          
              text-align: right;
          
              background: -webkit-gradient(linear, left top, right top,
                  from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
              background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);           
              background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
              background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
              background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); }
          

          HTML:

          <div class="ellipsis">
              <div class="blah">
                  <p>Call me Ishmael. Some years ago &ndash; never mind how long precisely &ndash; having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off &ndash; then, I account it high time to get to sea as soon as I can.</p>
              </div>
          </div>
          

          【讨论】:

          • 迄今为止我见过的最好的解决方案。您可能想减少小提琴中的高度(200px)变量,因为我的屏幕尺寸最初没有溢出。
          【解决方案10】:

          我相信纯 CSS 解决方案 text-overflow: ellipsis 仅适用于一行,因此您将无法走这条路:

          .yourdiv {
          
              line-height: 1.5em; /* Sets line height to 1.5 times text size */
              height: 3em; /* Sets the div height to 2x line-height (3 times text size) */
              width: 100%; /* Use whatever width you want */
              white-space: normal; /* Wrap lines of text */
              overflow: hidden; /* Hide text that goes beyond the boundaries of the div */
              text-overflow: ellipsis; /* Ellipses (cross-browser) */
              -o-text-overflow: ellipsis; /* Ellipses (cross-browser) */
          }
          

          你试过 http://tpgblog.com/threedots/ 的 jQuery 吗?

          【讨论】:

          • 正如我所提到的,将省略号与多行文本组合起来似乎不起作用,至少在 Chrome 中对我来说不起作用。
          • 这在我添加后解决了我当前的问题:display: block and min-height: 13px and max-height: 26px for setting height for a &lt;td&gt;
          【解决方案11】:

          如果设置元素的line-heightheight 并设置overflow:hidden;,则可以使用CSS 将输出限制为两行文本:

          #someDiv {
              line-height: 1.5em;
              height: 3em;       /* height is 2x line-height, so two lines will display */
              overflow: hidden;  /* prevents extra lines from being visible */
          }
          

          --- jsFiddle DEMO ---

          或者,您可以使用 CSS text-overflowwhite-space 属性添加省略号,但这似乎只适用于单行。

          #someDiv {
              line-height: 1.5em;
              height: 3em;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              width: 100%;
          }
          

          还有一个演示:

          --- jsFiddle DEMO ---

          实现多行文本和省略号似乎是 javascript 的领域。

          【讨论】:

          • 由于某种原因,我只看到一条线穿过:/
          • 第二个例子只有一行,而请求的解决方案需要两行。
          • 第三个例子对我不起作用,在 Chrome 和 Firefox 中测试过。
          • 在那个破碎的例子中white-space: nowrap; 打破了它,如果你把它注释掉它就可以了。
          • 2022 年有人改变了什么吗?我曾经这样做并且像魅力一样工作,但它似乎在我的浏览器中不再起作用。我在 Chrome 和 Firefox 中对其进行了测试,但没有显示省略号。即使在 jsFiddle 中。请参阅@vinesh stackoverflow.com/a/32585024/5830500 的其他答案。
          【解决方案12】:

          http://jsfiddle.net/SWcCt/

          只需设置line-height 的一半height

          line-height:20px;
          height:40px;
          

          当然,为了使text-overflow: ellipsis 工作,您还需要:

          overflow:hidden;
          white-space: pre;
          

          【讨论】:

          • 此解决方案不灵活,需要在源文本中手动换行。请注意jsfiddle.net/SWcCt/282 每个框只包含一行文本。所需的解决方案看起来像jsfiddle.net/SWcCt/283 的第二个框,除了第二行末尾有一个省略号。
          • @JoshuaCoady 好点,但text-overflow: ellipsis 仅适用于溢出行框的内联框。如果没有white-space: pre,他们只会转到下一行框。然后需要手动换行。我认为没有完美的解决方案。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-08
          • 1970-01-01
          • 2023-03-10
          • 2012-02-01
          • 2022-11-19
          • 1970-01-01
          相关资源
          最近更新 更多