font-size depending on character length?

无人久伴 提交于 2020-01-14 14:36:26

问题


Is it possible to change the div's font-size depending on how many characters are in it? I have image album titles in small fixed width div's (100px). Sometimes the album names have too many characters that they force a new line. So I was wondering if it is possible to re-size the font to keep the title on one line?


回答1:


Yes, by assigning a variable to a class:

In your css:

.longstring {
  font-size: 10pt;
}
.shortstring {
  font-size: 14pt;
}

In your view

<?php
$random_number = '42';
if(strlen($div_string)>$random_number){
    $font_class = 'longstring';
}else{
    $font_class = 'shortstring';
}?>
<div class="<?php echo $font_class; ?>">
    <?php echo $div_string; ?>
</div>



回答2:


There is not a css way to do it you can use javascript or text-overflow in css3



来源:https://stackoverflow.com/questions/9045390/font-size-depending-on-character-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!