I have a div with height of 192px. I want to truncate text within div and want to show ...
in the end. now due to large text, button is clipping as shown in the snapshot.
This happens when I add html tags in it.
Can anyone help?
With line-clamp text can be truncated after multiple lines, whats even more interesting is you can truncate it by specifying the line number where you want to truncate it. eg: -webkit-line-clamp: 3; will truncate start truncating the text from the third line.
We can solve this problem by using CSS overflow property. overflow: hidden; hidden – The overflow is clipped, and the rest of the content will be invisible.
text_truncate = function(str, length, ending) { if (length == null) { length = 100; } if (ending == null) { ending = '...'; } if (str. length > length) { return str. substring(0, length - ending. length) + ending; } else { return str; } }; console.
Method 1: Using the offsetHeight property: The offsetHeight property of an element is a read-only property and used to return the height of an element in pixels. It includes any borders or padding of the element. This property can be used to find the height of the <div> element.
Try the following CSS:
text-overflow: ellipsis;
overflow: hidden;
whitespace: no-wrap;
This only works for single lines. For multiple lines you need JavaScript.
This question is tagged with javascript, so here's the missing answer.
You could iterate over each character or word (as in this example), while you check that the height is lower than your desired height. On each truthy step, you could overwrite the element content with its text, but without the last word/character.
In converte this case I converted the string in an array and pop
it on each iteration. This removes the last part of our text, and makes sure that the loop doesn't go infiniteeeee...
/**
* Truncates the text of an element depending its height.
*
* @param {Element} element
* @param {Number} height
*/
function truncateByHeight(element, height) {
var textContent = typeof element.textContent === 'undefined' ? 'innerText' : 'textContent';
var parts = element[textContent].split(' ');
while (parts.pop() && element.clientHeight > height) {
element[textContent] = parts.join(' ');
}
}
var element = document.querySelector('.box');
truncateByHeight(element, 120);
<div class="box">Cornua naturae fulgura uno coegit quisquis ad margine? Pluvialibus umentia vultus nulli nunc pendebat speciem emicuit! Margine tonitrua caecoque iapeto. Origine levius nam. Silvas valles temperiemque forma? Ignotas tegit. Hunc ligavit: summaque freta illas invasit deerat proximus. Caelo calidis securae mentes pronaque traxit.
Caligine omnia gentes. Posset aere certis eurus titan verba unus homini ora. Sed volucres. Campos effervescere flamina illi pondus umor. Cinxit obstabatque secrevit. Ligavit: natus aberant. Indigestaque regio rapidisque carmen coegit erat discordia liquidas. Ripis nix horrifer terrae dei tepescunt ad vos regio.
Nabataeaque fronde pluviaque vos terra tellure flexi. Neu habendum poena locoque ne. Dedit locoque nunc nebulas. Mentisque liquidum summaque porrexerat cepit. Litem mare. Surgere adhuc ipsa. Orbem hanc volucres iapeto habentem. Dissociata otia inminet nubibus passim erant iners. Semina praecipites reparabat spectent fuerat.</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With