The concept behind this article came a few days ago when I was chatting in the shoutbox here. I was thinking about whether we could include fonts (.ttf) on server and with the help of some frameworks we could display text in browser. Unfortunately, after much chatting and pleasing google (it just didn't show any mercy that day), I finally understood its not possible.
I actually wanted headings (h1, h2... you get it?) in a webpage to use a nice font which I have. The problem is visitors won't have that font installed in thier system. The second idea was to convert those headings to gif. The alt attribute to the image in case of text browser or browser disabling images would not look good (it's a heading right?). So, what I found at last was a technique using css. I am explaining that in detail here so you save time luring google to throw you the best
Ok, I assume you know basic css. The idea behind this is to display the image (created using unknown fonts) in the place of text. In addition, you want the text to be present behind the image so that it becomes visible only if the image fails to download or the user disabled images shown in his browser. The trick is, I placed an empty span inside a heading. I relatively positioned the heading so child p elements can be absolutely positioned relative to it. I assigned a background image to the span and absolutely positioned it in front of the text in the heading element. I sized the span and the heading to accomodate exact size of the background image. The end product is that the background image of the span covers the text in the heading, and if the image fails to download, the styled css text is shown in the heading. Now!!! Is there any better way?
So, Here's the HTML and yes it is legal w3c:
Quote
<h1 id="h1" >Heading to display when image fails<span></span></h1>
Quote
#h1 { position:relative; width:250px; height:70px; overflow:hidden; }
#h1 span { position:absolute; width:250px; height:70px; left:0; top:0; background:url("heading1.jpg") no-repeat; }
/* Simple! Isn't it? */
#h1 span { position:absolute; width:250px; height:70px; left:0; top:0; background:url("heading1.jpg") no-repeat; }
/* Simple! Isn't it? */
Just remember the width and height should be same for both, replace those numbers with yours. And for the validity of code, I only processed in my brain while writing, please post any mistakes if any. I would put the end result if you want and if I too get the time.
Attached Files
Edited by pasten, 21 September 2008 - 11:53 AM.


















