How To Create a Sweet CSS3 Vertical Navigation

Today we’ll be creating a beautiful vertical CSS3 navigation, without the use of images. Basically we’ll display a circle with an icon in the center. When the user hovers over the circle, it expands and shows a short description.

Now you may be wondering: how are we going to display an icon without images? Well, Drew Wilson’s (fabulous) Pictos font makes this quite simple. Unfortunately (but understandably) it’s not free, you can purchase is for $49 here. Luckily, there are other icon fonts out there like iconic, which you can use for free.

I should also mention that this has been tested using Webkit browsers only (Safari, Chrome). It may work in other browsers but probably with limited support. Check out the demo here!

So, let’s get started!

HTML

As always, we’ll begin with the html. Paste the following code into your document.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Sweet CSS3 Vertical Navigation</title>
	<link rel="stylesheet" type="text/css" href="reset.css"/>
	<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
  
</body> 
</html>

As you can see we’re using the new html5 doctype. Both the html and the meta charset tags been simplified vastly in the new doctype. You can combine both stylesheets into one, but I think having two separate documents is cleaner. If you use two, make sure the reset is listed first, so that your normal stylesheet can overwrite the default values specified in the reset.

Next add this code.

<div id="nav">
    <a href="#" class="active"><span class="pictos">p</span><span class="hidden">My rants</span></a>
    <a href="#"><span class="pictos">i</span><span class="hidden">All about me</span></a>
    <a href="#"><span class="pictos">o</span><span class="hidden">My projects</span></a>
    <a href="#"><span class="pictos">M</span><span class="hidden">Drop a line</span></a>
</div>

We’re opening a div with the id nav, and placing four links inside. Inside each link, we have a span with the class pictos and a span with the class hidden. Inside the pictos span, we have a letter. Each letter and some symbols are equivalent to an icon in the pictos web font. The hidden span will not actually be hidden, instead it will have a large left margin, that hides it, as the overflow will be set to hidden. Inside the span you can add a short description of the icon displayed. E.g if the icon is a pencil, you can write ‘My blog‘ inside the second span. Make sure the first link has the class of active, meaning it’s wider than the others, hence it displays the description.

CSS

As with most navigation bars, you’ll find the larger part of the code inside the stylesheet.

First we’ll create a short reset sheet, to make sure all default values the browser has set back to 0. I normally use Eric Meyer’s css reset, but this one that jsFiddle uses should work just fine.

body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td { 
        margin:0;
        padding:0;
}

table {
        border-collapse:collapse;
        border-spacing:0;
}

fieldset,img { 
        border:0;
}

address,caption,cite,code,dfn,em,strong,th,var {
        font-style:normal;
        font-weight:normal;
}

ol,ul {
        list-style:none;
}

caption,th {
        text-align:left;
}

h1,h2,h3,h4,h5,h6 {
        font-size:100%;
        font-weight:normal;
}

q:before,q:after {
	content:'';
}

abbr,acronym {
	border:0;
}

First we’ll style the entire document by adding 10px of padding and and a background color of #d1eaf9.

body {
	background: #d1eaf9;
	padding: 10px;
}

Next we’ll ‘import’ the pictos font into the document by using @font-face. Luckily the pictos font comes with the necessary files and code, otherwise I use the FontSquirrel @font-face generator.

@font-face { 
	font-family: 'Pictos';
	src: url('font/pictos-web.eot');
	src: local('☺'), url('font/pictos-web.woff') format('woff'), url('font/pictos-web.ttf') format('truetype'), url('font/pictos-web.svg') format('svg');
	font-weight: normal;
	font-style: normal;
}

Now to the actual styling of the navigation:

#nav a {
	display: block;
	font-style: normal;
	font-family: 'Pictos';
	font-size: 20px;
	padding: 2px 20px 38px 20px;
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#0a324a), to(#08283b));
	width: 20px;
	text-decoration: none;
	overflow: hidden;
	text-shadow: 0 -1px 1px black;
	border-radius: 50px;
	color: white;
	height: 20px;
	margin-bottom: 10px;
	border: 5px solid #146595;
	-webkit-transition: all ease-in-out .3s;
	-webkit-background-clip: padding-box;
}

To be able to add width, height etc. we have to change the way the link displays to block.

Change the font-family to Pictos, and the font-size to 20px. For some reason, the pictos font has weird top-padding, meaning the top-padding on the link is less than the bottom padding.

Two apps helped me create the background gradient, the CSS3 gradient generator we created in a previous tutorial and 0to255, a sweet app to find variations of any color.

Remove any text-decoration, and very important, change the overflow to hidden. This hides the span with the description. Add a black text-shadow, a border-radius of 50px and a solid border of #146595. To ease the width-increase a little, change the -webkit-transition to all ease-in-out .3s. To remove the ugly background-bleed add -webkit-background-clip: padding-box to your code.

