简体   繁体   中英

font-size depending on character length?

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?

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>

没有css方法可以在css3中使用javascript或文本溢出

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