0

I have asked a similar question here : How to auto resize text in fixed DIV according to the text's length?

And its working. But its working on a single line of text. Let's say I want my text to wrap to 2 lines, and if its more (overflowing) then go and resize...

Is there a way to do this?

Thanks!

Community
  • 1
  • 1
Himberjack
  • 5,682
  • 18
  • 71
  • 115

2 Answers2

1

if you know font-size, font-style and line-height and I think you know it if it is your site :). You can write javascript function that will add characters to div until it's height is less or equal then two lines height.

Here is sample that worked in all browsers for one line check. To tell the truth it is not my solution, however my coworker allowed to post it :).

    function fitInWidth(name) {
        this.empty().append(name);
        while (this.height() > 19) {
            name = name.substring(0, name.length - 3);
            while (name.charAt(name.length - 1) == ' ') name = name.substring(0, name.length - 2);
            this.empty().append(name = name + '…');
        }
        return name;    
}
Danil
  • 1,883
  • 1
  • 21
  • 22
0

Could make the div inline then find the height and then make it block but with the new height.

 $("#element").css("display","inline").css({height: $("#element").height(), display: "block"});
Louis
  • 4,172
  • 4
  • 45
  • 62