How to create a large CCS3 menu with transitions

While playing around with some CSS3 transitions for an image gallery representation, I ended up creating a menu that I really liked, so I decided to write a tutorial around it. Have a look at the demo (or this pen) to see what we’re going to create. After building the horizontal version, it’s very easy to create a vertical menu as well. But we’ll get to that.

Note: All versions should work properly in the latest versions of Chrome, Firefox, Opera, Safari, and Internet Explorer, as well as work without transitions in IE8 & IE9.

So, first of all…

The HTML code

We are going to create our menu as an unordered list of links and images, and place it inside a nav element with the class menu-container, as follows:

Note: In the demo, there is more HTML & CSS code, but since it’s irrelevant of the actual menu, I’m not explaining it in the tutorial. You can find the whole code in the tutorial’s source code.

The CSS code

The idea is that the images are going to be visible, and the anchor tags are going to sit on top of them with an opacity value of 0, until there is a hover state. When this happens, the anchor will become visible, and will be expanded a little bit over the image.

To begin with, we will reset some styles of our ul and li elements, and we will set the width of our menu to 100%.

The next step is to set the width of the list items, which will hold the images. We want to use 6 images, so since 100 / 6 = 16.66666666666667, we’ll set the width to that. We’ll also specify that the width of each image will expand to the 100% of its container (i.e. the li element).

That was the simple part. Now, let’s move to the anchors. I broke the whole CSS code for the anchor tag in 2 blocks, just to explain it easier.

Starting with the first block, the important part is to specify the element’s position as absolute (that’s why we earlier set the li element’s position as relative), and its width and height values to 100%, to make sure it will cover the image. The box-sizing values are there to make calculations (e.g. with padding) easier.

We also have to specify that the initial opacity of the element is zero, and the appropriate values for the transitions.

Finally, let’s style the hover state. We’ll expand a little bit the width and height of the element (10% larger than the image), and use some margin values to center it. We’ll also set its opacity value to 0.8. Remember that this process will be animated due to the transition value we set before.

Have a look at the demo (or this pen) to see what we’ve just created. You can also download the tutorial’s source code.

But, let’s not stop here. Let’s build a…

Vertical version

Transforming the menu to work vertically is extremely easy. The HTML code remains the same. We will only have to set the .menu-container to something smaller than 100%, and set the li element’s width to 100% (adding a small margin fix if it’s required).

Here you go (pen). You can find this version as well in the tutorial’s source code.

Removing opacity from text

If you notice, the opacity value is inherited to the text as well, making it a little transparent. I quite like it that way, but since it’s highly likely that someone will want the text to be completely opaque, let’s create a workaround.

We will add the following before setting the anchor element’s opacity values (which are still zero).

And we will replace the opacity values of the hover state with the following:

That’s all! This version’s demo is here (pen). As you probably already figured out, you can find this version as well in the tutorial’s source code.

For any issues or questions, feel free to post a comment.