---
category: Navigation
created: '2020-01-13'
description: Create nested dropdown menu with CSS
keywords: css dropdown menu, css multi-level dropdown menu, css nested dropdown menu
thumbnail: /assets/css-layout/thumbnails/nested-dropdowns.png
title: Nested dropdowns
---

## HTML

```html index.html
<ul class="nested-dropdowns">
    <li>A</li>
    <li>
        <div class="nested-dropdowns__item">
            B
            <div class="nested-dropdowns__arrow">...</div>
        </div>
        <ul>
            <li>B.1</li>
            <li>B.2</li>
            <li>
                <div class="nested-dropdowns__item">
                    B.3
                    <div class="nested-dropdowns__arrow">...</div>
                </div>
                <ul>
                    <li>B.3.1</li>
                    <li>B.3.2</li>
                    <li>B.3.3</li>
                    <li>B.3.4</li>
                </ul>
            </li>
            <li>B.4</li>
            <li>B.5</li>
        </ul>
    </li>
    <li>C</li>
    <li>D</li>
</ul>
```

## CSS

```css styles.css
.nested-dropdowns {
    /* Border */
    border: 1px solid #d1d5db;
    display: flex;

    /* Reset list styles */
    list-style-type: none;
    margin: 0;
    padding: 0;
}

.nested-dropdowns li {
    cursor: pointer;

    /* Spacing */
    padding: 0.25rem;

    /* Used to position the sub nested-dropdowns */
    position: relative;
}

/* The sub nested-dropdowns */
.nested-dropdowns ul {
    /* Border */
    border: 1px solid #d1d5db;

    /* Hidden by default */
    display: none;

    /* Absolute position */
    left: 0;
    position: absolute;
    top: 100%;

    /* Reset styles */
    list-style-type: none;
    margin: 0;
    padding: 0;
}

/* The second level sub nested-dropdowns */
.nested-dropdowns ul ul {
    left: 100%;
    position: absolute;
    top: 0;
}

/* Change background color of list item when being hovered */
.nested-dropdowns li:hover {
    background-color: rgba(0, 0, 0, 0.1);
}

/* Show the direct sub nested-dropdowns when hovering the list item */
.nested-dropdowns li:hover > ul {
    display: block;
}
```

<Playground>
```css placeholders.css hidden
.triangle {
    border-style: solid;
    height: 0;
    width: 0;
}
.triangle--t {
    border-color: transparent transparent #d1d5db transparent;
    border-width: 0 var(--triangle-size) var(--triangle-size) var(--triangle-size);
}
.triangle--r {
    border-color: transparent transparent transparent #d1d5db;
    border-width: var(--triangle-size) 0 var(--triangle-size) 1rem;
}
.triangle--b {
    border-color: #d1d5db transparent transparent transparent;
    border-width: var(--triangle-size) var(--triangle-size) 0 var(--triangle-size);
}
.triangle--l {
    border-color: transparent #d1d5db transparent transparent;
    border-width: var(--triangle-size) 1rem var(--triangle-size) 0;
}
.triangle--tr {
    border-color: transparent #d1d5db transparent transparent;
    border-width: 0 var(--triangle-size) var(--triangle-size) 0;
}
.triangle--br {
    border-color: transparent transparent #d1d5db transparent;
    border-width: 0 0 var(--triangle-size) var(--triangle-size);
}
.triangle--bl {
    border-color: transparent transparent transparent #d1d5db;
    border-width: var(--triangle-size) 0 0 var(--triangle-size);
}
.triangle--tl {
    border-color: #d1d5db transparent transparent transparent;
    border-width: var(--triangle-size) var(--triangle-size) 0 0;
}
.triangle--sm {
    --triangle-size: 0.5rem;
}
.triangle--md {
    --triangle-size: 2rem;
}
.triangle--lg {
    --triangle-size: 4rem;
}
```

```css styles.css hidden
body {
    align-items: center;
    display: flex;
    justify-content: center;
    height: 32rem;
}

.nested-dropdowns {
    /* Border */
    border: 1px solid #d1d5db;
    display: flex;

    /* Reset list styles */
    list-style-type: none;
    margin: 0;
    padding: 0;
}

.nested-dropdowns li {
    cursor: pointer;

    /* Spacing */
    padding: 0.25rem;

    /* Used to position the sub nested-dropdowns */
    position: relative;
}

/* The sub nested-dropdowns */
.nested-dropdowns ul {
    /* Border */
    border: 1px solid #d1d5db;

    /* Hidden by default */
    display: none;

    /* Absolute position */
    left: 0;
    position: absolute;
    top: 100%;

    /* Reset styles */
    list-style-type: none;
    margin: 0;
    padding: 0;
}

/* The second level sub nested-dropdowns */
.nested-dropdowns ul ul {
    left: 100%;
    position: absolute;
    top: 0;
}

/* Change background color of list item when being hovered */
.nested-dropdowns li:hover {
    background-color: rgba(0, 0, 0, 0.1);
}

/* Show the direct sub nested-dropdowns when hovering the list item */
.nested-dropdowns li:hover > ul {
    display: block;
}

/* Demo */
.nested-dropdowns__item {
    align-items: center;
    display: flex;
}
.nested-dropdowns__arrow {
    margin-left: 0.25rem;
}
```

```html index.html hidden
<ul class="nested-dropdowns">
    <li>A</li>
    <li>
        <div class="nested-dropdowns__item">
            B <div class="nested-dropdowns__arrow">
                <div class="triangle triangle--b triangle--sm"></div>
            </div>
        </div>
        <ul>
            <li>B.1</li>
            <li>B.2</li>
            <li>
                <div class="nested-dropdowns__item">
                    B.3
                    <div class="nested-dropdowns__arrow">
                        <div class="triangle triangle--r triangle--sm"></div>
                    </div>
                </div>
                <ul>
                    <li>B.3.1</li>
                    <li>B.3.2</li>
                    <li>B.3.3</li>
                    <li>B.3.4</li>
                </ul>
            </li>
            <li>B.4</li>
            <li>B.5</li>
        </ul>
    </li>
    <li>C</li>
    <li>D</li>
</ul>
```
</Playground>
