Click here to Skip to main content
Click here to Skip to main content

CSS and Font Sizes

By , 28 Sep 2002
 

Introduction

It happens time and again. A graphic designer makes his or her cool new layout, complete with font sizes tweaked to perfection. This designer then tries to change the font size in their browser, and, much to his or her dismay, things break. Carefully sized tables overflow, things look different, and generally this designer curses the font-size change feature.

One solution to this problem is to fix the font sizes so that browsers can not change them. The advantage of this approach is that it appears to solve the problem, and the layout remains static, even when that nasty font-change feature is used. The designs are protected from the user, who simply does not know good design when he sees it!

What's the problem?

In terms of HTML and the Web, fixing the fonts this way is a hack. It is an inelegant solution to the problem, and thats not all. This method is disenfranchising a vast swath of potential customers/visitors. The designer is locking out platforms with different default type-rendering systems, like the Mac, and is of course interfering with a disabled visitor's ability to read at a comfortable text size. High-resolution-using visitors often want to dedicate more pixels to the rendering of fonts, but can not because the designer didn't think anyone would ever want his pristene design to change. This designer has instead locked such users into font sizes mesuring just a millimeter or two when the font is at 7pt and the display at 1600x1200 or higher. Clearly, this should be addressed.

So what is the real answer, then?

Example Area
Note that everything in this box scales when you change the font size. The border, the image, and all the text. Browsers can choose the font size they prefer, and the content of the box continues to look almost exactly the same.

A better solution is to use the EM unit in the CSS specification, which is well-supported by most browsers for both positioning and sizing.

EMs are always supposed to be the same relative size, as they are defined as about the width of the letter 'm' in the current font. The current font is always the font used by the parent object in the HTML DOM, or the default font size if none is specified. If you use the default font size, then when the user changes the font size in the browser, all objects defined via EMs are scaled in an exactly proportional way, since your font change command causes the size of the letter 'm' in the document's default font to change. You can define any HTML element in terms of EM via CSS.

6 X 3 em
40 X 20 pt
1.2 X 0.6 cm

So what does this solve? Well, if designers embrace variable font sizing on the web, not only will their design be more robust, it will survive the transition between platforms and resolutions much more successfully. Once your page uses this method, your visitors will no longer be frustrated by fixed font sizes, paving the way for a more flexable and useable web for all of us.

What's the catch?

EM's are - as you will recall - defined as the width of the letter 'm' in the parent object's font. The parent object may be defined in terms of points, pixels, or some other unchangeable unit, and so most of the power of EM is lost. It just becomes a fancy way of saying 'make this font 1.5 times bigger'. This causes things to act strangely, especially since width:1em will use the document default font, whereas font-size:1em will use the 'parent object font' method. This means you can't have perfect scaling of objects, like in PDF, and things quickly get out of control if you are not careful.

What is needed for PDF-like scaling is a global EM-like unit which is only resized by the user via the browsers font size commands, and not affected by the parent object. With this unit, we could build a page that would reflow with browser width like HTML, but which could scale depending on user preference. Images and other objects would all resize perfectly, and a printout would appear perfect as well.

Conclusion

So what should we do until we get this 'PDF-like' scaling? Either we can use fixed-size fonts as most do today. These fixed-size pages definitely appear as the designer intended, but is that style of page design appropriate given the current state of the Web? Hopefully you will agree that a good design should complement the medium used to convey it, and not attempt to fight it with size-constraining tricks and other carefully crafted hacks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Christopher Lord
Web Developer
Canada Canada
Member
I am currently a student at the University of Alberta
after working over two years as a programmer for a small company.
  • Software Developer
  • Graphic Designer
  • Student
www.clawbox.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionInformative post [modified]membergggmarketing10 Sep '11 - 3:22 
I ahve found the page very important for the CSS learner. In this page you have given a clear idea about CSS basic concepts. It's very good job. Keep providing such Concept.
 
I have a Question that what is the benifit of using % in the place of px?
 
Waiting for answer.

GeneralProblem when downloadinggrouppapa216 May '10 - 7:08 
Thanks a lot of your efforts,,
 
when I signed and download the deleting and updating file the file have no the showing data which is in the page,,,
 
