Suppose I have a div element with height: 150px and width:300px. Is it possible to calculate how many lines of text can it be reside within it without overflow, where text have predefined font-size, line-height and other text properties?
My Main Objective:
I have html like this:
<div id="parent">
<div>[Title]</div>
<div>[Description]</div>
<div>[Buttons]</div>
</div>
In [Description] div I have to display exactly eight lines of description where description comes form DB and size may be much or less. And the last lines will truncate with ... at the end. How can I achieve my goal?
Pleas help me.
Yes, there is a way to only show 8 lines. Basically, what you can do is add each word individually, and every time you add a word, check if the height of your div has changed. Count how many times it changes, and if more than 8, stop adding words.
Live example
If you need to support IE,
getClientRects()[0]won’t have aheightproperty, but it has abottomandtopwhich you can substract to get the height.That was fun, if you have any questions don’t hesitate to ask in the comments.