![]() |
![]() |
|
A Merger of Content Management and Localization Workflow The Wrong Way to Use CSS in Page Layouts A Simple Character Entity Chart Inside the evolt.org Rebuild: The HTML and CSS Breadcrumbs for Those Using ASP Real-World Browser Size Stats, Part I Real-World Browser Size Stats, Part II Using Excel Spreadsheets as Web Data Sources Let the User Skip the Splash Page Some Caveats with Using Frames Give the User Control Over Your Fonts. |
Home » About » Technical Articles » Give the User Control Over Your Fonts. ![]() This article was originally posted on evolt.org, an online resource for web developers, maintained by web developers. Give the User Control Over Your Fonts
Originally published:
July
13, 1999
There's nothing quite like getting your text just right with all your CSS attributes in place, and sitting back knowing that you finally have some typographic control over your pages. It's just too bad that's not the case for your users. A user may come to your site and see text so large they can't see more than three lines at a time, or so small they can't even see your text. "But wait," you say, "I was careful to test it on Ed's machine, and Sally's, and even mine — in two browsers at that. What could possibly be wrong?" Well, even though the CSS specification supports many different relative attributes, none of them seem to work on any of the current browsers.
Correct rendering of the ' So it doesn't work, nor do any of the other relative sizing units — at least not consistently across browsers and platforms. Instead you may find you are using points, or pixels, or something else to set your font sizes, and you try to accommodate all your viewers by making it the most 'average' size you can. But it doesn't have to be that way; you don't have to completely wrest control from your users. Instead, empower them again, give them the ability to control what they see, let them, not you, decide that smaller text is better. I'm a big proponent of server-side solutions whenever possible, it takes some strain off the browser, and ensures a consistent experience for all your users since nobody has to wait for clunky JavaScript to parse on their old P75 or Centris 610 with 16MB of RAM (way too little if you run any of today's bloated browsers or OSes). So, given that disclaimer, I'm going to offer a solution to the problem using Active Server Pages, which results in plain ol' HTML when passed to the browser. My code will not work with Apache, or ColdFusion, or any other server-side technology, but the capability to do it is in all of them, so at least follow the ideas as I go.
Let's start with a real-world example. If you surf over to any of the
pages at my personal gallery in a 4.x browser,
http://roselli.org/adrian/
(please pardon the plug), you will see in the navbar a pair of buttons.
By default, one should read 'Enlarge Text' and the other
should read 'Shrink Text.' Clicking one of these buttons will reload
the current page with a link to a different style sheet (I use the
Let me run down what happens when you first come to the site (provided I haven't changed anything, and it's working correctly):
While this doesn't cover all the bases, it does kill quite a lot of
birds with one stone (pardon the metaphor mixing). It is also
only a CSS solution, although it wouldn't be too hard to make
it apply to the So, on to the code.
First, you need to write up your cookie-setting script so you can call
it from within every page on your site. Save the file as
'
<%
'## Code by Adrian Roselli from http://roselli.org
'## Provided free for evolt.org and its members.
'## No catches, just give me credit.
'## This browser detection only detects IE4.x and NN4.x, please modify it as you see fit to get the 5.x and alternate manufacturer CSS-capable browsers.
'## Last change July 10, 1999
'########################
'## Browser Determination
'## Get the browser name and version number
UserAgent = Request.ServerVariables("HTTP_USER_AGENT")
'## If the cookie Browser has no value, then perform the following
IF Request.Cookies("Browser") = "" then
'## Check the UserAgent string to see if it contains Mozilla or MSIE, and then to see if it has 4. in the version number
if (instr(1,UserAgent,"Mozilla",1) > 0 OR instr(1,UserAgent,"MSIE",1) > 0) AND instr(1,UserAgent,"4.",1) > 0 THEN
'## Adds 1 month onto todays date so the cookie will expire 1 month from now
NewerDate = DateAdd("m", 1, FormatDateTime(Now,vbGeneralDate))
'## Sets a cookie so that the browser is marked as CSS-capable
Response.Cookies("Browser") = "CSS"
Response.Cookies("Browser").expires = cdate(NewerDate)
end if
END IF
'################
'## Set Text Size
'## If the URL does not have a ?size=value appended to it (meaning the user clicked one of the links), then perform the following
IF NOT Request("Size") = "" THEN
'## If the user clicked to enlarge, then set the cookie to large
IF Request("Size") = "Large" THEN
Response.Cookies("TextSize") = "Large"
Response.Cookies("TextSize").expires = #10/10/2000#
'## If the user clicked to normalize, then set the cookie to large
ELSEIF Request("Size") = "Normal" THEN
Response.Cookies("TextSize") = "Normal"
Response.Cookies("TextSize").expires = #10/10/2000#
'## If the user clicked to shrink, then set the cookie to small
ELSEIF Request("Size") = "Small" THEN
Response.Cookies("TextSize") = "Small"
Response.Cookies("TextSize").expires = #10/10/2000#
END IF
'## Otherwise, check the cookie, and if it also has a blank value, set it to normal
ELSEIF Request.Cookies("TextSize") = "" THEN
Response.Cookies("TextSize") = "Normal"
Response.Cookies("TextSize").expires = #10/10/2000#
END IF
'##########################
'## Get Current Script Name
'## Sets ScriptName equal to whatever the current script name is
ScriptName = Request.ServerVariables("SCRIPT_NAME")
%>
Now you need to include this file in your script (web page). To
accomplish this and to ensure any time you need to update your script you can do so with little trouble,
we will include it in all pages on the site. We will assume that your
scripts (web pages) are in a different directory at the same level
as your ' <!--#include virtual | file = "../includes/cookie.asp"-->
Now you need to create your resize links. Create
another text file and name it '
<%
'## Code by Adrian Roselli from http://roselli.org
'## Provided free for evolt.org and its members.
'## No catches, just give me credit.
'## Make sure you supply your own images, rollovers, whatever. These are just suggestions, especially since text links are not always attractive.
'## Last change July 10, 1999
'## Request the cookie Browser and see if it is a CSS-capable browser, based on your previous work.
'## This ensures that you do not display the buttons if the browser cannot use them.
IF Request.Cookies("Browser") = "CSS" THEN
%>
<%
'## If the text isn't already set to large, offer the option.
IF NOT Request.Cookies("TextSize") = "Large" THEN
%>
<a href="<% = ScriptName %>?size=Large">Enlarge Text</a><br>
<% END IF %>
<%
'## If the text isn't already set to normal, or is not set, offer the option.
IF NOT Request.Cookies("TextSize") = "Normal" OR Request.Cookies("TextSize") = "" THEN
%>
<a href="<% = ScriptName %>?size=Normal">Return Text to Normal</a><br>
<% END IF %>
<%
'## If the text isn't already set to small, offer the option.
IF NOT Request.Cookies("TextSize") = "Small" THEN
%>
<a href="<% = ScriptName %>?size=Small">Shrink Text</a><br>
<% END IF %>
<%
'## Display this text if the browser is not CSS capable - you could also just leave it blank.
ELSE
%>
<i>Sorry, you can't see this because I've determined your browser can't do CSS.</i>
<%
END IF
%>
Now you can see that when a user clicks one of the links, it reloads
the current script (web page) with the size appended to the end of the URL.
It does this because Now, to put the resize links in your script (web page), just insert the following line in your page where you want the links to appear: <!--#include virtual | file ="../includes/CSSResize.asp"-->
We are now on to the last step, including the updated CSS in the
document. The next step you are going to take is create a new
text file called '
<%
'## Code by Adrian Roselli from http://roselli.org
'## Provided free for evolt.org and its members.
'## No catches, just give me credit.
'## Make sure you point to the right style sheets, silly.
'## Last change July 10, 1999
'## Iterate through the size options, with a default if there are no matches.
IF Request.Cookies("TextSize") = "Large" THEN
%>
<LINK REL="stylesheet" TYPE="text/css" HREF="../includes/large.css" NAME="master">
<%
ELSEIF Request.Cookies("TextSize") = "Small" THEN
%>
<LINK REL="stylesheet" TYPE="text/css" HREF="../includes/small.css" NAME="master">
<%
ELSE
%>
<LINK REL="stylesheet" TYPE="text/css" HREF="../includes/normal.css" NAME="master">
<%
END IF
%>
Or, if you prefer to include all of your styles within each page, then
you can use the following script. Don't worry, the server will only
send the chunk of CSS that you specify with your
<style type="text/css">
<!--
<%
'## Code by Adrian Roselli from http://roselli.org
'## Provided free for evolt.org and its members.
'## No catches, just give me credit.
'## Make sure you include all your styles, instead of just this one, silly.
'## Last change July 10, 1999
'## Iterate through the size options, with a default if there are no matches.
IF Request.Cookies("TextSize") = "Large" THEN
%>
.text { font : 12pt Arial, Helvetica, serif ;
color : #cccc99 ;
line-height : 16pt }
<%
ELSEIF Request.Cookies("TextSize") = "Small" THEN
%>
.text { font : 8pt Arial, Helvetica, serif ;
color : #cccc99 ;
line-height : 16pt }
<%
ELSE
%>
.text { font : 10pt Arial, Helvetica, serif ;
color : #cccc99 ;
line-height : 16pt }
<%
END IF
%>
-->
</style>
Somewhere within the <!--#include virtual | file ="../includes/CSS.asp"--> Congratulations, if I wrote the script correctly, then you should be ready to experiment on your own servers. For double-checking purposes, the final code on your page may resemble this: <!--#include virtual | file ="../includes/cookie.asp"> <html> <head> <title>Soylent Green</title> <!--#include virtual | file ="../includes/CSS.asp"--> </head> <body bgcolor="#003333" text="#cccc99" link="#cc9933" vlink="#999999" alink="#ff0000"> <font face="arial,helvetica,sans serif" size="2" class="text"> This is some standard body text. It will change size of you click a link below. <br><br> <!--#include virtual | file ="../includes/CSSResize.asp"--> </font> </body> </html>
There are many more options available to you here. Perhaps you want
more than 3 styles, maybe you want 6. Perhaps you want the styles to change depending on
where your user is surfing — for instance, pages of just text get a larger
stylesheet, but pages with images and only caption text may have smaller stylesheets.
You can also use the same code to re-write your Obviously, many of these suggestions go beyond what you may need to do for your users, but at the very least, giving the user control over the fonts on your site could go a long way to keeping them as repeat users. And at the same time, you may sleep better knowing that you are working toward making the web a friendlier place for both the eagle-eyed and the lazy-eyed. |
|
© 1998-2010 Algonquin Studios
|
200 Brisbane Building, 403 Main Street, Buffalo, NY 14203 | p.716.842.1439
|