Archive for November, 2007

A simple guide to CSS positioning

Tuesday, November 27th, 2007

BarelyFitz Design has written up a very nice explainer on how to position elements through CSS. Their step four is probably the one that is most easily forgotten: Combining position:relative with position:absolute. In a nutshell, setting a ‘relative’ position on the container element will allow you to position subelements absolutely within that element. And the explainer goes to show how this method can be used for two- or three-column layouts that rival the traditional methods of floating elements.

Cross-browser hovering [Howto]

Tuesday, November 27th, 2007

The CSS :hover pseudoclass is nice and allows you to easily modify behaviour on web page elements when user moves her mouse, and pretty much every web programmer has written something like this in her stylesheet:
a {text-decoration:none;}
a:hover {text-decoration:underline;}

And all these programmers have found that CSS hovering doesn’t work well in Internet Explorer when it isn’t done on a straightforward link. For example this will not have the desired effect:
a:hover div {border-color:red;}
div:hover {font-weight:bold;}

With some light scripting however you can make you page work this way. Basically, make sure that an elements receives the class name .hover when the mouse moves over it; and that the class is removed when the mouse moves away. If you enable such behaviour you’ll see that this works on across different browsers:
a.hover div {border-color:red;}
div.hover {font-weight:bold;}

The code to set up this magic is simpe (example in Prototype)…

function makeHoverable(el) {
  // Setup mouseover and mouseout events to add and remove the 'hover' class
  Event.observe(el, 'mouseover', function(){el.addClassName('hover');});
  Event.observe(el, 'mouseout', function(){el.removeClassName('hover');});
}
// When the document is ready
document.observe('dom:loaded', function() {
// ... we loop through all element with the class name 'hoverable'
$$('.hoverable').each(makeHoverable);
// ... and all links to make them act nicely
$$('a').each(makeHoverable);
});

Now finally add the class name hoverable to all the elements you want to use this new behaviour for:

<div class="hoverable">
  I'll be bold when you move the mouse over me.
</div>

ยป See it in action

If you plan to use this method along with complex CSS positioning you should be aware that mouseover and mouseout sometimes behaves strangely when applied to two elements on top of each other. In this case, you could try to combine mouseenter and mouseleave for robust IE support with CSS pseudo-classes to support everything else. It might work.

Introducing Refresh for the web practitioners

Tuesday, November 27th, 2007

When we started building 23 in 2004 the advanced javascripting of today was still in its infancy, and accordingly much of what we did back then was based on a nice mix of server-side scripting, HTML and CSS with very little client scripting in the mix. Of course, time have changed dramatically since then, and along with every other web programmer I’ve become a practitioner of this web 2.0 and ajaxy thing. This blog is a stab at documenting the experiences made along the way.

In the first few posts I’m going to kick things off by writing a few posts about the simple tools the practitioner should always have in her toolkit: A good basic stylesheet that does a bit of resetting and specifies some commonly used classes — and a few JavaScript tricks that just makes everything easier. And frankly I’m probably going to make a few detours along the way.

A small disclaimer is probably warranted: By default and by habit I’m usually building on Prototype and Scriptaculous. To be honest this wasn’t a concious choice when I made it, but never really felt the need to force a change in habit.