How To Create a Trendy Flat Style Nav Menu in CSS

I’ve heard from a bunch of people who found my CSS drop down menu tutorial really useful, so today’s we’re going to build another menu with some fancy hover effects. With the Flat design trend being so popular we’ll use adopt this style for today’s menu by using bright solid colours and clean icons. We’ll be using various must-know CSS techniques so this is a great tutorial for any web designers learning the basics.

The Menu Concept

Here’s the menu we’ll be building as part of this tutorial. It’s based on the oh-so-popular flat design trend with solid colours and neat square boxes. The clean icons are courtesy of the Linecons pack and the font we’ll be using via Google Webfonts is Dosis. When each navigation box is hovered the text label appears off to the right, adopting the same colour scheme as its parent menu item.

The HTML Structure

Before getting started with any styling we first need to build the foundations and construct the menu in HTML. HTML5 elements such as nav are widely supported nowadays even across IE with the help of plugins such as html5shiv.

The HTML begins with the usual document structure of doctype, title and link to the CSS stylesheet which we’ll be populating later. The Dosis font is set up via Google Webfonts and its stylesheet is attached. The actual structure of the navigation menu begins at the nav element, inside which is a typical unordered list. Each list item contains a link, but to give ourselves an extra item to target when styling up the offset text the label of each anchor is wrapped in a span element.

The CSS Styling

The menu styling begins by altering the appearance of the unordered list by removing the list bullet points and floating each <li> side by side. To compensate for this the overflow: hidden; declaration is added to the <ul> to clear the floats, then its positioning is altered to allow the location of the hover text to be relative to the parent ul.

Each anchor inside the list element is styled to appear square by adding a width and height of 120px, allowed by the conversion from an inline element to block with display: block;. All the icons were exported from Photoshop in a single sprite image, so the icons.png file is added as a background image to all anchors using the generic nav ul li a selector.
Any unique styling to each individual anchor is then added using an :nth-child selector. This beats adding extra classes to the HTML by simply being able to target each individual li based on its number. The different colour backgrounds are then added and the background position of the icons image is adjusted to locate the correct icon within the sprite.

If this design only featured the square blocks this tutorial would be pretty much complete, but an extra fancy step is to create the offset text effect on hover of each element. This is all done by targeting the <span> that was added to each anchor. First the font styling is set up as the Dosis Google WebFont and its appearance converted to uppercase using the text-transform property.
By default each label is aligned to the top left of every navigation block, but we want them all aligned off to the right of the whole menu. To do this we simply add the position: absolute; declaration and alter the left and top positioning to suit. That position: relative; declaration that was added to the nav ul earlier allows this absolute positioning to be relative to the parent ul, rather than relative to the full width of the browser window.

All the labels are visible at the same time until they’re hidden with display: none;, they’re then told to reappear on hover of each anchor by adding the opposite declaration: display: block;. The only thing left to do is use the :nth-child selectors once again to give each label the corresponding colour to match the menu block it’s paired with.

The Complete CSS

Here’s the complete CSS should you want to copy/paste into your own designs.

The use of colors in this example is great. However I am not sold on the flat Ui for web design. It seems ok for certain uses on a mobile Ui but so far people are making a bigger deal out of this trend than required.

In the tutorial you say the icons are exported from photoshop as a single sprite. Is the sprite part of the icon pack that I’m missing or is it something you created, and if you created the sprite how did you go about it?

Subscribe for email updates

About Line25

Line25 was built in March 2009 as a place to share web design ideas and inspiration through articles, tutorials and examples of stunning site designs. Be the first to see new posts by subscribing by RSS, have new content delivered by Email, or join Line25 on Twitter.