Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

trio-group I.AM
trio-group I.AM
23,858 Points

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.