在div中仅两行内换行


85

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

例如

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

谢谢

Answers:


142

限制输出到两行文字是可能的CSS,如果设置line-heightheight元素,并集overflow:hidden;

#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演示-

另外,您可以使用CSStext-overflowwhite-space属性添加省略号,但这似乎仅适用于单行。

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

和演示:

--- jsFiddle演示-

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


11
由于某种原因,我只能看到一行内容:/
SearchForKnowledge 2014年

7
当请求的解决方案需要两行时,第二个示例只有一行。
goyote '16

第三个示例对我不起作用,在Chrome和Firefox中进行了测试。
奥利弗·洛顿

1
在那个破碎的示例中white-space: nowrap;,如果您注释掉它,它就会起作用。
Wilt

42

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

.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 */
}

原始问题和答案的参考在这里


3
惊人的解决方案!我尚未对其进行完整的测试,但是在初次尝试时,效果很好
Gambai

4
@vinesh这个解决方案着火了!🔥–
PhillipJacobs

似乎box-orient弃用或过时
Greg Herbowicz

作品!mat-card-contentflexbox container
faizanjehangir

17

我见过的最好的CSS仅是CSS,它来自Mobify开发人员博客-CSS Ellipsis:如何在纯CSS中管理多行省略号

JS小提琴示例

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>

到目前为止,我所见过的最好的解决方案。您可能要减少小提琴中的height(200px)变量,因为我的屏幕尺寸最初没有溢出文本。
Mike Fuchs

14

我相信纯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/ for jQuery?


正如我提到的,将椭圆与多行文本结合起来似乎不起作用,至少对我来说在Chrome中不起作用。
jackwanders 2012年

这个工作在我目前的问题我补充后:display: blockmin-height: 13pxmax-height: 26px设置高度为<td>
Underverse

8

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>


这是最佳解决方案,必须标记为答案。谢谢。
QMaster

6

仅CSS

    line-height: 1.5;
    white-space: normal;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;


4

通常,单行截断非常简单

.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. */
  }
}

2

参见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它们,他们将转到下一个行框。然后需要手动换行。我认为没有完美的解决方案。
Oriol


0

@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;
 }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.