Div के अंदर केवल दो लाइनों के भीतर एक पाठ लपेटें


85

मैं विशिष्ट चौड़ाई के div के अंदर केवल दो पंक्तियों के भीतर एक पाठ लपेटना चाहता हूं। यदि पाठ दो रेखाओं की लंबाई से आगे जाता है तो मैं एलिप्स दिखाना चाहता हूं। वहाँ सीएसएस का उपयोग करने का एक तरीका है?

जैसे

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

धन्यवाद

जवाबों:


142

पाठ की दो पंक्तियाँ करने के लिए उत्पादन को सीमित करता है, तो आप सेट, सीएसएस के साथ संभव है line-heightऔर heightतत्व की, और सेट 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 */
}

--- jsField DEMO ---

वैकल्पिक रूप से, आप ellipses को जोड़ने के लिए CSS text-overflowऔर white-spaceगुणों का उपयोग कर सकते हैं , लेकिन यह केवल एकल पंक्ति के लिए काम करता है।

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

और एक डेमो:

--- jsField DEMO ---

पाठ और दीर्घवृत्त दोनों की कई पंक्तियों को प्राप्त करना जावास्क्रिप्ट का क्षेत्र प्रतीत होता है।


11
मैं केवल एक ही लाइन को किसी कारण से देखता हूं: /
SearchForKnowledge

7
दूसरे उदाहरण में केवल एक पंक्ति है, जब अनुरोधित समाधान के लिए दो की आवश्यकता होती है।
गत

तीसरा उदाहरण मेरे लिए काम नहीं करता है, क्रोम और फ़ायरफ़ॉक्स में परीक्षण किया गया है।
ओलिवर लॉर्टन

1
उस टूटे हुए उदाहरण में यह टूट white-space: nowrap;जाता है, यदि आप इसे टिप्पणी करते हैं तो यह काम करता है।
विल्ट

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
अद्भुत समाधान! मैंने इसका पूरी तरह से परीक्षण नहीं किया है, लेकिन पहली कोशिश में, यह बहुत अच्छी तरह से काम करता है
गैंबाई

4
@vinesh यह समाधान आग पर है! 🔥
फिलिप जेकॉब्स


काम करता है! एक के अंदर इस परीक्षण किया गया mat-card-contentएक मेंflexbox container
faizanjehangir

17

मैंने जो सबसे अच्छा देखा है वह CSS ही है और उत्तरदायी Mobify Developer Blog - CSS Ellipsis से आता है : Pure CSS में Multi-Line Ellipsis को कैसे प्रबंधित करें :

जेएस फिडेल उदाहरण

सीएसएस:

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); }

एचटीएमएल:

<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) चर को कम करना चाह सकते हैं, मेरी स्क्रीन के आकार के लिए पाठ शुरू में अतिप्रवाह नहीं हुआ था।
माइक फूक

14

मेरा मानना ​​है कि CSS-only समाधान केवल 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) */
}

क्या आपने jQuery के लिए http://tpgblog.com/threedots/ की कोशिश की है ?


जैसा कि मैंने उल्लेख किया है, पाठ की कई पंक्तियों के साथ दीर्घवृत्त का संयोजन काम करने के लिए प्रकट नहीं होता है, कम से कम मेरे लिए क्रोम में नहीं।
जैकवंडर्स

यह मेरे वर्तमान समस्या में काम करने के बाद मैं कहा: display: blockऔर min-height: 13pxऔर max-height: 26pxएक के लिए ऊंचाई की स्थापना के लिए<td>
Underverse

8

वेबकिट के लिए सीएसएस केवल समाधान

// 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



4

आमतौर पर एक-लाइन ट्रंकट काफी सरल होता है

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

दो लाइन ट्रंकट थोड़ा अधिक मुश्किल है, लेकिन यह सीएसएस के साथ किया जा सकता है यह उदाहरण सास में है।

@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 के 2 बॉक्स की तरह दिखेगा, सिवाय 2 पंक्ति के अंत में एक दीर्घवृत्त के साथ।
जोशुआ कोएडी

@ जोशुआकोडी अच्छा बिंदु, लेकिन text-overflow: ellipsisकेवल इनलाइन बॉक्स के लिए काम करता है जो लाइन बॉक्स को ओवरफ्लो करता है। बिना white-space: preवे बस अगली पंक्ति के डिब्बे में जाते। और फिर एक मैनुअल लाइन ब्रेक की जरूरत है। मुझे नहीं लगता कि एक सही समाधान है।
ओरिओल

1

दो लाइनों में लपेटने के लिए नीचे दिए गए लिंक का उपयोग करें लिंक की जाँच करें

यदि सामग्री बहुत अधिक है तो HTML टैग में दीर्घवृत्त (...) डालें

नीचे दिए गए jquery की जरूरत है

http://www.jeremymartin.name/projects.php?project=jTruncate


0

@ असिडीन bn मुहम्मद के समाधान ने मेरे लिए 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;
 }
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.