I have a few users who complain that the font size on my web application is too small to read. They aren't happy with trying to resize the font setting on the browser because when they make the font larger the dimensions and layout of the application change. Are there any existing tools that give me a good way to let the end user pick a small/medium/large font size and have that change the .css of the site to a larger font size without affecting the layout of the page?
views:
135answers:
4
+5
A:
Here's code I use on some of my sites. Apply your HTML accordingly:
HTML:
<span>Font Size: </span>
<a href="#" onclick="decreaseFontSize();">
<img src="/images/minus.png" alt="Decrease Font Size" />
</a>
<a href="#" onclick="increaseFontSize();">
<img src="/images/plus.png" alt="Increase Font Size" />
</a>
Javascript:
var min=8;
var max=18;
function increaseFontSize() {
p = document.getElementById("[the id of container w/font you want to size]");
if(p.style.fontSize) {
var s = parseInt(p.style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=max) {
s += 1;
}
p.style.fontSize = s+"px"
}
function decreaseFontSize() {
p = document.getElementById("[the id of container w/font you want to size]");
if(p.style.fontSize) {
var s = parseInt(p.style.fontSize.replace("px",""));
} else {
var s = 12;
}
if(s!=min) {
s -= 1;
}
p.style.fontSize = s+"px"
}
Jason
2009-11-24 19:33:34
I have exactly the same script! I think I repaired it a bit, I have to check...
Trick
2009-11-24 19:38:30
it's possible. i didn't author this font, although i modified it a bit maybe 2 years ago. looking at it now, it could be easily merged into one function that takes a bool param telling you which way to go.
Jason
2009-11-24 19:41:48
*i didn't author this *script*. wow. can you tell i just finished my coffee?
Jason
2009-11-24 20:02:02
:) It can be seen :) I have added as an answer what I changed.
Trick
2009-11-25 08:35:07
+1
A:
If the layout of your application changes in such a way that your users find it unusable there must be some problem with your CSS. Perhaps you have assumed a specific font size when designing the application, or only tested with one font size. I would suggest that you try using your own site with a different font size for a while to find out where the problems with the layout are, and then try to fix them so that it looks good regardless of the font size.
Mark Byers
2009-11-24 19:41:51
A:
var min=8;
var max=30;
var reset=14;
function increase(tag) {
var p = document.getElementsByTagName(tag);
for(i=0;i<p.length;i++) {
if(p[i].style.fontSize) {
var s1 = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s1 = reset;
}
if(s1!=max) {
s1 += 2;
}
p[i].style.fontSize = s1+"px"
}
}
function decrease(tag) {
var p = document.getElementsByTagName(tag);
for(i=0;i<p.length;i++) {
if(p[i].style.fontSize) {
var s = parseInt(p[i].style.fontSize.replace("px",""));
} else {
var s = reset;
}
if(s!=min) {
s -= 2;
}
p[i].style.fontSize = s+"px"
}
}
function reset(tag) {
var p = document.getElementsByTagName(tag);
for(i=0;i<p.length;i++) {
p[i].style.fontSize = reset+"px"
}
}
I changed this script so it decreases by tags and added reset function.
Trick
2009-11-25 08:36:31