CS3172 > Mats > examples/ > in-class/ > image trick

J. Blustein

Web-centric Computing

[Crs | Ann | Mats | Res]

In-class Examples

Image Trick

From Ten CSS Tricks you may not know by Trenton Moss at evolt.org.

It's always advisable to use regular HTML markup to display text, as opposed to an image. Doing so allows for a faster download speed and has accessibility benefits. However, if you've absolutely got your heart set on using a certain font and your site visitors are unlikely to have that font on their computers, then really you've got no choice but to use an image.

Say for example, you wanted the top heading of each page to be ‘Buy widgets’, as you're a widget seller and you'd like to be found for this phrase in the search engines. You're pretty set on it being an obscure font so you need to use an image:
<h1><img src="widget-image.gif" alt="Buy widgets" /></h1>

This is OK but there's strong evidence to suggest that search engines don't assign as much importance to alt text as they do real text (because so many webmasters use the alt text to cram in keywords). So, an alternative would be:
<h1><span>Buy widgets</span></h1>

Now, this obviously won't use your obscure font. To fix this problem place these commands in your CSS document:
h1
{
background: url(widget-image.gif) no-repeat;
}

h1 span
{
position: absolute;
left:-2000px;
}

The image, with your fancy font, will now display and the regular text will be safely out of the way, positioned 2000px to the left of the screen thanks to our CSS rule.

Demonstration

   <style type="text/css">
     p#replace   {background:url(funky-title.gif) no-repeat;
                  border: medium black dotted; width: 10ex}
     span.remove {position:absolute; left:-500em}
   </style>
   <!-- ... -->
   <p id="replace">
    <span class="remove" >Funky title<br />as text</span>
   </p>
  

Funky title
as text


This webpage uses valid XHTML 1.0, This webpage uses valid cascading style sheets