Creating a CSS3 Navigation Menu
In today’s tutorial, I thought a good topic to cover would be the creation of a custom navigation menu using CSS3. The purpose of this tutorial is to further familiarise ourselves with CSS3 and Less, as well as have to an attractive navigation at the end that you’ll hopefully be able to integrate into your own website or projects.
First, let’s take a look at what we’re building:
Please note that before proceeding, it’d be best if you have some knowledge of CSS3 animations and Less. If you aren’t familiar with these, I urge you to read 2 of our previous posts, CSS3 Animation Tutorial and An Introduction to Less CSS.
In addition, this tutorial assumes that you’re relatively experienced with HTML and CSS. I will therefore only be covering certain aspects of the code.
Let’s start by building a simple unordered list within the HTML5
<nav> element. We’ll ignore the labels, sub-menus and animation for now.
As you saw in the demo, what we want is for the navigation to be positioned at the top left of the page, and for each of it’s navigation items to be stacked vertically.
Positioning the navigation is simple enough:
// Additional styles
We can then go ahead and apply CSS to the rest of the HTML:
// Set variables
Hopefully most of the above will make sense. Below are some of the key points:
- On lines 2-3 I’ve set 2 variables, one for the size of the squares and one for the width of the border that should be applied. I could have hard-coded this into the CSS, but doing it this way means that I can easily change the appearance of the box at a later date.
- I’ve used a mixture of child combinator selectors and more generic selectors to define what styles should cascade down to the sub-menu (which we will add in a bit). For example, between lines 27-37, you’ll notice 2 different sets of styles for the links, as I don’t want the sub-menu links to have borders.
Some of you may have noticed that I’ve set a border width and style to the some of the links, but haven’t yet added any colour. That’s actually the next step we’re going to cover, and we’re going to use some cool Less functions!
In the demo, each of the navigation items is different colour. In addition, there are 2 shades used; for example the Gallery link has a light orange background with a dark orange border.
Again, a situation like this is perfect for Less variables, especially when you start adding sub-menus utilising the same colours!
So the next step is to define some additional variables to hold the hex value for each background colour:
@navColorHome: #0891cb; // Dark blue
@navColorAbout: #3bc2fc; // Light blue
@navColorServices: #31ae0a; // Green
@navColorGallery: #ff9600; // Orange
@navColorContact: #ff5831; // Red
But how about the darker shades used for the borders and mouse over effect of the sub-menu? The laborious way would be to find other colours manually, and prior to CSS pre-processors like Less and Sass, this would have actually been the only option.
darken() function takes 2 parameters:
- Colour – An HSL (Hue, Saturation, Lightness) object of the base colour.
- Amount – A percentage to darken the colour by.
Looking at the above, it’s apparent that we need to somehow create the HSL object from our hex values. Less has functions for that too:
hue()– Extracts the hue channel of a colour object in the HSL colour space.
saturation()– Extracts the saturation channel of a colour object in the HSL colour space.
lightness()– Extracts the lightness channel of a colour object in the HSL colour space.
hsl()– Creates an opaque colour object from hue, saturation and lightness (HSL) values.
Using a combination of the above functions, we can get Less to automatically generate an appropriate colour for each navigation item. To clarify this, see the code below:
// Set colours
// Calculate other colours
@navColorHomeDark: darken(hsl(hue(@navColorHome), saturation(@navColorHome), lightness(@navColorHome)), 10%);
@navColorAboutDark: darken(hsl(hue(@navColorAbout), saturation(@navColorAbout), lightness(@navColorAbout)), 20%);
@navColorServicesDark: darken(hsl(hue(@navColorServices), saturation(@navColorServices), lightness(@navColorServices)), 6%);
@navColorGalleryDark: darken(hsl(hue(@navColorGallery), saturation(@navColorGallery), lightness(@navColorGallery)), 8%);
@navColorContactDark: darken(hsl(hue(@navColorContact), saturation(@navColorContact), lightness(@navColorContact)), 15%);
If you ever need to change the colour of a navigation item in the future, you now only need to change the hex values at the top and Less will handle the rest. How awesome is that?
It’s worth noting that I’ve actually gone ahead and tweaked the percentage values (the last parameter) to suit my tastes.
Adding Icons with Font Awesome
We’ve now got a navigation that comprises of vertically stacked boxes of differing colours. What we don’t have are icons; instead we have text which flows out of the boxes.
Let’s go ahead and fix that! We’re going to use Font Awesome for our icons.
Font Awesome is an iconic font set that gives you scalable vector icons. It’s a font, so you can apply CSS to it like any other text. You can change the colour, add text shadow etc… Needless to say, it’s super useful! I’m not sure if I explained that very well, but it’ll become very clear shortly.
First things first, how do we install Font Awesome onto our website? Just paste the following in the
<head> section of your HTML:
And you’re now all set to use Font Awesome on your website! You can use the following to add icons to your website. For example for the icon, use:
The first class above is the generic class that must be used on all Font Awesome icons. The second class then specifies which particular icon to show. There are a wide variety of icons you can use; you can take a look at the cheatsheet to see what’s available. It’s also worth a looking at their examples, as it’ll show you how to do things like this .
Ok, so let’s integrate Font Awesome into our navigation:
<a href="#"><i class="fa fa-home"></i></a>
<a href="#"><i class="fa fa-info"></i></a>
<a href="#"><i class="fa fa-camera"></i></a>
<a href="#"><i class="fa fa-th"></i></a>
<a href="#"><i class="fa fa-comments-o"></i></a>
As aforementioned, the Font Awesome icons are fonts, and therefore we don’t need to touch the CSS. It’ll look the same as the previous text did, i.e. white, no text decoration etc…
Finishing It Off
We’re now pretty much there with our navigation. The rest of it is pretty standard, i.e. nest the sub-menu, add some hover over effects, and some CSS3 animation to give it a little more flare.
Below is a link so that you can download & inspect the entire code. You may notice that I’ve used the HTML5 Boilerplate, which I will cover in a later Tutorial Tuesday post!