The Wayback Machine - https://web.archive.org/web/20240728000745/https://www.geeksforgeeks.org/how-to-create-nested-sub-menu-using-css/
Open In App

How to Create Nested Sub Menu using CSS ?

Last Updated : 22 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

CSS Nested Sub-Menus refers to Dropdown Menus that are contained within another dropdown menu. These are commonly used in navigation menus to organize and structure content hierarchically. In this article, we are going to build a nested sub-menu using CSS, it will consist of a nav bar with various links such as Home, About Us, Contact, etc.

Preview

Screenshot-2024-01-22-122852

Approach

  • Create a basic structured HTML page with a header and <nav> element, Include a logo, link section, and search icon section in the <nav>.
  • Nest a sub-navigation bar within one of the links to demonstrate the concept of nesting.
  • Apply CSS to set the background colour, align text, and adjust text-decoration to enhance the visual appeal.
  • Use the Poppins font family for the text inside the navigation bar to achieve a clean and modern look and ensure that text is aligned appropriately within each component.
  • Implement a smooth hover effect using the transition property for visual feedback when hovering over links, create transitions to change background colours or add underlines.
  • Introduce nested sub-menus within the link structure to showcase the capability of creating dropdowns.
  • Utilize the `display` property with a transition effect to toggle sub-menus visibility from `none` to `flex` on hover.

Example: Implementation to design nested sub-menu using CSS.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0">
    
    <!-- Font Awesome CDN -->
    <link rel="stylesheet" href=
    <title>Nested Sub Menu</title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <header class="header">
        <nav class="nav">
            <div class="logo">
                GeeksforGeeks
            </div>
            <ul class="navbar-links">
                <li class="home">
                      <a href="#">Home</a>
                  </li>
                <li class="html">
                    <a href="#" class="html-link">
                        Web Devlopment
                        <i class="fa-solid 
                                  fa-chevron-down html-chevron">
                          </i>
                    </a>
                    <ul class="html-sub-menu">
                        <li><a href="#">HTML</a></li>
                        <li><a href="#">CSS</a></li>
                        <li>
                            <a href="#" class="html-js-link">
                                JavaScript Frameworks
                                <i class="fa-solid fa-chevron-down"></i>
                            </a>
                            <ul class="html-js-sub-menu">
                                <li><a href="#">Angular</a></li>
                                <li><a href="#">Nextjs</a></li>
                                <li><a href="#">jQuery</a></li>
                                <li><a href="#">Express.js</a></li>
                            </ul>
                        </li>
                        <li><a href="#">jQuery</a></li>
                    </ul>
                </li>
                <li class="js">
                    <a href="#" class="js-link">
                        CS Subjects
                        <i class="fa-solid
                                  fa-chevron-down js-chevron">
                          </i>
                    </a>
                    <ul class="js-sub-menu">
                        <li>
                              <a href="#">Operating System</a>
                          </li>
                        <li>
                              <a href="#">Computer Networks</a>
                          </li>
                        <li>
                              <a href="#">DBMS</a>
                          </li>
                        <li>
                              <a href="#">OOPS</a>
                          </li>
                    </ul>
                </li>
                <li class="about-us">
                      <a href="#">About Us</a>
                  </li>
                <li class="contact">
                      <a href="#">Contact</a>
                  </li>
            </ul>
            <div class="navbar-search">
                <a href="#">
                      <i class="fa-solid fa-magnifying-glass"></i>
                  </a>
                <a href="#">
                      <i class="fa-solid fa-cart-shopping"></i>
                  </a>
                <a href="#">
                      <i class="fa-solid fa-user"></i>
                  </a>
            </div>
        </nav>
    </header>
</body>
  
</html>


CSS




