最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Shrink text to table cell - Stack Overflow

programmeradmin1浏览0评论

Maybe it's an easy question, but there seems to be no obvious answer.

I have a table with a fixed layout (cells can't resize without screwing up the entire layout) and some dynamic text inside some cells. In case the text overflows, the text should shrink to fit inside the cell, instead of being clipped or wrapped.

I actually expected had to be a good CSS solution, but I failed to find any. Do I really have to do this manually using JavaScript?

Any idea? (best solution would be to not put dynamic text inside a fixed layout table, of course, but in this case there's no way around).

Maybe it's an easy question, but there seems to be no obvious answer.

I have a table with a fixed layout (cells can't resize without screwing up the entire layout) and some dynamic text inside some cells. In case the text overflows, the text should shrink to fit inside the cell, instead of being clipped or wrapped.

I actually expected had to be a good CSS solution, but I failed to find any. Do I really have to do this manually using JavaScript?

Any idea? (best solution would be to not put dynamic text inside a fixed layout table, of course, but in this case there's no way around).

Share Improve this question edited Sep 13, 2018 at 15:30 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 19, 2011 at 21:42 leolukleoluk 13k6 gold badges46 silver badges51 bronze badges 2
  • I'm thinking that, other than the possibility of an awesome algorithm, one would need to check for overflow, shrink, check, shrink, and so on until the text fits. – JCOC611 Commented Oct 19, 2011 at 21:44
  • Problem finally solved by not exporting to HTML at all and going the PostScript -> GhostScript -> PNG path instead. Publishing Excel spreadsheets was finally more plicated than expected. – leoluk Commented Feb 9, 2012 at 20:35
Add a ment  | 

4 Answers 4

Reset to default 3

I hate to be the guy that says "you can't", but today I'll have to be that guy. Unfortunately there is no way to dynamically size text based on the length of a text string using only CSS. You'll certainly have to solve this using JavaScript.

Different font sizes on a table is going to look horrific, if acceptable, clip the text with overflow: hidden; text-overflow: ellipsis; and add a tooltip so people can see the clipped text

For anyone still interested in this, I wrote a PHP function to wrap the text in a span class and control the size. You can play with the $maxLength and $divisor variables to get it right for your table and then apply the function globally:

    function ShrinkText($text)
    {
        $maxLength = 20; // The length at which to start shrinking.
        $divisor = 5; // The factor to shrink by

        /*
         * A divisor of 5 with a string of 30 would do the following:
         *
         * $diff = (30 - 20)                                     [10]
         * $multiplier = ($diff / $divisor) * 10     [(10/5)*10 = 20]
         * $percentage = 100 - $multiplier            [100 - 20 = 80]
         * 
         * Output = '<span style="font-size:80%;">' . $text . '</span>'
         *
         */

        if(strlen($text) > $maxLength)
        {
            $diff = (strlen($text) - $maxLength);
            $multiplier = ($diff / $divisor) * 10;
            $percentage = 100 - $multiplier;

            return '<span style="font-size:' . $percentage . '%;">' . $text . '</span>';
        }
        else
        {
            return $text;
        }
    }

Nowadays (Original answer was 8 years ago), using "vw" instead "px" on text-size does the magic. Left this for future references to whom may need this info.

For a detailed reference of setting CSS font-size on webpages, please read this reference site - Backup.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>