More permanent stuff at

03 June 2009

Stupid CSS Tricks

I have recently made the committment to wrap my head around CSS.  In other words,  I'm tired of guessing.  Part of this experience will have me documenting my discoveries on this blog.

Here is what I learned today:

Pseudo-classes aren't just for anchors.

That's right.  You're supposed to be able to apply standard pseudo-classes to just about any selector you can come up with.  This means you don't to rely on a) jQuery or b) onmouseover/onmouseout to do the hover-effect work for you, so long as you can do it all in CSS.

Here is some sample code:

        <style type="text/css">
        /* a class for an element */
        span.my_hover_class:hover {

        /* attached to nested elements. */
        div > div > span > span:hover {
        <span class="my_hover_class">Should hover red (span.my_hover_class)</span>
            Should not have hover effect.
                <span><span>Should hover green (div>div>span>span)<span><span>


I would like to show the effect here, but the Blogger editor insists on tidying up my pasted HTML.  I uploaded the file to my website.

This example was tested in Firefox and Safari on a Mac.