div

2012-08-16 18 views
42

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

उदा।

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

धन्यवाद

उत्तर

75

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

--- jsFiddle DEMO ---

वैकल्पिक रूप से, आप सीएसएस text-overflow और white-space उचित का उपयोग कर सकते हैं इलिप्स जोड़ने के लिए संबंध है, लेकिन यह केवल एक पंक्ति के लिए काम करता प्रतीत होता है।

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

और एक डेमो:

--- jsFiddle DEMO ---

पाठ और अनेक बिंदुओं के दोनों कई पंक्तियों हासिल जावास्क्रिप्ट के दायरे प्रतीत होता है।

+8

के लिए समाधान मैं केवल देख एक पंक्ति किसी कारण से भर जा रहा:/ – SearchForKnowledge

+0

इस पुरानी पड़ चुकी है? – rclai

+0

दूसरा उदाहरण IE में काम नहीं करता है ... – Weggo

8

मेरा मानना ​​है कि सीएसएस-एकमात्र समाधान 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/ को आजमाया है?

+0

जैसा काम करता है, टेक्स्ट की कई पंक्तियों वाले इलिप्स को संयोजन में काम नहीं करता है, कम से कम मेरे लिए क्रोम में नहीं। – jackwanders

+0

मैंने अपनी वर्तमान समस्या में काम करने के बाद काम किया: 'प्रदर्शन: ब्लॉक' और 'न्यूनतम ऊंचाई: 13 पीएक्स' और' अधिकतम ऊंचाई: 26 पीएक्स' '' – Underverse

2

http://jsfiddle.net/SWcCt/ देखें।

बस एक line-heightheight के आधे सेट:

line-height:20px; 
height:40px; 
बेशक

, आदेश text-overflow: ellipsis काम आप भी जरूरत बनाने के लिए:

overflow:hidden; 
white-space: pre; 
+0

के लिए ऊंचाई निर्धारित करने के लिए यह समाधान लचीला नहीं है और मैन्युअल लाइन की आवश्यकता है स्रोत पाठ में तोड़ो। ध्यान दें कि http://jsfiddle.net/SWcCt/282/ प्रत्येक बॉक्स में केवल टेक्स्ट की एक पंक्ति होती है। वांछित समाधान दूसरी पंक्ति के अंत में एक इलिप्सिस को छोड़कर http://jsfiddle.net/SWcCt/283/ के दूसरे बॉक्स की तरह दिखेगा। –

+0

@ जोशुआकोडी अच्छा बिंदु, लेकिन 'टेक्स्ट ओवरफ्लो: इलिप्सिस' केवल इनलाइन बॉक्स के लिए काम करता है जो लाइन बॉक्स को ओवरफ्लो करते हैं। 'व्हाइट-स्पेस' के बिना: प्री 'वे सिर्फ अगले लाइन बॉक्स में जाएंगे। और फिर एक मैनुअल लाइन ब्रेक की आवश्यकता है। मुझे नहीं लगता कि एक आदर्श समाधान है। – Oriol

11

सबसे अच्छा एक मैं जो देखा है सीएसएस है केवल और उत्तरदायी Mobify Developer Blog - CSS Ellipsis: How to Manage Multi-Line Ellipsis in Pure CSS से आता है:

JS Fiddle Example

सीएसएस:

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> 
+0

अब तक का सबसे अच्छा समाधान मैंने देखा है। आप मेरी स्क्रीन आकार के लिए ऊंचाई में (200px) चर को कम करना चाहते हैं, पाठ प्रारंभ में ओवरफ़्लो नहीं हुआ था। –

6

एक और सरल और त्वरित समाधान

.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

+1

सबसे अच्छा समाधान यहाँ है! –

2

कुछ lik प्रयास करें ई इस: 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; 
} 
1

आमतौर पर एक पंक्ति truncate काफी सरल

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

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

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

सीएसएस केवल वेबकिट

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