Scripting.com, with added CSS
One of the aims of this course is to show how relatively simple CSS can be used to make dramatic improvements to existing sites. Today, I’ll show how CSS can be used to reduce the amount of code needed for a small part of the design of Scripting.com.
Scripting.com presents the main blog entries as a series of paragraphs under a single header for each day. Here is a screenshot from today’s edition of the site:

The HTML behind the above screenshot is as follows:
<table width="400" cellspacing="0" cellpadding="3" border="0">
<tr bgcolor="#000000">
<td colspan="2"><font color="#F5F5F5"> <b>Monday, May 19,
2003</b></font></td>
<td valign="middle"><font color="#F5F5F5">
<b><a href="http://scriptingnews.userland.com/2003/05/19"
title="Permanent link to Scripting News archive for Monday, May 19, 2003.">
<img src="http://www.scripting.com/images/home/dailyLinkIcon.gif"
height="15" width="12" border="0"></a></b></font></td>
</tr>
<tr bgcolor="white">
<td width="5"> </td>
<td valign="top">
<br>
<a name="When:1:11:34PM">
<a href="http://icann.blog.us/2003/05/19.html#a1356">Bret Fausett</a>:
"It seems that an article making the rounds on Googlenews -- '.org Registry
Vanishes Into Thin Air' -- has no merit whatsoever.
<a href="http://scriptingnews.userland.com/2003/05/19#When:1:11:34PM"
title="Permanent link to this item in archive.">
<img src="http://www.scripting.com/images/2001/09/20/sharpPermaLink3.gif"
height="9" width="6" border="0"></a><br><br>
<a name="When:7:06:28AM">Guardian:
<a href="http://www.guardian.co.uk/online/comment/story/0,12449,959151,00.html">
The blog clog myth</a>.
<a href="http://scriptingnews.userland.com/2003/05/19#When:7:06:28AM"
title="Permanent link to this item in archive."><img
src="http://www.scripting.com/images/2001/09/20/sharpPermaLink3.gif"
height="9" width="6" border="0"></a><br><br>
I’ve added some extra line breaks to fit all of the code in the space available, but other than that it’s unmodified.
First, let’s reduce the above to the equivalent structural HTML, removing all of the presentational tags and adding in a few that might be useful. We’re also going to remove the images—they’ll still be there in the final version, but by using background images defined in the CSS we can save quite a lot of code.
<div class="entry">
<h2><a href="http://scriptingnews.userland.com/2003/05/19"
title="Permanent link to Scripting News archive for Monday, May 19, 2003."
class="permalink"><span>#</span></a>Monday, May 19, 2003</h2>
<p><a name="When:1:11:34PM"></a>
<a href="http://icann.blog.us/2003/05/19.html#a1356">Bret Fausett</a>:
"It seems that an article making the rounds on Googlenews -- '.org Registry
Vanishes Into Thin Air' -- has no merit whatsoever.
<a href="http://scriptingnews.userland.com/2003/05/19#When:1:11:34PM"
title="Permanent link to this item in archive." class="permalink"><span>#</span></a></p>
<p><a name="When:7:06:28AM"></a>Guardian:
<a href="http://www.guardian.co.uk/online/comment/story/0,12449,959151,00.html">
The blog clog myth</a>.
<a href="http://scriptingnews.userland.com/2003/05/19#When:7:06:28AM"
title="Permanent link to this item in archive." class="permalink"><span>#</span></a></p>
</div>
A few notes about the above. First, I’ve wrapped the whole entry in a div with the class “entry”. This means I can use descendant selectors to style the other elements in the block qithout needing to add additional classes. The only classes I’ve added are the permalink classes on the various permalinks—these will be used in a moment. Note also that the ’#’ signs in the permalinks have a span tag around them. The reason for this will become apparent in a moment. The table used for the header has been replaced with a structural h2 tag. Header tags should be applied in order, with the most important header on the page using h1 and the other headers being used for sub headers, sub-sub headers and so on. I’m assuming that h1 would be used for the site title (or logo) so I’m using h2 in this example.
The unstyled HTML can be seen here.
Now let’s style it with some CSS. First up, Scripting.com’s main layout table is 400 pixels wide. We can emulate that by applying a width to our <div>:
div.entry {
width: 400px;
}
Now let’s concentrate on the header. The date header on Scripting.com has a black background with white text. The text is bold, and is displayed at the same size as the body text of the rest of the site. In CSS, relative sizes can be specified using the em unit. 1em means 1 times the current text size, so we’ll set the font-size to 1em. Finally, the header is padded with about 3 pixels on every side. Here’s the CSS:
h2 {
background-color: black;
color: white;
font-size: 1em;
font-weight: bold;
padding: 3px;
}
See the result here. So far, so good—but what about the funky permalink? It’s an image, and it appears over on the right. Normally this would be a sign that we need a table, but we can achieve the same effect in CSS using the float property. Floats are actually one of the hardest properties to fully understand, but if you’ve ever used the align attribute on an image you should have a good idea of what they do. We will use float: right on the permalink to cause it to shfit as far right as possible, appearing next to the main header text.
h2 a.permalink {
float: right;
}
But what about the image? Here’s where things get tricky. We’re going to use a technique called the Fahrner Image Replacement. This is what the extra spans are for—we can apply a background image to the a element, then make the contents of the span invisible using display: none.
h2 a.permalink {
float: right;
width: 12px;
height: 15px;
background-image: url("dailyLinkIcon.gif");
}
h2 a.permalink span {
display: none;
}
And here’s the result.
That’s the header finished—now for the paragraphs. The first thing to notice is that they are indented approximately 15 pixels on the left hand side. This can be easily emulated in CSS using either a margin or padding—I’ll use padding, but margin would work just fine as well.
div.entry p {
padding-left: 15px;
}
We’re nearly finished—all that’s left to do now is the permalinks at the end of each paragraph. We will use FIR again, but this time the background image we are using is far smaller than the # sign it is replacing so we can take a slightly different approach:
div.entry p a.permalink {
background: #fff url("sharpPermaLink.gif") no-repeat center center;
text-decoration: none;
}
div.entry p a.permalink span {
visibility: hidden;
}
This time the permalink is made invisible using the visiblility property rather than being removed entirely—that way we don’t have to worry about adding a width or height to the link. The background image is placed using a shorthand property which allows all of the background properties to be specified at once—it should be relatively clear what the property does.
The end result can be seen here—viewed in a standards compliant browser (I tried it in Firebird, IE 5 and Opera 7) I think you’ll agree it’s an almost exact replica of the original, but with a great deal less markup. Times that saving by several dozen entries on a page and it really starts to add up.
Final note: this entry comes with apologies to Dave Winer of scripting.com, who has not been consulted prior to the publication of the article.
fyi: the individual entry permalinks don't appear in Mac IE 5, whereas the daily permalink in the h2 does
headless - 20th May 2003 00:15 - #
Matt - 20th May 2003 00:15 - #
Rats, I knew I should have tried to see it in IE5 for Mac befoer I posted it. Any idea for a fix? It might just be a case of adding a width to the link element...
The reason I didn't replace the
<a name>elements with ids is that Dave uses colons in his link targets, and colons aren't valid in id attributes.Simon Willison - 20th May 2003 00:21 - #
Could someone with Mac IE5 confirm if this slightly altered version works?
Simon Willison - 20th May 2003 00:27 - #
Dave Winer - 20th May 2003 00:46 - #
Bill Humphries - 20th May 2003 00:58 - #
The
visibility: hiddenbit is to make the actual # vanish so it won't overlap the purple # background image. I've just changed the altered version to usedisplay: noneinstead - any luck?Simon Willison - 20th May 2003 01:09 - #
Chris Chapman - 20th May 2003 01:49 - #
Chris Chapman - 20th May 2003 01:52 - #
Paul Howson - 20th May 2003 01:55 - #
rick - 20th May 2003 02:07 - #
Simon Willison - 20th May 2003 02:09 - #
Why not dispense with the item permalink image, and just style the pound sign? This seems to get pretty close to the original, and doesn't require the nested span. Works in MSIE5/mac.
div.entry p a.permalink { font-size: smaller; text-decoration: none; color: #909; }Michael Zajac - 20th May 2003 02:10 - #
Sebastian Delmont - 20th May 2003 03:31 - #
I humbly suggest that the image replacement technique is a pain simply in order to give three people on Palm Pilots et al a # symbol rather than alt text. This sort of thing is the reason some people don't use css. It really should not be directed to beginners as part of a course, but only to css hairshirt martyrs. -And it mucks up your code anyway.
Use the image, or a nice unicode symbol if you want to match colors on stylesheets?
And if really clever and naughty, use & # 0 1 5 7; in a span with a class styling of webdings/marlett. 95% of users will get a nice page symbol, while moz/etc. defaults to a ? mark. I was thinking of a css trick to rectify that using content and display *without* referring to parents, but doesn't work that I can see. Can do it referring to parents, though. I'll leave it with you, but seems more trouble than its worth.
stylo~ - 20th May 2003 04:33 - #
I agree, meaningful images should not be relegated to backgrounds. The { display: none; } hack is exactly that - a hack that is not forwards compatible.
A conformant CSS implementation should not render anything that has { display: none; } applied to it - and that includes aural browsers like JAWS. The fact that JAWS does not is a bug that may or may not be rectified in the next release.
Please, if the alt attribute is not good enough for you, use <object> elements and live with their drawbacks.
Jim - 20th May 2003 11:04 - #
The Mac test doesn't seem to work in Win/Firebird, so I don't think that's a good idea...
Jemaleddin - 20th May 2003 15:17 - #
I'd've recommended against the Farhner method for something like this; although I've had good luck with it in a few projects, it can be damned tricky to get right. besides, the second use is just to replace text with a graphic of that same text, where there isn't a lot of visual distinction between the two.
I think it's worthy to note, though, that all of this is fussing over one fairly minor element of the reworking - the spectacular part is how simple it is to do EVERYTHING ELSE...making the date header semantically meaningful while keeping the same look, giving the "right" spacing to the text, abstracting the look from the text, lightening up the page weight.
it's maybe not that obvious how much code is saved, given the example, but I imagine spread over the site as a whole, it could be pretty substantial.
Nice work.
Elaine - 20th May 2003 17:05 - #
stylo~ - 20th May 2003 17:48 - #
Ryan - 20th May 2003 20:17 - #
Paul - 28th May 2003 14:26 - #
Seems like most of the posts concentrated on Fahrner Image Replacement and not the important stuff. Perhaps a better explanation of what you were trying to achieve is necessary.
alanjstr - 29th May 2003 14:44 - #
Remember when some webdesigners added text colored the same as the page background to "optimize" the content for google?
(and got excluded from the index because the googlebot "understood" the font tag attributes)
FIR easily could be used for the same purpose, replacing indexable text by an invisible gif, for instants.)
Is there any information about the probability of getting trouble with google that way?
(lately google has reportedly excluded sites from the index because of certain redirection javascripts, obviously the're beginning to take DHTML into account)
Marek Moehling - 10th September 2003 21:15 - #
tetras - 2nd July 2004 09:10 - #