Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to truncate text with respect to div height?

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.

enter image description here

Can anyone help?

like image 591
Zain Shaikh Avatar asked Aug 15 '12 07:08

Zain Shaikh


People also ask

How do you truncate text in CSS?

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.

How do I hide the overflow text in a div?

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.

How do you truncate a paragraph to 100 characters in HTML?

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.

How do you measure the height of a div?

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.


2 Answers

Try the following CSS:

text-overflow: ellipsis;
overflow: hidden;
whitespace: no-wrap;

This only works for single lines. For multiple lines you need JavaScript.

like image 117
Tanzeeb Khalili Avatar answered Sep 28 '22 19:09

Tanzeeb Khalili


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>
like image 43
yckart Avatar answered Sep 28 '22 21:09

yckart