1. Web Design
  2. UX/UI
  3. Navigation

How to Build a Striped Navigation With Flexbox

Scroll to top

Thanks to flexbox, nowadays we can build some really attractive and modern layouts with relative ease. In this tutorial we’re going to look at an interface used recently for Google National Parks, and see how flexbox can help us improve on it.

 As always, before going any further, let’s look at what we’ll be building (you may need to check out the larger version as the full effect kicks in on viewports wider than 800px):

Be sure to hover over the links to trigger the corresponding effects. 

The Markup

First, let’s examine the markup we’ll be using to build this layout. We define a div element with five links within it (you can use whichever elements you feel are appropriate). Each of our links contains a div with the class of overlay. Below you can see the skeleton of our code:

1
<div class="flex-wrapper">
2
  <a href="" class="one">
3
    <div class="overlay">
4
      <div class="overlay-inner">
5
        <h2>Title #1</h2>
6
        <p>Fusce vulputate orci at nulla consequat, ac tincidunt quam ultrices.</p>
7
      </div>
8
    </div>
9
  </a>
10
  
11
  <!-- four more links here -->
12
</div>

Initial CSS Styles

With the markup ready, we start defining some initial CSS styles, specifically:

  • Specify the outermost div as a flex container and set its height equal to the viewport height with height: 100vh.
  • Set an equal width for all links (flex items). To achieve that, we give them each flex: 1. Additionally, all of our links will have a background image (sourced from unsplash.com) which covers the viewport height. As a fallback (in case an image isn’t available), we also specify a unique background color for each one.
  • When we hover over a link, its size becomes three times bigger relative to the size of the other links. We do this by changing the value of the flex property of the target link. Happily enough this property belongs to the animated CSS properties, so we’re able to generate a smooth transition effect (at least in most recent browsers). 

Here’s part of the CSS code demonstrating what we’ve described above:

1
.flex-wrapper {
2
  display: flex;
3
  height: 100vh;
4
}
5
6
.flex-wrapper a {
7
  position: relative;
8
  flex: 1;
9
  background-size: cover;
10
  background-position: center;
11
  background-repeat: no-repeat;
12
  transition: flex .4s;
13
}
14
15
.flex-wrapper .one {
16
  background-image: url(IMAGE_PATH);
17
  background-color: red;
18
}
19
20
.flex-wrapper a:hover {
21
  flex: 3;
22
}

So far, if we preview the demo in a browser that supports flexbox, we’ll see this result:

Styling the Overlay

Our next step is to assign a few styles to the overlay. Here’s what we’ll do:

  • Give overlay the same dimensions as the parent link. We can achieve this behavior by positioning it absolutely (relative to the immediate parent) and then specifying zero values for all the offset properties. 
  • Define the overlay as a flex container. This way, we’re able to center its direct child (i.e. .overlay-inner element) vertically and horizontally.
  • Add a semi-transparent grey background to the overlay when we hover over the parent link.

Here are the associated CSS styles:

1
.flex-wrapper .overlay {
2
  display: flex;
3
  align-items: center;
4
  justify-content: center;
5
  padding: 0 10px; 
6
  position: absolute;
7
  top: 0;
8
  right: 0;
9
  bottom: 0;
10
  left: 0;
11
  transition: background-color .4s;
12
}
13
14
.flex-wrapper a:hover .overlay {
15
  background-color: rgba(0, 0, 0, .5);
16
}

Inner Overlay

Now, it’s time to style the inner parts of our overlay. By default these are hidden and should appear only when we hover over the corresponding parent link. But not immediately, we’ll reveal them after a small delay. This delay is important; if we don’t define it specifically, the transition effect will look a little bit ugly. Remove it and test the demo to understand what I mean. 

So just to recap, first the link gets bigger, then the overlay elements become visible. Also, we use the translate3d() to create some slide in effects. Last but not least, we use the transform-style: preserve-3d hack (or a similar one) to prevent possible flickering effects across different browsers.

And here are the related CSS styles:

1
.flex-wrapper .overlay-inner * {
2
  visibility: hidden;
3
  opacity: 0;
4
  transform-style: preserve-3d;
5
}
6
7
.flex-wrapper .overlay h2 {
8
  transform: translate3d(0, -60px, 0);
9
}
10
11
.flex-wrapper .overlay p {
12
  transform: translate3d(0, 60px, 0);
13
}
14
15
.flex-wrapper a:hover .overlay-inner * {
16
  opacity: 1;
17
  visibility: visible;
18
  transform: none;
19
  transition: all .3s .3s;
20
}

Let’s see what that’s given us.


Going Responsive

The page looks great on large screens, but perhaps on small, or even on medium screens we’ll have to make a few adjustments. For example, here are a few things we can do:

  •  Flip the main-axis of the flex container by adding flex-direction: column to it. In doing so the flex items will flow from top to bottom.
  • Cancel all the transition effects and show the overlay elements by default . 

Our desktop-first media query looks as follows (I’ve used 800px only because that suits the embedded demos in this post–you can choose whatever you feel is best):

1
@media screen and (max-width: 800px) {
2
  .flex-wrapper {
3
    flex-direction: column;
4
  }
5
  
6
  .flex-wrapper a:hover {
7
    flex: 1;
8
  }
9
  
10
  .flex-wrapper a:hover .overlay {
11
    background-color: transparent;
12
  }
13
  
14
  .flex-wrapper .overlay h2,
15
  .flex-wrapper .overlay p {
16
    opacity: 1;
17
    visibility: visible;
18
    transform: none;
19
  }
20
}

Here’s the final appearance of our navigation:

Browser Support

The demo works in all recent browsers that support flexbox. 

In some browsers you may notice that the animation of the flex property isn’t as smooth as it should be, or perhaps doesn’t work at all. For example, IE11 doesn’t animate this property, whereas Edge does. This is reasonable enough, considering the fact that flexbox is a new layout mode which is still gaining traction.

Conclusion

In this tutorial, we improved our flexbox knowledge by learning how to build a stylish navigation layout. Hopefully you enjoyed what we’ve built here and have taken some inspiration for your next projects!

If you build a similar layout, don’t forget to show us the approach (pure CSS or JavaScript-based solution) you used.  

In the Wild   

Before closing, I’d like to share with you a few sites which use a similar layout to what we’ve just created:

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.