Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Flexbox Layout Building a Layout with Flexbox Building a Navigation Bar with Flexbox

How's the code if i want a dropdown menu with flexbox?

I'd like to use a flexbox navigation like in the video. However I actually want a dropdown menu on one of the <li>.

1 Answer

Hey! You could do something like this:

<ul class="nav">
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li class="dropdown">
      <a href="#" class="dropdown-menu">Dropdown</a>
      <div class="dropdown-menu__content">
        <a href="#">Lorem ipsum</a>
        <a href="#">Lorem ipsum</a>
        <a href="#">Lorem ipsum</a>
      </div>
    </li>
    </ul>

<style>
.nav {
  list-style-type: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-flow: row nowrap;
  background-color: #e3e3e3;
}

li a, .dropdown-menu {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover, .dropdown:hover .dropdown-menu {
  background-color: #586aba;
}

li.dropdown {
  display: inline-block;
}

.dropdown-menu__content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-menu__content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-menu__content a:hover {
  background-color: #586aba;
}

.dropdown:hover .dropdown-menu__content {
  display: block;
}
</style>

I wrapped the css in style tags just for convenience.

Of course, there are other ways to achieve the same result.

Hope this helps.