div内の2行だけでテキストをラップします


87

特定の幅のdiv内の2行だけでテキストを折り返したい。テキストが2行の長さを超える場合は、楕円を表示します。CSSを使用してそれを行う方法はありますか?

例えば

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

ありがとう

回答:


147

要素のline-heightとを設定し、次のように設定するとheight、CSSで出力を2行のテキストに制限できます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プロパティを使用して省略記号を追加することもできますが、これは1行でのみ機能するようです。

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

そしてデモ:

--- jsFiddleデモ---

複数行のテキストと省略記号の両方を実現することは、javascriptの領域のようです。


11
なんらかの理由で1行しか通過していません:/
SearchForKnowledge 2014年

7
2番目の例では、要求されたソリューションに2行が必要な場合、1行しかありません。
goyote 2016

ChromeとFirefoxでテストした3番目の例は私には機能しません。
オリバーロートン

1
その壊れた例でwhite-space: nowrap;はそれを壊します、あなたがそれをコメントアウトするならば、それは働きます。
ウィルト

46

別のシンプルで迅速なソリューション

.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


動作します!内部でこれをテストmat-card-contentflexbox container
faizanjehangir

17

私が見た中で最も優れたCSSのみでレスポンシブなものは、Mobify Developer Blog-CSS Ellipsis:How to Manage Multi-Line Ellipsis in Pure 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>

私がこれまでに見た中で最高の解決策。私の画面サイズではテキストが最初にオーバーフローしなかったので、フィドルの高さ(200px)変数を減らしたいと思うかもしれません。
Mike Fuchs

14

CSSのみのソリューションtext-overflow: ellipsisは1行にのみ適用されると思います。そのため、次のルートに進むことはできません。

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

jQuery用にhttp://tpgblog.com/threedots/を試しましたか?


先に述べたように、楕円と複数行のテキストを組み合わせても、少なくともChromeでは機能しないようです。
jackwanders 2012

これは私が追加した後、私の現在の問題で働いていた:display: blockmin-height: 13pxmax-height: 26pxのために高さを設定する<td>
Underverse

9

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の

7

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

通常、1行の切り捨ては非常に単純です

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

2行の切り捨ては少し注意が必要ですが、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の各ボックスには、1行のテキストしか含まれていないことに注意してください。目的のソリューションは、2行目の末尾に省略記号が付いていることを除けば、jsfiddle.net / SWcCt / 283の2番目のボックスのようになります。
Joshua Coady 2016

@JoshuaCoady良い点ですがtext-overflow: ellipsis、ラインボックスをオーバーフローするインラインボックスでのみ機能します。white-space: pre彼らがいなければ、次のラインボックスに行くだけです。そして、手動の改行が必要です。完璧な解決策はないと思います。
オリオール2016


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;
 }
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.