Design

Unique Numbers in CSS

If you’ve been learning CSS and looking around people’s code all over the web, I’m sure you have found some strange numbers inside their CSS files, seemingly used out of the blue. This is the list of some of those numbers that I noticed, accompanied with some background information about it.

-9000px

This is the number originally used by Mike Rundle on his famous Phark image replacement technique. Another variation is -100em or -5000px or -9999px. Nevertheless, the idea is to apply a big, negative number to the CSS text-indent property, so that it puts the text way, way off of the screen, effectively hiding it. Combined by adding a background image of choice to the element, this gives the effect of having text inside the HTML structure, but shows an image instead.

HTML:

<h3>Revised Image Replacement</h3>

CSS:

h3 {
  text-indent: -9000px;
  overflow: hidden;
  background: url(image-to-show.png);
  height: 25px;
  }

Note: The usage of em causes some slight problem with old Version of Safari, so whenever possible (actually I can’t think of a situation when this isn’t possible) use px.

62.5%

This is the known number used for the base font size in browsers so that 1em equals 10px, mostly used in the practice of using em’s for setting font size yet still thinking in pixels to make things simpler. This method is first introduced and explained by Richard Rutter. The usage of em instead of pixel for font-size, of course, is good for many reasons, one of them is due to the fact that fonts set in px cannot be resized on a certain wretched browser. Back to this number, it is commonly used on the html element, for obvious reasons.

body {
  font-size:62.5%;
  }

A more advanced usage is 62.5%/1.6em, where the second number denotes the line height. This only works in font instead of font-size property:

body {
  font:62.5%/1.6em Arial, Helvetica, sans-serif;
  }

1%

This number, used on height property, is commonly used with the Holly Hack to fix the strange escaping floats bug in IE6. It is also featured on A List Apart’s 2004 article on creating horizontal drop down menu. Now if you’d excuse me, talking about IE bugs is obviously not my (or any other web designers’) happiest moment, so kindly peruse the links above to learn more about it.

101%

Like the number before it, 101% is also used on the height property. This is commonly used to deal with Firefox’s behavior of adding/hiding its vertical scrollbar depending on whether the page is taller than the viewport or not. This behavior gives the effect of inconsistencies between pages depending on their height. Using this technique, Firefox is forced to always display the scroll regardless of page height.

html {
  height: 101%;
  }

Nevertheless, it is said that this does not fix the same problem in Opera, and the more correct solution is this:

html {
  overflow-y: scroll;
  }

Another similar discussion about this can be found at Dzone Snippets.

Closing

These are what I can remember easily. It’s by no means a complete list, and I’d love to hear it if you know more about them!

6 Responses to “Unique Numbers in CSS”

  1. RhoVisions

    Hate to be a critic, but in your second example for 62.5%, you point out that fontsize/lineheight only works with the font property, not font-size, but your example uses font-size :-)

  2. thousands of free online flash games

    @”A more advanced usage is 62.5%/1.6em, where the second number denotes the line height. This only works in font instead of font-size property.”
    Why not use “font-size : 62.5%; line-height : 1.6em;”?

    @scrollbar
    I tend to prefer “overflow : auto;” to equalize IE’s and Firefox’s scrollbar appearances. IE is set to “overflow-y : scroll” by default, which I don’t approve of if there is nothing to scroll – it makes the window pointlessly smaller.

  3. SharpShark28

    Interesting concepts, I’ve seen quite a few of these before and just shrugged them off until now. I’m sure I’ll pay more attention to these sort of details in other’s scripts in the future ^_^

  4. Jono

    I found this list very useful and interesting. I have not seen this much but I have and helps explain so much as well as some good techniques.

  5. Joel Nichols

    Thanks for this mate. Just found it on StumbleUpon, now bookmarking your site.

    To be honest, while I probably won’t use most of this, since I do avoid using images for text etc and all that stuff, I suppose so I don’t have to use workarounds, I do think the trick to ensure that the scrollbar always shows up is a good one, and I’m now thinking I should implement that on my site for consistency reasons.

    Again,
    Thanks.

  6. Hafiz Rahman

    @Rhovisions: Oh, so true, that was a long unnoticed typo. Fixed!

Leave a Reply

Hello!

My name is Hafiz Rahman. I do standards-based web design and WordPress works. I'm open for new projects, and here's where you can contact me.