admin管理员组文章数量:1207780
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?
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?
Share Improve this question edited Aug 15, 2012 at 7:15 PaperThick 2,7994 gold badges25 silver badges42 bronze badges asked Aug 15, 2012 at 7:05 Zain ShaikhZain Shaikh 6,0436 gold badges44 silver badges66 bronze badges 06 Answers
Reset to default 8Try 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>
Use overflow: hidden
... I can get more specific if you post more code. – j-man86 just now edit
To hide the text, there is simple solution, add overflow:hidden css property in div like follow
<div style="overflow:hidden">your code</div>
However to show ... at the end, you need to get contents of div in javascript and use substr function there. This will be trial and error solution to figure out how many characters can be displayed in the div.
As mentioned earlier, the easiest way to solve the problem would be to add overflow:hidden
to your div's CSS Style.
However, this will not help you add the ellipsis (dots) at the end of the wrapping and there is no way that I am aware of to do multi-line text wrapping (ending with the 3 dots at the end) using solely CSS.
The easier way would be to use jQuery (or similar JavaScript Libraries) to wrap the text and add the 3 dots at the end. Example:
Reference to another StackOverflow post about wrapping content using CSS and jQuery for single line and multi line text.
It's also sometimes recommended to process the content server-side and then display it processed on the page, but it's sometimes more convenient (or faster/easier) to just use JavaScript.
Here's a jQuery Plugin that will do the trick: jQuery DotDotDot
If you want to cut and show ...
and you know the max chars you want, you can just conditionally slice it:
const truncateDesc = description.length > 300 ? description.slice(0, 300).concat("...") : description;
本文标签: javascripthow to truncate text with respect to div heightStack Overflow
版权声明:本文标题:javascript - how to truncate text with respect to div height? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738745467a2110094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论