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!





About the author:

Eric Hoffman is a young American freelance web and brand identity designer based in Switzerland, working under the brand hatchergraphics (portfolio coming soon). He enjoys reading, swimming, going to school (yes, I do) and tweeting. If you’d like to connect with him, you should follow him on Twitter as @erichoffman

Comments:

Scroll back to the top