Skip to content Skip to sidebar Skip to footer

Can I Change Font Size Based On The Number Of Letters?

Possible Duplicate: Auto-size dynamic text to fill fixed size container I have a box which displays the username. However I found that it can only fit 10 letters - I use the sta

Solution 1:

Please try the code below.

<html><head><scriptsrc="http://code.jquery.com/jquery-latest.js"></script><scriptlanguage="javascript">

    $(function() {
        var len_fit = 10; // According to your question, 10 letters can fit in.var un = $('#user_name');

        // Get the lenght of user name.var len_user_name = un.html().length;
        if(len_fit < len_user_name ){

            // Calculate the new font size.var size_now = parseInt(un.css("font-size"));
            var size_new = size_now * len_fit/len_user_name;

            // Set the new font size to the user name.
            un.css("font-size",size_new); 
        }
    });
</script></head><body><h1id="user_name">Your Long Name</h1></body></html>

I hope this could help you.

Post a Comment for "Can I Change Font Size Based On The Number Of Letters?"