How to make a transparent glass menu bar with CSS3
Thanks to CSS3, our design and development options have grown a lot. In this tutorial I will how you how to make a transparent glass menu ba...
Thanks to CSS3, our design and development options have grown a lot. In this tutorial I will how you how to make a transparent glass menu bar with CSS3.
The demo can be seen below.
See the Pen Colorful glass menu concept by Creative Punch (@CreativePunch) on CodePen.
For this demo, I wanted to have a background with some variation. I used a colorful gradient for this, but you could also use a nice image. A flat color as background would not look as good with a glass menu.
I made use of a CSS reset, and prefix-free
The HTML for the glass menu bar
The CSS for our glass menu bar will be much the same as with any menu bar. We will place a nav tag, containing an u list with links.
01
<nav>
02
<ul>
03
<li>
04
<a href="#">Home</a>
05
</li>
06
<li>
07
<a href="#">Blog</a>
08
<li>
09
<a href="#">About</a>
10
</li>
11
<li>
12
<a href="#">Contact</a>
13
</li>
14
</ul>
15
</nav>
The CSS for the glass menu bar
The CSS for the glass menu bar is really simple, but uses a few nice CSS3 features!
01
nav {
02
max-width: 960px;
03
mask-image: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #ffffff 25%, #ffffff 75%, rgba(255, 255, 255, 0) 100%);
04
margin: 0 auto;
05
padding: 50px 0;
06
}
07
08
nav ul {
09
text-align: center;
10
background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.2) 25%, rgba(255, 255, 255, 0.2) 75%, rgba(255, 255, 255, 0) 100%);
11
width: 100%;
12
box-shadow: 0 0 25px rgba(0, 0, 0, 0.1), inset 0 0 1px rgba(255, 255, 255, 0.6);
13
}
14
15
nav ul li {
16
display: inline-block;
17
}
18
19
nav ul li a {
20
padding: 20px;
21
font-family: "Roboto";
22
color: rgba(0, 0, 0, 0.5);
23
text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.4);
24
font-size: 25px;
25
text-decoration: none;
26
display: block;
27
}
28
29
nav ul li a:hover {
30
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1), inset 0 0 1px rgba(255, 255, 255, 0.6);
31
background: rgba(255, 255, 255, 0.1);
32
color: rgba(0, 0, 0, 0.7);
33
}
Let’s look at some of this in a bit more detail.
1
nav ul {
2
text-align: center;
3
background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.2) 25%, rgba(255, 255, 255, 0.2) 75%, rgba(255, 255, 255, 0) 100%);
4
width: 100%;
5
box-shadow: 0 0 25px rgba(0, 0, 0, 0.1), inset 0 0 1px rgba(255, 255, 255, 0.6);
6
}
I styled the ul tag with a background gradient that is white and transparent. I also added a two box-shadow values, one for an outer, dark shadow and one for an inner, light edge. Sadly the box-shadow’s sides don’t fade into the background like the gradient does. Luckily we can solve this by using the mask-image property. We apply this property on the parent element (in this case the nav). We do this so that we can apply a padding to the top and bottom. If we don’t do this, our shadows will get cut off by the mask.
1
nav {
2
max-width: 960px;
3
/* The mask-image gives us some extra fading. It is not necessary but without this, you can't face out the box-shadows. This clips our menu */
4
mask-image: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, #ffffff 25%, #ffffff 75%, rgba(255, 255, 255, 0) 100%);
5
margin: 0 auto;
6
/* Using padding instead of margin for the top and bottom here will keep our box-shadow visible and not affected by the mask-image */
7
padding: 50px 0;
8
}
We also use the same gradient technique for the hover event, only rotated by 90 degrees like this:
1
nav ul li a:hover {
2
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1), inset 0 0 1px rgba(255, 255, 255, 0.6);
3
background: rgba(255, 255, 255, 0.1);
4
color: rgba(0, 0, 0, 0.7);
5
}
This adds an extra “overlay” of glass when you hover over a menu item.
That’s pretty much all there is to it! I hope this inspires you to create some beautiful glass menus.