There is a block with text with a fixed height and width, and the value of overflow: hidden; that is, if the text does not fit into the area it is clipped, you need to add an ellipsis to the end of the last visible line. text-overflow works only for single-line text.
- oneBe kind, attach your questions to the question. - Mr. Brightside
5 answers
For webkit browsers, you can use this solution.
div { border: 1px solid #000; padding: 5px; display: block; display: -webkit-box; max-width: 400px; font-size: 20px; -webkit-line-clamp: 4; /* ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΡΠΎΠΊ */ -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; } <div> ΠΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΠ΅ (β¦) β Π·Π½Π°ΠΊ ΠΏΡΠ΅ΠΏΠΈΠ½Π°Π½ΠΈΡ Π² Π²ΠΈΠ΄Π΅ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ
(Π² ΡΡΡΡΠΊΠΎΠΌ ΡΠ·ΡΠΊΠ΅ ΡΡΡΡ
) ΠΏΠΎΡΡΠ°Π²Π»Π΅Π½Π½ΡΡ
ΡΡΠ΄ΠΎΠΌ ΡΠΎΡΠ΅ΠΊ. ΠΠ΅ ΠΏΡΡΠ°ΡΡ Ρ ΡΡΠΎΠ΅ΡΠΎΡΠΈΠ΅ΠΌ. Π’ΡΠΎΠ΅ΡΠΎΡΠΈΠ΅ (β΄) β ΡΠ°Π·Π΄Π΅Π»ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ Π·Π½Π°ΠΊ ΠΌΠ΅ΠΆΠ΄Ρ ΡΠ»ΠΎΠ²Π°ΠΌΠΈ ΠΈΠ»ΠΈ ΠΏΡΠ΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΡΠΌΠΈ, ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π²ΡΠΈΠΉΡΡ Π² Π΄ΡΠ΅Π²Π½ΠΈΡ
ΡΠ»Π°Π²ΡΠ½ΡΠΊΠΈΡ
ΠΈ Π³ΡΡΠ·ΠΈΠ½ΡΠΊΠΈΡ
ΡΡΠΊΠΎΠΏΠΈΡΡΡ
</div> Cross-browser solution through pseudo-selectors. Requires text-align: justify; and settings for .block-with-text:after the same color as the background:
/* ΡΡΠΈΠ»ΠΈ Π΄Π»Ρ ΠΌΠ½ΠΎΡΠΎΡΠΈΡ 'β¦' */ .block-with-text { /* ΡΠΏΡΡΡΠ°ΡΡ ΡΠ΅ΠΊΡΡ Π΅ΡΠ»ΠΈ Ρ Π±ΠΎΠ»Π΅Π΅ N ΡΡΡΠΎΠΊ */ overflow: hidden; /* Π΄Π»Ρ ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠΈ 'β¦' Π² Π°Π±ΡΠΎΠ»ΡΡΠ½ΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ */ position: relative; /* ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ ΡΡΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π΄Π»Ρ ΡΠ°ΡΡΡΡΠ° Π²ΡΡΠΎΡΡ Π±Π»ΠΎΠΊΠ° */ line-height: 1.2em; /* max-height = line-height (1.2) * ΡΠΈΡΠ»ΠΎ ΡΡΡΠΎΠΊ (3) */ max-height: 3.6em; /* ΠΈΡΠΏΡΠ°Π²Π»ΠΈΠ»Π΅Π½ΠΈΠ΅ ΠΏΡΠΎΠ±Π»Π΅ΠΌΡ ΠΊΠΎΠ³Π΄Π° ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅Π΅ ΡΠ»ΠΎΠ²ΠΎ Π½Π΅ ΡΠΎΠ΅Π΄ΠΈΠ½ΡΠ΅ΡΡΡ Ρ ΠΏΡΠ°Π²ΠΎΠΉ ΡΡΠΎΡΠΎΠ½Ρ */ text-align: justify; /* ΠΌΠ΅ΡΡΠΎ Π΄Π»Ρ ΠΌΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΡ 'β¦' */ padding-right: 1em; } /* ΡΠΎΠ·Π΄Π°ΡΠΌ ΠΌΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΠ΅ β¦ */ .block-with-text:before { /* ΠΌΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΠ΅ Π² ΠΊΠΎΠ½ΡΠ΅ */ content: 'β¦'; /* Π°Π±ΡΠΎΠ»ΡΡΠ½ΠΎΠ΅ ΠΏΠΎΠ·ΠΈΡΠΈΠΎΠ½ΠΈΡΠΎΠ²Π°Π½ΠΈΠ΅ */ position: absolute; /* ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ Π² ΠΏΡΠ°Π²ΡΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ ΡΠ³ΠΎΠ» Π±Π»ΠΎΠΊΠ° */ right: 0; bottom: 0; } /* ΡΠΏΡΡΡΠ°ΡΡ ΠΌΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΠ΅ β¦ Π΅ΡΠ»ΠΈ Ρ Π½Π°Ρ Π΅ΡΡΡ ΡΠ΅ΠΊΡΡ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΌΠ΅Π½ΡΡΠ΅ ΠΈΠ»ΠΈ ΡΠ°Π²Π΅Π½ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²Ρ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ ΡΡΡΠΎΠΊ */ .block-with-text:after { content: ""; position: absolute; /* ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ ΠΏΠΎΠ·ΠΈΡΠΈΡ Π² ΠΏΡΠ°Π²ΡΠΉ Π½ΠΈΠΆΠ½ΠΈΠΉ ΡΠ³ΠΎΠ» Π±Π»ΠΎΠΊΠ° */ right: 0; /* ΡΡΡΠ°Π½ΠΎΠ²ΠΈΡΡ width ΠΈ height */ width: 1em; height: 1em; margin-top: 0.2em; /* ΡΠ²Π΅Ρ ΡΠΎΠ½Π° ΠΏΠ΅ΡΠ΅Π΄ Π±Π»ΠΎΠΊΠΎΠΌ */ background: white; } <div class="block-with-text"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </div> white-space: nowrap; /* ΠΠ°ΠΏΡΠ΅ΡΠ°Π΅ΠΌ ΠΏΠ΅ΡΠ΅Π½ΠΎΡ ΡΡΡΠΎΠΊ */ overflow: hidden; /* ΠΠ±ΡΠ΅Π·Π°Π΅ΠΌ Π²ΡΠ΅, ΡΡΠΎ Π½Π΅ ΠΏΠΎΠΌΠ΅ΡΠ°Π΅ΡΡΡ Π² ΠΎΠ±Π»Π°ΡΡΡ */ background: #fc0; /* Π¦Π²Π΅Ρ ΡΠΎΠ½Π° */ padding: 5px; /* ΠΠΎΠ»Ρ Π²ΠΎΠΊΡΡΠ³ ΡΠ΅ΠΊΡΡΠ° */ text-overflow: ellipsis; /* ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ ΠΌΠ½ΠΎΠ³ΠΎΡΠΎΡΠΈΠ΅ */ If not needed The background color can be removed and if needed this is -o-text-overflow
In theory, I think you can wrap each word in a span with the help of JS and look at the coordinates (getBoundingClientRect or offset in jQuery), whether it goes beyond the wrapper ... Maybe there is an easier way ... But in general, hereβs the article - https: / /css-tricks.com/line-clampin/
Empirically, for example, set the block size in characters
//js str = $('.my_div').html(); str = str.substr(0,127) + '...';//ΠΊ ΠΏΡΠΈΠΌΠ΅ΡΡ, Π΅ΡΠ»ΠΈ Π²Π΅Π»ΠΈΡΠΈΠ½Π° Π±Π»ΠΎΠΊΠ° 130 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ². $('.my_div').html(str); Of the minuses: when kerning various characters, the symbolic value of a block may conditionally change, but not greatly affecting the final result. Illustrative example (5 characters in each line):
VVVVV
AVAVA
From the pros: the simplicity of the solution, the only question arises in counting characters (for example, fill the block with "O" characters and copy them before overflow into some notepad ++, see the number there)