/* style.css */
@import url(
  
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
  
body {
    font-family: "Poppins", sans-serif;
}
  
.logo {
    cursor: pointer;
    font-weight: bold;
    letter-spacing: 1px;
    font-size: 1.25rem;
}
  
.nav {
    display: flex;
    justify-content: space-around;
    align-items: center;
    background-color: green;
    color: #fff;
}
  
i {
    transition: transform 0.5s ease;
}
  
ul,
li,
a {
    text-decoration: none;
    list-style-type: none;
    color: #fff;
}
  
.home,
.html,
.angular,
.js,
.about-us,
.contact {
    padding: 1rem 0;
}
  
.navbar-links,
.navbar-search {
    display: flex;
    gap: 2rem;
}
  
.html,
.js,
.html-js-link {
    position: relative;
}
  
.html:hover .html-chevron,
.js:hover .js-chevron {
    transform: rotate(-180deg);
}
  
.html-js-link:hover i {
    transform: rotate(-90deg);
}
  
.html-sub-menu,
.js-sub-menu,
.html-js-sub-menu {
    display: none;
    flex-direction: column;
    position: absolute;
    top: 3.45rem;
    left: -5%;
    background-color: hsl(120, 100%, 20%);
    width: 110%;
}
  
.html-sub-menu li,
.js-sub-menu li,
.html-js-sub-menu li {
    padding: 0.75rem;
}
  
.html:hover .html-sub-menu,
.js:hover .js-sub-menu {
    display: flex;
}
  
.html-sub-menu:hover .html-sub-menu {
    display: flex;
}
  
.html-js-sub-menu {
    top: 5rem;
    left: 9rem;
}
  
.html-sub-menu li:nth-child(3):hover .html-js-sub-menu {
    display: flex;
}
  
.html-sub-menu:hover i {
    transform: rotate(-90deg);
}


Output:

mnb'



Similar Reads

Foundation CSS Menu Vertical Menu
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
2 min read
Foundation CSS Menu Nested Style
Foundation CSS is an open-source &amp; responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails to look amazing &amp; can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is b
2 min read
How to avoid dropdown menu to close menu items on clicking inside ?
The default behavior of a dropdown menu is to close the menu list items when clicked inside. In this article, We will use stropPropagation method to prevent the dropdown menu from closing the menu list. stopPropagation(): The stopPropagation() method is used to stop propagation of event calling i.e. the parent event is called we can stop the propag
2 min read
Create a Mobile Toggle Navigation Menu using HTML, CSS and JavaScript
To create a Mobile Toggle Navigation Menu you need HTML, CSS, and JavaScript. If you want to attach the icons with the menu then you need a font-awesome CDN link. This article is divided into two sections: Creating Structure and Designing Structure. A glimpse of complete Navigation: Creating Structure: In this section, we will create a basic site s
3 min read
How to create Right Aligned Menu Links using HTML and CSS ?
The right-aligned menu links are used on many websites. Like hotels website that contains lots of options in the menu section but in case of emergency to make contact with them need specific attention. In that case, you can put all the menu options on the left side of the navigation bar and display contact us option on the right side of the bar. Th
3 min read
How to create Vertical Menu using HTML and CSS ?
In This article, we will learn how to create vertical menus by using HTML and CSS. Vertical Menu: We can create a vertical menu in the form of buttons and a scrollable menu. Vertical Menu is the buttons arranged in the vertical menu bar/navbar. How to create a vertical menu using buttons: We can create it simply by using HTML and CSS. We will creat
2 min read
How to create a mega menu using HTML and CSS ?
HTML is a markup language used to create web pages and CSS is a stylesheet language used to design a document written in a markup language such as HTML. In this article, we are going to know how can we create a mega menu on our web page with the help of only HTML and CSS. Mega menus are a type of expandable menu in which many choices are displayed
4 min read
Create A Custom Select Menu with Search Box using HTML CSS and JavaScript
This article will show you how to create a custom select menu with a search box using HTML, CSS, and JavaScript. First, we create an option list for the select box, and we can search for text in the text input box. The search result displays the matched option value. Additionally, we have added a "clear" button to remove your latest search. Approac
5 min read
Create a Curtain Menu using HTML and CSS
This article will show you how to create a Curtain menu navbar effect with the help of HTML and CSS. The curtain menu means revealing or pulling down the menu items like a curtain. HTML is used to create the structure of the curtain menu and applies styles on that element to make like curtain menu effect. Also, we will add a close button to the cor
3 min read
Create A Bottom Navigation Menu using HTML and CSS
This article will show you how to create a bottom navigation menu using HTML and CSS. The navigation menu is an essential component in modern web design. It allows users to navigate through a website easily. Here, we use HTML to create the structure of the Bottom Navigation menu, and CSS add styles to the elements. Example 1: In this example, we wi
2 min read
three90RightbarBannerImg