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

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
216,057 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; }