Next style the the span.hidden

#nav a span.hidden {
	font-family: 'Lucida Grande';
	font-size: 12px;
	font-weight: bold;
	margin-top: 2px;
	margin-left: 40px;
}

Set the font-family to Lucida Grande, or whatever nice-looking sans-serif typeface you like. 20px is a little too big for my taste, 12px looks a lot better. So does font-weight: bold. The pictos font and the description are not aligned correctly, that’s why we’ll add a 2px margin-top. And to hide the description from view, add a 40px left margin.

We’ll have to display the description when the user hovers over the button, meaning we change the width to 200px.

#nav a:hover, #nav a.active {
	width: 200px;
}

#nav a:active {
	background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#08283b), to(#0a324a));
	-webkit-box-shadow: inset 0 0 2px #222;
}

Finally we’ll rotate the background gradient by 180 degrees in it’s active state, and add a box shadow of 2px with the color #222222.

Your Turn To Talk

Done! Great job!

You can view the demo right here →

Some of you have been asking why most of my tutorials only support webkit browsers. The answer is simple: Webkit browsers support most of the new ‘features‘ of HTML5 and CSS3, whereas Mozilla, Opera, etc… are still catching up. I’m sure it’s possible to achieve similar results in all browsers (except for IE6 of course), but it’d definitely require a lot more code.

As always, post your results in the comment section if you like. And feel free to ask questions too!

Comments

  1. Josh says

    Lame – it doesn’t work on Firefox. Not sure how this is a “sweet” vertical navigation when it doesn’t even work on modern browsers. What good is that? It’s not smart to not support a modern browser simply because it’s “catching up” with browsers that only about 15% of the internet population use.

    “Pretty” lost. Usability is King. Get with it.

  2. says

    If someone says ‘Webkit only’ and it doesn’t work in your favorite non-Webkit browser, don’t blame the person that said it – there’s a lot that can be done with Webkit that a lot of browsers don’t support, should we stop experimenting because it doesn’t work in Firefox? This nav menu is not meant to be used in a production environment.

    The goal with this tutorial wasn’t to show people how to code this kind of navigation using 12 different methods (jQuery, CSS3, etc..) it was to show how it can be done with Webkit.

  3. says

    @Josh You are right to some extent, the navigation of a site should definitely work with all browsers, old and new. But as @Jon already said, this tutorial shows you how to create a quite good looking vertical navigation using only code – meaning it is image-less. It is not meant to replace your current navigation. Web designers and developers should explore new techniques and experiment with them, perhaps even implement them into their designs. And as I wrote (if you take the time to read it, or even skim through the tutorial), only webkit browsers support this navigation, so why do you try it in Firefox (note: it’s “in Firefox” and not “on Firefox”)? I’m sure some of these techniques, like a linear gradient, are supported in other browsers, like your beloved Firefox. That’s why I mentioned the CSS3 gradient generator we created last time (scroll up, you’ll see the link).

    OK? Great. For future reference, please think before you write :)

  4. says

    I guess you just can’t read when he says that it only tested and perhaps only work on -Webkit browser. What’s the use of usability when you can’t even read and understand. Read properly the content – is the KING. Get with it.

  5. says

    Creating vertical navigation have always been a big headache for me and my team. But I thankfully found this link. Thanks for this information. It was very much useful for us…

  6. James says

    Thanks for the tutorial. I think people need to appreciate that just because CSS3 isn’t supported by all browsers yet, it shouldn’t stop us from experimenting. We’re in a transitional period, and old school designers that refuse to adopt new techniques that aren’t working 100% in all browsers will be left lagging behind. I’m not saying that you should implement a navigation that doesn’t work in Firefox, it’s about keeping your eyes open, and gaining inspiration from what others do.

  7. Josh says

    Eric, I did read the entire article. I tried it on Firefox (note: it’s “on Firefox, not “in Firefox because you, the user are not inside the browser, only viewing what is on it) because it’s a modern browser that there’s no excuse for something to not work nearly perfect on it. I know you said to view it with a webkit browser – and that’s fine. It looked really cool in Safari. Webkit browsers are definitely the leading edge when it comes to browser technology. I personally use all sorts of webkit and mozilla specific css3 properties when I’m developing websites – but I at least make sure that they fail gracefully.

    My main beef is that you didn’t specify that this was “just an example” of what is possible. I’m all for experiments. If you had included that one disclaimer, I wouldn’t have even commented. But what’s to stop the average Joe with a mac to run across this code, think it’s really cool and put it on his company’s website – only to make it completely unusable for people on most other browsers? That’s my problem.

Trackbacks

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *