Swishy CSS3 navigation
- Knowledge needed: HTML, CSS
- Requires: Web editor, browser
- Project time: 30 minutes
- Download source files
- View demo
CSS3 offers so many possibilities to create cool rollovers on website menus. Dan Voyce explains how he created the effect on Graphite Design’s site
This article first appeared in issue 225 of .net magazine – the world's best-selling magazine for web designers and developers.
Here’s a simple way to spice up your navigation with just a sprinkle of CSS magic. This technique, which we used on www.graphitedesign.com, takes your typical list based navigation and adds a nice rollover effect. Obviously this is only visible on modern browsers, but it fails very gracefully for the others.
Advertisement
HTML: We’ll start by creating our buttons for the nav. We’ll wrap them in an unordered list like so:
CSS: Nothing too complex here. First we set the width we want the box around our nav (in this case the unordered list) then set the background colour.
- ul{
- width: 230px;
- background-color: #303728;
- }
Next we’ll style up the buttons. We’ll start by making them block level, so that the whole area reacts to the mouse. We’ll then set the width, padding and margin. Chuck some top and bottom padding so that the text sits centrally on the button, and some bottom margin so the buttons have some space. Finish by setting the text colour and applying a border to the bottom of the button.
- li a{
- display: block;
- width: 196px;
- padding: 3px 4px;
- margin: 5px 13px;
- color: #969E8D;
- border-bottom: 1px dotted #96BF1F;
- }
We’ll now set the rollover effect. A contrasting background colour, the text colour and the transition properties. We’re going to transition the background colour and set it to just 0.01 seconds. What’s the point of the transition if it’s that quick? You’ll see …
- li a:hover{
- background-color: #96C11F;
- color: #fff;
- -moz-transition: background-color 0.01s;
- -webkit-transition: background-color 0.01s;
- -o-transition: background-color 0.01s;
- transition: background-color 0.01s;
- }
If you go ahead and test this in your browser, this will give you an idea of how the older browsers will be seeing this. Now just a few lines of CSS to reward users for using a modern web browser … We’re going to add the same transition to the button, but we will make this transition slightly longer, at 0.3 seconds. We’ll also add the “ease-in” function to enhance the effect slightly.
- li a{
- display: block;
- color: #969E8D;
- width: 196px;
- padding: 3px 4px;
- margin: 5px 13px;
- border-bottom: 1px dotted #96BF1F;
- -moz-transition: background-color .3s ease-in;
- -webkit-transition: background-color .3s ease-in;
- -o-transition: background-color .3s ease-in;
- transition: background-color .3s ease-in;
- }
Voilà! Your beautiful rollover effect is finished!
Discover 101 CSS and Javascript tutorials at our sister site, Creative Bloq.




5 comments
Comment: 1
What is the argument that will now justify that behavior doesn't only mean behavior of the user? Presentation is now allowed to encroach on the behavior layer?
Not crappy about this, I've just never heard or read anything about why the modules for CSS3 are suddenly ok to break all the early rules?
Comment: 2
Comment: 3
Comment: 4
Comment: 5
Actually, I have a slightly different view from Dan: I don't think "animation" can be considered part of the "presentation" layer necessarily. It can be viewed as part of the behaviour layer.
But to answer your first question: The reason guys like Zeldman and Meyer don't say anything about this rule breaking is either that they believe as Dan does (that animation falls under the category of "presentation") or else they realize that hard-and-fast rules are not always reliable.
In other words, it doesn't matter if we're combining presentation and behaviour? The benefits to separating those two things are highly overrated. If it solves a problem, then do it. But if it doesn't, there's no point in being dogmatic about something that has very little tangible benefit.