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

GoldSpec Digital
GoldSpec Digital
4,185 Points

How to use Flexbox with nested list items

Hi,

So i'm trying to create a navbar with a CSS dropdown menu. My nav consists of an icon, a search bar, and then an ul with one list item, and then another ul within this one list item.

As you would expect, these are currently all sat under one another.

I've been working on this for a good few hours and just can't figure it out.

I know Flex containers only work with direct children.

So how do I get this to all display on one line (nicely) with all my nested elements.

Here is my HTML:

<header>

        <span class="fab fa-asymmetrik"></span>

        <div>
            <input class ="search-input" type="text" placeholder="">
            <span class="fas fa-search"></span>
        </div>

        <nav class="container__nav"> 
            <ul>
                <li class="nav__item-dropdown"><a href="#">User</a>

                    <ul>
                        <li class="dropdown__item"><a href="#">Account</a></li>
                        <li class="dropdown__item"><a href="#">Settings</a></li>
                        <li class="dropdown__item"><a href="#">Log Out</a></li>
                    </ul>
                </li>
            </ul>

        </nav>

    </header>

1 Answer

Steven Parker
Steven Parker
229,783 Points

To get all the nested elements to display on one line you can use nested flex containers:

header,
.nav__item-dropdown,
.nav__item-dropdown ul
{
  display: flex;
  align-items: center;
}
header *, .dropdown__item { flex: 1; }
header :last-child { flex: 4; }