Skip to main content

CSS Rollover Buttons

My work place tasked me with trying to be as purist as possible with HTML and CSS for an interface their web application. Without any formal web development background, I have always been one of those who intermingle the code and have presentation within the HTML.

I think it came out pretty well. I separated design from content in addition to separating the dynamics. Mind you, I even separated out all the server talk and taught myself some XML/XSL in the process. After all that I learnt Ajax, without realising it.

One little nugget from all this is creating a pure CSS rollover button for the menu, without Javascript. There's alot of websites that help out in this, but they are not "pure" as they involve you including the image in the HTML and to perform some shifting with the image. Hell no to that.

My way leaves out the usual ul tag and dives straight into the anchor (a) tag as it should be:

a href="links.htm" id="linksButton" title="Links">

Embedded within the anchor is a blank image, which can be a 1-pixel transparent GIF. The image size but be set due to browsers behaving differently.

img src="assets/buttons/blank.gif" weight="45" height="45" title="Links" />

That's fine enough, but the magic is the assigned id for each anchor which must, obviously, be unique. The id is used within the CSS. First for the initial UP state:

#linksButton img {
background-image:url('./buttons/linksBtn_up.gif');
background-repeat:no-repeat;
width:45px;
height:45px;
}

Here I set the desired image icon as the background image and ensuring that it does not repeat, for safety measures. Due to browser quirks you need to set the size of the image tag here as well.

The rollover is set using the same id:
#linksButton:hover img {
background-image:url('./buttons/linkBtn_down.gif');
}


And that's it. You will have a pure CSS rollover, without any manky HTML and Javascript. This is also accessible in case you want to fulfill the WAI Triple-A Compliant guidelines by the W3C.

Beacuse it's easy, I am now a fan of puritan coding of HTML and CSS. Spread the love.

Comments

Popular posts from this blog

Will we ever get Dreamweaver on Linux?

I have been a keen user of Ubuntu for some time now, and have to admit that this distribution has improved by leaps and bounds. However, what I do honestly miss on windows is... Dreamweaver. Some will swear by it, some will swear at the mention of its name. But for me, it is a very powerful tool when used correctly. Such a very popular and powerful web design and development tool is available on Windows and Mac OS, but not Linux. If you would just consider, Adobe has already supported the platform with their Adobe Reader and Adobe Air. Why is Dreamweaver not available? It just seems that there's not such a huge call for it so they just don't see the need to put in an effort for another platform. One chap at Adobe suggested to use Dreameaver under wine , but it doesn't work so great. There is an alternative to wine which, according to a user on the forums, works flawlessly - Crossover . However, the former option is not free, so some may be dissuaded where they woul...

Image links in IE8

While mocking up a design page for a colleague's website I came across a new browser behaviour in IE8. This was the first time I used IE8 as my main development browser, due to the fact that the PC was re-installed and high-priority updates were applied. Surprisingly Microsoft branded IE8 as a priority update. Anyway, back to what I was talking about, here's a sample: <a href="index.htm"><img src="logo2.jpg" alt="Logo" title="Logo" /></a> As you can tell, this is using an image for a link, where it behaves well in Chrome, Firefox and in the previous versions of IE (7 and older). But in IE8, this is not the case as the DOM does not treat the image as innerText, only as a child node. As a result, everytime I hover the mouse over this image, an extra gap would come from nowhere shifting any relatively positioned elements. To fix this I explicitly added a space (&nbsp;), so the line would look like: <a href="inde...

Morange

I have Nokia N70 on the T-Mobile network, and it's really impressive. Labelled as a Smart Phone, it is a bit like a cross between a PDA and a normal mobile phone. Boy does it live to expectations. You can check your emails (POP/IMAP), read Word documents, Spreadsheets, view To-Do lists and so much more. Now I sound like a sales man. The most handy aspect is the ability of downloading "mini-applications". What I really want to talk about is a mini-application that let's you treat your phone as if it is an iPhone. Yes, you heard me. An iPhone. Why bother with a mobile phone that costs you an arm and a leg for a phone with a pretty interface. Well, I would, but I would not like to risk my limbs in the process. This application is called Morange ( http://www.morange.com ) and is feature rich. You can receive an "push" POP/IMAP emails. It even works with GMail. All you have to do is enable POP3 in the settings (Follow this link to find out how http://ma...