Change Font Size with JavaScript
While building your website or blog do you want to give your readers the option of changing font size? This is usually a good idea and your viewers appreciate the functionality. You can change font size by loading an alternate stylesheet or using JavaScript. I prefer a simple JavaScript function that will change the font size according to the user’s selection. You can even implement it into your blog, if you want me to turn the code into a wordpress plugin, let me know. Here is how to do it in the most simple way:
Between your <head> and </head> tags add the following JavaScript:
That’s it. Enjoy!
Popularity: 46%
If you enjoyed this post, please consider to leave a comment.
Comments
This is a great way to give the user an option for increasing and decreasing font size. I’m using this with the Vanilla forum software. One problem I am looking into is they have preset body id depending upon what page you are on. So, could I change the js to accept a parameter identify the id instead of “text”?
I finally did get this to work with Vanilla and have created a Vanilla extension, giving your attribution. You can see it in action at my forum.
Well, I thought it worked but maybe I don’t understand how to pass a variable to the js so that it will change the font size of the body id that is passed. Can you help?
The code is working fine. But when I add an ‘onMouseOver’ event to the tag the event was not fired in IE but in FF and NN it works correctly.
Can anyone reply?
Thanks in advance.
For wordpress you can use WP-CHGfontsize
I have tried applying the text thing to a website I am working on: http://www.seniorsrelocating.com/services.list.a.html
This page is not ready for the real world yet.
It is not working. Is it because I have the text in a table?
I work in dreamweaver and am still learning!
Please help!
Thank you,
Cindy
This code won’t work for me in IE7 either. Works perfectly in FF. Any ideas on how to make it work in IE?
Hey not sure if this will help you, from a project of mine works in ie7
Javascript:
function setFontSize(){
size = document.getElementById(’selectSize’).value;
document.getElementById(’center’).style.fontSize=size;
}
Html
Font-size: 8px9px
(i removed most of the sizes to reduce space)
The above post didnt like my html input
Font-size: <select onchange=”setFontSize()” id=”selectSize”><option value=”8″>8px</option><option value=”9px”>9px</option>
Hi Phil,
Thanks for posting a work around for IE7.
Since I’m a newbie, would you mind posting the code with full html? I’m afraid I can’t get it to work, and I’m not sure where to start troubleshooting.
Also, I’m wondering if your code works in FF as well?
I appreciate the first option with the Javascript. However, it only appears to work on a page-by-page basis. Is it possible to have it apply to the entire site?
Thanks.
In order for any javascript to be run on entire site you have to declare it on every page or put it in external file and call that file on every page. If you are running wordpress, you can just put the javascript part in header and the bottom part in your sidebar. Hope that helps.
Great Job. Thank you for the simple, efficient and effective script. I am using it for one of my upcoming site. I’ll inform you once its ready. Cheers
[…] * Change Font Size with JS Link * The JavaScript Source Link * Visual Effects - Java scripts Link * Working with iFRAME in JS Link […]












“…if you want me to turn the code into a wordpress plugin, let me know.”
Did you ever get around to making this a plugin? I’m going to try it with the instruction you’ve outlined, but a plugin would be great.
Thanks
Tim