CSS navigation with animated underline effect
Posted: March 16th, 2016 | Posted in: CSS, Tutorials
Note: This tutorial was originally published in 2016. The tips and techniques explained may be outdated.
Just part of something I’ve been working on. The requirement was for a simple navigation, but it also had to have something different. I’m really starting to love CSS transforms!
See the Pen Navigation with Animated Underline by Gordon Mackay (@gordonmacdotcom) on CodePen.
The HTML
<nav class="nav--top"> <ul> <li><a href="#">About</a></li> <li><a href="#">How it works</a></li> <li><a href="#">E-mail me</a></li> </ul> </nav>
The CSS
.nav--top > ul { font-family: Arial, Sans-Serif; font-size: 22px; margin: 0; padding: 0; list-style: none; text-align: center } .nav--top > ul > li { line-height: normal; padding: 0; margin: 0 0 1rem } @media (min-width: 52em) { .nav--top > ul > li { margin: 0 0 0 .75rem; display: inline-block } } .nav--top > ul > li a { display: inline-block; vertical-align: middle; -webkit-transform: translateZ(0); transform: translateZ(0); box-shadow: 0 0 1px transparent; -webkit-backface-visibility: hidden; backface-visibility: hidden; -moz-osx-font-smoothing: grayscale; position: relative; padding: 0 0 .15rem; border-bottom: 2px solid #d8d8d8; text-decoration: none; font-weight: 400; color: #787777 } .nav--top > ul > li a:before { content: ""; position: absolute; z-index: -1; left: 0; right: 100%; bottom: -2px; background: #368e65; height: 2px; -webkit-transition-property: right; transition-property: right; -webkit-transition-duration: 0.3s; transition-duration: 0.3s; -webkit-transition-timing-function: ease-out; transition-timing-function: ease-out } .nav--top > ul > li a:hover:before, .nav--top > ul > li a:focus:before, .nav--top > ul > li a:active:before, .nav--top > ul > li.current_page_item a:before { right: 0 } .nav--top > ul > li a:hover, .nav--top > ul > li a:focus, .nav--top > ul > li a:active, .nav--top > ul > li.current_page_item a { border-bottom: 2px solid #fff; color: #121212 }
Enjoy :)