I'd say it can't be done in a reliable way.
I don't think nizsmo's will work because this "go to the last character of the first line, which is determined by the "width"" assumes that the width is measured in characters, but it is measured in pixels. You would have to do some calculations on how many pixels in a character, but tath wouldn't work because most fonts aren't fixed width (ie different characters are different widths).
Main Topics
Browse All Topics





by: nizsmoPosted on 2007-11-02 at 22:15:31ID: 20206023
Theoratically, you can write a function/formula which calculates this, based upon the width of your div compared with the length of your string/sentence/content.
If the whol word gets wrapped (instead of partial word) then you can go about doing something like this as an idea:
we can use the substr() method in javascript, and go to the last character of the first line, which is determined by the "width", and detect if the next character is a space, if not then we keep rolling back to the previous character until a space is encountered, then we know the div will wrap from there (since it wraps the whole word).
We can then go from there and repeat the step for the second line (since now we know where the second line begins) and repeat until you get to the third and fourth line which is what you are wanting.
Hope this gives a a kind of idea on how to do it with javascript. I'm not sure if there's an easier way of going about doing this.