Use your left/right keys to browse tutorials
Mootools auto font size adjustment

Mootools auto font size adjustment

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on February 5, 2009

font-size-mootools

After browsing the vast space that is the internet, i stumbled upon a great piece of code that, depending on the number of words in a div automatically adjusts the font size. This could be really useful if you have a list of user feedback, that you want to jazz up.

So whats the code doing? Simply put. The code looks for the text within class ‘.text’ then counts the number of words by exploding the text where there is a space, then its just a matter of putting in some if statements to adjust the font size using ‘el.setStyle’.

The Code:

window.addEvent('domready',function() {

    	$quote = $$('.text p:first-child');

        $each($quote,function(el) {

            var $numWords = el.get('text').split(' ').length;

                if (($numWords >= 1) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; ($numWords < 10)) {
                    el.setStyle('font-size','36px');
                }
                else if (($numWords >= 10) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; ($numWords < 20)) {
                    el.setStyle('font-size','32px');
                }
                else if (($numWords >= 20) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; ($numWords < 30)) {
                    el.setStyle('font-size','28px');
                }
                else if (($numWords >= 30) &amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp; ($numWords < 40)) {
                    el.setStyle('font-size','24px');
                }
                else {
                    el.setStyle('font-size','20px');
                };

            });

    	});

make sure that you include the mootools core js file.

The HTML:

<div class="text">

Don't expect me to write anything interesting in here as this is really just a test! but you can check out the demo over at papermashup.com</div>

demo

Credit to Chris Coyier over at CSS tricks for this one.


Recent shares

More tutorials from Papermashup
Comments
One Response about Mootools auto font size adjustment
  1. Oleta McGregor says:

    Not really a comment but a question: I have a client who personalizes dinnerware with names or photos. We have encountered an issue with the names. We would like to be able to populate the text area to automatically size and center. Is that possible? The original files are done in PhotoShop.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Looking for a registry cleaner to speed up your PC and show a full diagnostics?

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.