The Catfish – Part 1

Tweet

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.

The Catfish waits..

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://wv4.co.uk/ roblewis100

    Great write-up, it’s just a pity that we have to hack this code around to get it to work in IE.

    Rob.

  • http://boyohazard.net Octal

    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.

  • ChiliJ

    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.

  • ChiliJ

    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.

  • Sojan80

    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…. =)

  • Anonymously

    Thanks!!!!!

    Looking forward to reading the all the parts… GET TO WORK! ;)

  • Andre W

    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!

  • dhtml12345

    Honestly, I think that this feature is very annoying – I’d rather see a standard ad.

  • potsed

    Honestly, I think that this feature is very annoying—I’d rather see a standard ad.

    I personally disagree with this, to me its a less obtrusive method than most in the face advertising on sites. Thanks for the blog.

  • hdsol

    I personally disagree with this, to me its a less obtrusive method than most in the face advertising on sites. Thanks for the blog.

    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.

  • http://www.sitepoint.com AlexW

    Btw, typo on the last code snippet. Should display a element instead of the stylesheet.

    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.

  • ChiliJ

    Btw, the syntax highlighter chokes on Opera 8.5 too.

  • http://www.sitepoint.com AlexW

    Btw, the syntax highlighter chokes on Opera 8.5 too.

    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.

  • http://www.sitepoint.com AlexW

    Why not just place the & tags inside a conditional comment? Isn’t this more efficient?

    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.

  • http://www.errewf.it RaS!

    Nice code!

  • http://teenageforums.com SHANE-D-PAIN

    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. :)

  • Pingback: SitePoint Blogs » Set Us Up The Catfish—Part 2: SlideMe

  • Pingback: 冰古 » links for 2005-10-20

  • Tryst

    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!

  • http://www.sitepoint.com AlexW

    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!

    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.

  • Pingback: CSS Reboot - Launched - Life’s a Bench

  • http://www.bcimport.com/ Transition

    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.

  • Pingback: Practical Web Design magazine » Blog Archive » *New podcast* Spice up your site and generate repeat traffic

  • Pingback: Fixed positioning within Internet Explorer 6 : Weblog @ Stuart Homfray

  • cartere

    Very Very nice information here… Thanks

  • wincel

    Very needed information found here, thank you for your work

  • gayle

    looking for information and found it at this great site.

  • bisiacaria.com

    Great tutorial, really…
    But how to make it appear and disappear scrolling like the one you are using in SitePoint.com web site?

  • Pingback: Gen-X-Design | Ian Selby » Ajax Activity Indicators - Make Them Global and Unobtrusive

  • nikhilkumar80

    Wow! thats Great.. very useful information..

  • stickycarrots

    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!

  • http://www.sitepoint.com AlexW

    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!

    The link abovewas actually wrong – it should be:
    http://www.sitepoint.com/examples/catfish/catfish.css

    Fixed.

  • Road Boss

    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.

  • http://www.sitepoint.com AlexW

    Roadboss, it’s kinda difficult to bugfix without seeing the code in action. Can you put it online somewhere?

  • Kenpo

    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.

  • Anonymous

    We used this here: [http://www.growfruitandveg.co.uk] what do you think?

  • http://www.sitepoint.com AlexW

    We used this here: [http://www.growfruitandveg.co.uk] what do you think?

    Nice work!

    Impressively executed.

  • Bob

    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!

  • Bob

    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!

  • http://www.kjimn.com CHeeSeBLiND

    While I was playing around with this I noticed that if you use:

    body
    {
      text-align: center;
    }
    
    div#container
    {
      margin: 0 auto;
      text-align: left;
    }

    to center your container, then you need to add:

    text-align: left

    to the body in IEHack.css for the suckerfish to align properly. Otherwise the div starts half way across the page.

  • Freek

    Yo Sitepoint, thanks for sharing.

  • robbrend

    hey guys

    come across this website – www.instantslideup.com

    It makes catfish ad creation dead simple. Check them out

  • robbrend

    Yo man. .. thanks a lot !

  • ProDev

    In the IE example. Try clicking the bottom scroll bar button. F%&# IE.

  • centinel3

    I love the script, though am running into an issue with the background of my website. When I have the catfish script working on the index page, it hinders the background from scrolling. So when the catfish pops up, the content on the index page scrolls, but not the background image. Any help would be greatly appreciated.

    you can see it in action here: http://www.campbellcanes.com/

    Chris