If you’ve visited the site at all in the last 2 months you would have most likely noticed our new Catfish book banners hugging the page bottom from time-to-time. Since they launched, we’ve been receiving around 3-4 comments/email per week asking how it’s done. So, rather than answering each email individually, we thought this is might be a good place to walk you through the finer points — for those interested.

Of course, the code has always been there in public view, but if you have ever trawled through it you’ll know that SitePoint has a lot of deeply interwoven CSS and scripting, so we’re going to break out a streamlined version of the Catfish into a holding tank.
Firstly, some basics. Yes, the Catfish is ‘nought but a ‘garden-variety’ DIV with some CSS and a bit of JavaScript to make it shimmey — no crazy technology required.
The first ‘proof of concept’ was hacked together with no animation and pure CSS. At that stage the idea was just get a sense of how it looked on the page, so we set it up quickly using the ‘position:fixed’ CSS property and a little JavaScript to make it go away when asked.
The DIV was slotted in at the end, just before the closing BODY tag. We also padded the bottom of the HTML element to make sure the footer could never be obscured by the Catfish.
catfish.css
#catfish {
position:fixed;
bottom:0;
background:transparent url(images/catfish-tile.gif) repeat-x left bottom;
padding:0;
height: 79px; /* includes transparent part */
cursor: pointer;
margin: 0;
width:100%;
}
html {
padding:0 0 58px 0; /* 58px = height of the opaque part of the Catfish */
}
The contents of ‘DIV#catfish’ are totally up to you. You could conceivably use it for navigation, site announcements, log-in panels or a multitude of things. Space is obviously limited, so keeping things relatively simple is recommended.
After demoing it with some of the guys here, we all agreed the idea had some legs. At this point the major issue became the small matter that it didn’t work at all in Internet Explorer. If you’re viewing the demo in IE you’ll see that the DIV is behaving exactly as if it were ‘position: static‘ (the default). Our big challenge was to make IE play ‘nice-like’ — and that’s what I’ll be concentrating on here.
There has already been plenty of good work on the ‘fixed’ problem from (amongst others) Stu Nicholls, Simon Jessey and Petr ‘Pixy’ Stanicek. Although each differed in the fine print, they generally seemed to agree on some main tenets — position the ‘wannabe fixed DIV’ using ‘position:absolute’ and then wrap everything else in a ‘position:relative’ DIV to keep them apart. Sounded like a good place to start.
We also made another decision at this point. Since FireFox, Opera and Safari do a dandy job with the W3C standard ‘position:fixed’, why throw extra markup at them? — only IE would be getting the extra markup.
In this ’sandbox’ version, I’m going to attach our IE-specific styles and scripting using ‘conditional comments’, although we actually use ‘object sniffing’ to target IE on the live version. I think conditional comments are great way to go at the moment as they invoke a purpose-built function within IE, rather than relying on fixable and likely transient browser bugs. With IE7 on the horizon, relying on bugs is more dangerous occupation than ever before.
Conditional Comments
<!--[if IE]>
<link rel="stylesheet" href="IEhack.css" type="text/css" />
<![endif]-->
The above markup will allow us to deliver different styles to IE only. Other browsers will read it as a ‘bog standard’ HTML comment, which means that HTML validators will also find it wholesome and satisfying. If IE7 supports ‘position:fixed’, it will be trivial to change the comment to make it only target IE6 and older ( e.g. ‘<!--[if lt IE 7]> ...’ if less than IE7).
So, what extra CSS should we send IE ?
Not a great deal. We need to switch Catfish’s positioning to ‘absolute’, set it’s z-index to ‘100’ to keep it at the front, and set it’s overflow to ‘hidden’.
IEhack.css
#catfish {
position: absolute;
z-index: 100;
overflow: hidden;
}
Now we have our Catfish positioned correctly — that is, until we try to scroll, at which point it trundles off up the page. The problem is the browser calculates ‘bottom:0’ as the exact point where the bottom of the viewport overlaps the BODY — when the BODY scrolls, that point moves with it.
So, in theory, we can fix this problem by taking the rather drastic-sounding action of forcibly preventing our BODY from scrolling under any circumstances. Using ‘overflow:hidden’ and ‘height:100%' we can force the viewport, the HTML element and the BODY element to aquire exactly the same dimensions. No scrolling means the Catfish stays put.
IEhack.css
html, body {
height:100%;
overflow: hidden;
width:auto;
}
Of course, this minor victory has been regretably soured by the fact that we now have no way of accessing any content outside our viewport. It’s now that we call on that wrapper DIV mentioned in other methods. I’ve called it ‘#zip’ as we’re ‘zipping up’ all the non-catfish content up into it. In the markup it looks something like this.
catfish-ie.php
<body>
<div id="zip">
<div id="masthead...
...</div>...<!-- close zip -->
<div id="catfish">...
...</div><!-- close catfish-->
</body>
This new ‘div#zip’ is now bulging with most of the content on the page, so if we set it’s overflow to ‘auto’, it will only too happy to give us some nice new scrollbars. These scrollbars will be almost indistinguishable from BODY’s own default scrollbars. The CSS for this DIV is pretty unremarkable.
IEhack.css
div#zip {
width: 100%;
padding:0;
margin:0;
height: 100%;
overflow: auto;
position: relative;
}
Ok, so now IE is playing nice and giving a fine imitation of a browser who knows what fixed positioning is,… just as long as we give it that extra DIV to work with.
But, as I said above, why burden better browsers with stuff they don’t use? It’s a DIV that is more likely to hinder than help them, so let’s inject it only into IE using the DOM.
We’re going to add a new function called ‘wrapFish’. The script goes a little like this.
catfish.js
function wrapFish() {
var catfish = document.getElementById('catfish');
// pick the catfish out of the tree
var subelements = [];
for (var i = 0; i < document.body.childNodes.length; i++) {
subelements[i] = document.body.childNodes[i];
}
// write everything else to an array
var zip = document.createElement('div');
// Create the outer-most div (zip)
zip.id = 'zip';
// give it an ID of 'zip'
for (var i = 0; i < subelements.length; i++) {
zip.appendChild(subelements[i]);
// pop everything else inside the new DIV
}
document.body.appendChild(zip);
// add the major div back to the doc
document.body.appendChild(catfish);
// add the Catfish after the div#zip
}
window.onload = wrapFish;
// run that function!
The comments give you the blow-by-blow on what it’s doing, but, in short, it:
- takes the catfish out of the document,
- creates the new
DIV#zip, - copies everything else into the new DIV,
- attaches that DIV to the document, and
- tacks the catfish back on the end
Now all we need to do is call this script from inside our conditional comment. IE now has the extra ‘leg-up’ it needs and all the other little browsers will be none the wiser.
Conditional Comments
<!--[if IE]>
<link rel="stylesheet" href="IEhack.css" type="text/css" />
<script type="text/javascript" src="catfish.js">
<![endif]-->
So, there you have it. I’ve left a red dashed border on ‘div#zip’ to demonstrate that only IE is rendering that extra DIV. We’ve patched IE without messing with anyone else.
So, is that all you have to know to get a Catfish-like system running ?
Not quite. It’s more than likely you only want to run the Catfish on certain pages, at certain times, so we need an intelligent system that knows if and when to inject the Catfish via the DOM. It would also be nice to be able to select from a library of different banners.
Tom will take tackle these and other exciting problems in Part II — coming soon.