thakx
 
asmaa1990-1@hotmail.com
GeneralBrowser compatibilitymemberTheAlas13 May '09 - 21:52 
Hi all, I want to note that scaling only really works on mozilla-based browsers. Other browsers only have the feature but it's in many cases useless as it either doesn't work at all (safari) or makes the site hard or impossible to read (IE). Firefox is so far able to resize even the most stubborn of sites, along with all of their graphical content (with anti aliasing too) leaving site layout almost untouched.
 
So, for all those who like BIG, FF is the best according to my testing.
 
Sadly, for developers - making zooming to work with all browsers involves too much of a design sacrifice which makes the site ultimately unappealing compared to competition. This problem is especially present with smaller sites.
GeneralThanksmemberAhmad.Nour23 Nov '08 - 0:53 
it's Good Shucks | :->
 
Persian Golf

GeneralCheck out YUI FontsmemberP.J. Tezza14 Sep '07 - 15:38 
I really like the idea of em units (and allowing the end user to resize text with the browser's Text Size feature), but they are painfully quirky across different browsers. I've done a fair amount of work with ems and two problems occur: One, even if you are careful, the difference in text sizes between browsers is significant. Two, IE's Text Size feature doesn't really work well with ems.
 
Check out YUI Fonts. Even if you don't use it, you'll get a feel for how tough matching font sizes and line heights is across browsers. To get IE's Text Size feature to work well, they use percentages instead of ems. However, they have to use all kinds of tricks to get 100% to be 13 pixels, 136% to be 16 pixels, etc..
 
The best looking (and working) non text size based browser zoom feature is on Opera. Recent versions of Opera are free and ad-free. I'm not a fan boy, but low vision users should check it out.

Generalgreat job!!memberLubna_047 Dec '06 - 0:43 
Its a great thing. thanks a lot.
GeneralAmazing how many websites loose business because of thismemberowen65432124 Sep '06 - 9:17 
After injuring my back, I lay down while programming, which puts me about 4 feet away from my screen (1600x1200, extra large fonts). Evem after selecting "Ignore Font Sizes specified by the web site" under the accessibility menu, there are so many TOP NOTCH, popular sites that fail to render properly, or even worse, part of the site scales, but other parts of the site (usually the critical menus) remain tiny. I am sure there are many users, like myself, who simply navigate away from the site if we can't read the important text. Also, I am a firm believer that users should have the option of selecting image sizes for viewing maps, graphs, etc. Anyway, just my rant / two cents Smile | :)
 
The stats guy behind-the-scenes at http://www.braintechllc.com.

GeneralExample AreamemberChristopher Lord29 Sep '02 - 7:45 
The example area is broke now. It might have been when the article changed from an 'unedited' contribution. The box was specified in EMs so that it would scale with the font size of the browser. The small box to the left still scales properly, however.
GeneralRe: Example AreamemberShog930 Sep '02 - 13:51 
Yes, it's broken for IE, since CP uses fixed-sized fonts for articles. Mozilla however, lets you "zoom" fonts even when they're sizes are fixed (*very* useful), so the demo still works there.
 
Shog9
------
If you really what to get shitfacde tell a crowded bar that is is yourt borthday and hife your wallet. you ka will many more friends.
- David Wulff, Brithday selebrations, 9/19/02

GeneralNice oneeditorPaul Watson29 Sep '02 - 2:31 
I especially liked the "Hopefully you will agree that a good design should complement the medium used to convey it, and not attempt to fight it with size-constraining tricks and other carefully crafted hacks. " paragraph.
 
The problem is that clients expect pixel perfect layouts. They think the web is a print page and that we have absolute control. Even if we had, we shouldn't want absolute control.
 

BTW there is one problem with EMs. They are not "sized" the same across all browsers (just like the small, medium, large etc. keywords) and so 1em in one browser may be fine but on another it is unreadable or too large.
 
At the moment there really is no perfect way of controlling font-sizes and keeping your content accessible. So we must either give up control, or fix our designs.
 
Paul Watson
Bluegrass
Cape Town, South Africa

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 29 Sep 2002
Article Copyright 2002 by Christopher Lord
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid