Skip to main content

Allow Site Visitors To Change Font Sizes on your Blog

Few months back, we received an email from Mr Franco, a senior 62 year old DI reader who was not very happy with the default font size of this blog. He wrote:
What's the programming difficulty of having a site that has fonts changeable by one click from the Explorer menu bar. Are the New York Times programmers more capable? I doubt that... I hope you consider the need of people to see larger fonts sometimes.
While site visitors can dynamically change the font sizes in their web browser using the ctrl button and the mouse wheel, it is not reasonable to expect that everyone would know this font size shortcut.

Dynamic Font SizesMost web users, especially elderly people, look for an option on the webpage itself that would allow them to increase or decrease the text font sizes on the fly. Hence this short Javascript tutorial will show how to add that "dynamic font size" functionality in your blogs.

Technically, there are several ways to resize fonts on the fly like having separate CSS files or by predefining an array of font sizes or a simple font size javascript. Anjanesh has a hack that allows visitors to change fonts families on a webpage.

Here, we use the White Hat hack since it's simple to implement and does the job with few lines of code.

Step 1: Add the following Javascript in the insde the <HEAD> tag of your HTML.
<script language="JavaScript" type="text/javascript">
function changeFontSize(inc)
var p = document.getElementsByTagName('p');
for(n=0; n<p.length; n++) {
if(p[n].style.fontSize) {
var size = parseInt(p[n].style.fontSize.replace("px", ""));
} else {
var size = 12;
p[n].style.fontSize = size+inc + 'px';
Step 2: Insert the following HTML anywhere in your blog - you can customize the text or replace it with visual graphics (like the alphabet A - one small and the other one slightly large)
<a href="javascript:changeFontSize(1)">Increase Font Size</a> 
<a href="javascript:changeFontSize(-1)">Decrease Font Size</a>
You can extend this to either all the HTML elements on your blog or limit it to only the text sections. The font size is specified in pixels.

Related: Create a Printer Friendly Version of webpage with CSS