October 18th, 2005 at 4:48 am
Great write-up, it’s just a pity that we have to hack this code around to get it to work in IE.
Rob.
October 18th, 2005 at 8:49 am
Thanks for the write up. I’m one of the want-to-know-how-they-did -it-BUT-don’t-want-to-sift-through-the-code (wtkhtdiBdwtsttc!?!) people.
And as a bonus you provide the hack to get it to work in IE! Usually with something like this I would leave it as a non-critical part of the site and serve it to standard compliant browsers only.
October 18th, 2005 at 9:27 am
Why not just place the & tags inside a conditional comment? Isn’t this more efficient?
Btw, typo on the last code snippet. Should display a element instead of the stylesheet.
October 18th, 2005 at 9:30 am
Opps. not aware comment does not convert markup into html entities. Should be:
Why not just place the <zip> & </zip> tags inside a conditional comment? Isn’t this more efficient?
Btw, typo on the last code snippet. Should display a <script> element instead of the stylesheet.
October 18th, 2005 at 11:29 am
Yeah this was cool. Definitely looking forward to Part II… although I have to admit I did know it had a name… and since it does I think they should leave the fish in the banner…. =)
October 18th, 2005 at 11:51 am
Thanks!!!!!
Looking forward to reading the all the parts… GET TO WORK! ;)
October 18th, 2005 at 12:49 pm
I’ve been trying to mimic your catfish since the day i saw it.
I was having a problem where it flickers in IE and was stuck in it. I couldn’t figure out the problem was.
Thank You for sharing this code.
I’m a beginner in CSS and this tutorial really help s me understand more about it.
Thanks again!
October 18th, 2005 at 12:55 pm
Honestly, I think that this feature is very annoying - I’d rather see a standard ad.
October 18th, 2005 at 3:09 pm
I personally disagree with this, to me its a less obtrusive method than most in the face advertising on sites. Thanks for the blog.
October 18th, 2005 at 3:51 pm
I agree. This is much less in the way. By locking it to the botom of the screen its lack of motion compared to the rest of the site draws your eys in. And as a bonus you give the user the option to never have to see it agin if they so choose. I look forward to trying this in an upcoming project.
October 18th, 2005 at 7:45 pm
Actually, it was in the code, but I had bolded it to show the code change, and the groovy new syntax highlighter choked on it. Fixed now.
October 18th, 2005 at 7:52 pm
Btw, the syntax highlighter chokes on Opera 8.5 too.
October 18th, 2005 at 8:07 pm
Thanks for that, ChiliJ. The highlighting is a ‘DOM-injected’ too, but I’m pretty surprised Opera has problems with it. We’ll fix that.
October 18th, 2005 at 9:59 pm
We use a single ‘header include’, but many different layout templates (article, blog list, post, categories, etc), so deploying something across the whole site is easiest to manage via the header.
In part 2 we walk-thru how to inject the whole catfish div via the DOM, so your template code can remain untouched.
October 19th, 2005 at 5:28 am
Nice code!
October 20th, 2005 at 2:12 pm
Great, I have wanted to know how to do this for a while now.
I will start to use this after part 2 hopefully, if you include how to close it and make it not show again. :)
October 21st, 2005 at 12:53 am
[…] In Part 1, Alex introduced our implementation of Catfish ads and demonstrated how we managed to have them appear at the bottom of the window, in all browsers, without jerky motion while scrolling. […]
October 21st, 2005 at 12:55 am
[…] SitePoint Blogs » The Catfish—Part 1 (tags: CSS Design) […]
October 27th, 2005 at 3:42 pm
Sweet code!
Works a treat!
Would it be too difficult to have four fixed ’s (left, right, top, bottom) on the screen? You could create something really atmospheric with this in place!
October 27th, 2005 at 9:29 pm
Haven’t tried that, Tryst, but I can’t imagine why it would be much harder to get working.
I would be cautious though. Users don’t like change as a rule, so it’s best to make small, incremental changes over time, rather than unleashing major changes to the way they’ve always used your site. That dosesn’t mean never change anything — it just means be prepared for backlash if you make fundamental changes.
October 31st, 2005 at 5:31 pm
[…] The CSS/XHTML design is integrated into a WordPress theme (obviously) and uses a modification of the catfish technique from SitePoint (http://www.sitepoint.com/blogs/2005/10/18/the-catfish-part-1/) to get the top and bottom of the image fixed in Internet Explorer as well as Firefox. […]
January 1st, 2006 at 8:37 pm
There’s a problem in IE 6 with this code when a horizontal scroll bar is present it shows behind the transparent part of the div and makes horizontal scrolling impossible.
April 25th, 2006 at 3:25 pm
[…] Finally Paul answers some user submitted questions relating to the use of frames and fixed position screen elements. For more information on fixed positioning elements read this great tutorial at sitepoint.com. […]
May 9th, 2006 at 9:31 am
[…] He also highlights an article from last year at Sitepoint that utilises Javascript to fix IE by adding the extra container to the content, just so that the nice browsers don’t have to get this extra little <div> markup. […]
June 26th, 2006 at 5:26 pm
Very Very nice information here… Thanks
June 27th, 2006 at 12:37 pm
Very needed information found here, thank you for your work
June 29th, 2006 at 6:00 am
looking for information and found it at this great site.
June 29th, 2006 at 8:50 pm
Great tutorial, really…
But how to make it appear and disappear scrolling like the one you are using in SitePoint.com web site?
July 9th, 2006 at 2:09 am
[…] Now for the tricky part. The key to this whole solution is to keep the div at the bottom of the window, not the page. We want to make sure the user always sees the indicator. The solution is actually a slightly modified version of SitePoint’s Catfish Script. If you’ve been to their site lately, you have probably noticed their cool little ads that appear at the bottom of the screen after a few seconds. Well, they call that a catfish, which makes complete sense to me now, but was of no use to me when I was searching for their solution. As usual, I’m going to summarize and borrow from their article, but I suggest you stop reading now and read their full post before continuing. Also, just to be perfectly clear, I’m not trying to steal their work, or claim it as my own, I’m just using it to achieve our goal (don’t need people thinking I’m plaigarizing!). […]
September 8th, 2006 at 6:54 pm
Wow! thats Great.. very useful information..
September 12th, 2006 at 10:43 am
Link for catfish.css does not work anymore:
http://www.sitepoint.com/blogs/2005/10/18/the-catfish-part-1/catfish.css
Please fix it. Thanks!
September 12th, 2006 at 12:22 pm
The link abovewas actually wrong - it should be:
http://www.sitepoint.com/examples/catfish/catfish.css
Fixed.
September 14th, 2006 at 5:32 am
This was a great help. It’s going to save me a lot of headaches using frames. I know I’m probably missing something really silly, but I can’t seem to make the scrollbar stay pinned to the bottom right hand corner of the screen.
I am getting a scroll bar, but it is at a fixed height and I cannot see it’s bottom. Any suggestions on how to get it to resize with the browser window would be greatly appreciated. Thank you.
September 14th, 2006 at 3:00 pm
Roadboss, it’s kinda difficult to bugfix without seeing the code in action. Can you put it online somewhere?
October 12th, 2006 at 8:26 am
I agree, it is a nice feature and it gives you the option to close it. It is a nice way to squeeze in advertisement or information. Thanks for the blog. I will use it for my things.
November 16th, 2006 at 2:03 am
We used this here: [http://www.growfruitandveg.co.uk] what do you think?
November 16th, 2006 at 9:21 am
Nice work!
Impressively executed.
January 13th, 2007 at 6:45 am
WOW! Just what I was looking for, but being an HTML newbie, can someone spell out for me in detail what includes to download, where to put them on my server, and then what html code to use to display the castfish type banner?
Much thanks!
January 13th, 2007 at 11:39 pm
Can someone please help me and take a look at http://www.softtouchlaser.com/index2.html and tell me (post here) why it’s not working for me? THANKS!
March 26th, 2007 at 11:21 pm
While I was playing around with this I noticed that if you use:
to center your container, then you need to add:
to the body in IEHack.css for the suckerfish to align properly. Otherwise the div starts half way across the page.
January 21st, 2008 at 9:05 pm
Yo Sitepoint, thanks for sharing.
February 12th, 2008 at 1:55 am
hey guys
come across this website - www.instantslideup.com
It makes catfish ad creation dead simple. Check them out
February 12th, 2008 at 1:59 am
Yo man. .. thanks a lot !
July 31st, 2008 at 3:33 am
In the IE example. Try clicking the bottom scroll bar button. F%&# IE.