简体   繁体   中英

How to make font-size change depending on string length?

I have a table:

<table width="200px">
   <tr>
     <td>
       <font style="font-size:14px;"><?php echo $text; ?></font>
     </td>
    </tr>
 </table>

The $text variable is dynamic, and might be 3 characters long, or 50 characters long.

The problem is the width of the table column is limited to 200px.

How can I make the text always fit on one line, and preserve the limited width of the table?

I am looking for something like a dynamic font-size function? Or any other (good/easy) way?

Thanks

如果您使用的是jQuery,则自动调整大小的动态文本以填充固定大小的容器中的解决方案似乎将为您工作。

Using Mootools you can basically measure an Element size.

I think that if you create a dummy element in the DOM (positioned outside the screen view) containing the text you need to insert. Then you can cycle the font size until you find the dimension that fit you're table width.

A little bit dirty ...

EDITED: same approach, but in jquery, is used by miket2e answer link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM