jQuery Tip: Quick and Easy Font Resizing

Wednesday, January 7th, 2009

One of the many great features of jQuery is how easy it is to edit the css via a user event, such as a click. We can use this to knowledge to edit any css property we wish. Recently, NETTUTS had a great article that went all out with a font resizing slider bar using the jQuery UI. While this article was certainly in-depth and well done, we want something quick and simple. Today we will create a simple script to allow the user to edit the current font size of an element.

Our Goal

Before starting any project, we need to know what we want to do. We want a simple and quick script to resize the text within a certain element 'on click'.

Basic HTML

First thing we need to do is setup our simple HTML page, we will use the code below for the demo.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<meta name="author" content="Drew Douglass" />
	<meta name="description" content="" />
	<meta name="keywords" content="" />
	<title>jQuery Font Size Change | Dev-Tips</title>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
	<script type='text/javascript'>

	</script>
</head>

<body>
	<h2>Text Resizing with jQuery Demo.</h2>
	<input type='button' value='Larger' id='large' />
	<input type='button' value='Smaller' id='small' />
	<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
</body>
</html>

Setup some variables

Now it's time to setup some variables to use later on in our script, I'll explain more after the code below.

$(function(){
		$('input').click(function(){
			var ourText = $('p');
			var currFontSize = ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);

First we use some shorthand jQuery, which is the same as using document.ready(). Next we will setup some variable to use when the input buttons are clicked. The variable ourText will be our paragraph to re size. The variable currFontSize gets the current font size of the paragraph. The variable finalNum uses the parseFloat function to convert the string into a floating point number (with a base of 10). The variable stringEnding gets the pixels or em ending so we can add it back on later.

Conditional Statements

Next we need to decide what to do depending on the button the user is clicking on.

if(this.id == 'large') {
				finalNum *= 1.2;
			}
			else if (this.id == 'small'){
				finalNum /=1.2;
			}
			ourText.css('fontSize', finalNum + stringEnding);
		});
	});

The above should be pretty self-explanatory. If they clicked on the large button, we multiply the current font size by 1.2, and if they click on the small button the font size is divided by 1.2.

Full Script

Our final script looks like this:

$(function(){
		$('input').click(function(){
			var ourText = $('p');
			var currFontSize = ourText.css('fontSize');
			var finalNum = parseFloat(currFontSize, 10);
			var stringEnding = currFontSize.slice(-2);
			if(this.id == 'large') {
				finalNum *= 1.2;
			}
			else if (this.id == 'small'){
				finalNum /=1.2;
			}
			ourText.css('fontSize', finalNum + stringEnding);
		});
	});

And that's it, we now have a quick and simple font resizing script using jQuery. Don't forget to check out the demo below if you want to see it in action.

View the Demo

Update with animation!

Due to the popularity of this article, we have published another font resizing script, this time with animations on the resize. Be sure to check it out 'Font resizing with animation effects.'.

If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hi, I'm Drew Douglass and I make sure Dev-Tips.com runs smoothly. I also write for NETTUTS.com and ThemeForest.net. I'm passionate about anything web development related, especially php, WordPress, MySQL, and jQuery. Feel free to follow me on twitter.
  1. Juarez
    January 13, 2009 at 5:47 am
    • January 13, 2009 at 10:57 am
  2. January 13, 2009 at 8:53 am
    • January 13, 2009 at 10:52 am
  3. January 13, 2009 at 10:30 am
    • January 13, 2009 at 10:53 am
  4. January 14, 2009 at 11:18 am
    • January 14, 2009 at 2:20 pm
  5. January 28, 2009 at 1:35 pm
    • January 28, 2009 at 2:11 pm
  6. April 15, 2009 at 4:29 pm
    • April 15, 2009 at 6:16 pm
  7. April 30, 2009 at 4:52 pm
    • April 30, 2009 at 4:54 pm
      • April 30, 2009 at 8:46 pm
  8. April 30, 2009 at 8:51 pm
    • April 30, 2009 at 8:57 pm
      • April 30, 2009 at 9:02 pm
    • April 30, 2009 at 9:00 pm
      • April 30, 2009 at 9:09 pm
  9. May 1, 2009 at 12:12 pm
    • May 1, 2009 at 12:36 pm
  10. Moon
    May 22, 2009 at 9:32 am
  11. key
    June 4, 2009 at 3:23 am

Leave a Reply

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