Building a sub-navigation menu within the main navigation menu

Can anyone help me create a submenu in my navigation menu? My CSS knowledge is limited and I'm encountering some issues. When I add a submenu to the nav menu, it displays as ul li directly.

#cssmenu {
  background: #88BC18;
  width: auto;
  z-index: 1;
  position: relative;
}

#cssmenu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  line-height: 1;
  display: block;
  zoom: 1;
}

#cssmenu ul:after {
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

#cssmenu ul li {
  display: inline-block;
  padding: 0;
  margin: 0;
}

#cssmenu.align-right ul li {
  float: right;
}

#cssmenu.align-center ul {
  text-align: center;
}

#cssmenu ul li a {
  color: #ffffff;
  text-decoration: none;
  display: block;
  padding: 15px 25px;
  font-family: 'Open Sans', sans-serif;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 14px;
  position: relative;
  -webkit-transition: color .25s;
  -moz-transition: color .25s;
  -ms-transition: color 25s;
  -o-transition: color .25s;
  transition: color .25s;
}

#cssmenu ul li a:hover {
  color: #004900;
}

#cssmenu ul li a:hover:before {
  width: 100%;
}

#cssmenu ul li a:before {
  content: "";
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  background: #004900;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width 25s;
  -o-transition: width .25s;
  transition: width .25s;
}

#cssmenu ul li a:after {
  content: "";
  display: block;
  position: absolute;
  right: -3px;
  top: 19px;
  height: 6px;
  width: 6px;
  background: #ffffff;
  opacity: .5;
}

#cssmenu ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
  display: none;
}

#cssmenu ul li.active a {
  color: #004900;
}

#cssmenu ul li.active a:before {
  width: 100%;
}

#cssmenu.align-right li.last>a:after,
#cssmenu.align-right li:last-child>a:after {
  display: block;
}

#cssmenu.align-right li:first-child a:after {
  display: none;
}
<div id="navWrap">
  <nav class="nav">
    <div id="cssmenu" class="centered">
      <ul>
        <li class='first'><a href="#welcome" class="smoothScroll"><span>Home</span></a></li>
        <li><a href="#portfolio" class="smoothScroll"><span>Features</span></a></li>
        <li><a href="#services" class="smoothScroll"><span>About</span></a></li>
        <li><a href="#prices" class="smoothScroll"><span>Prices</span></a></li>
        <li><a href="faq.html" class="smoothScroll"><span>F.A.Q</span></a></li>
        <li><a href="how-to-uses.html" class="smoothScroll"><span>How to Use</span></a></li>
        <li><a href="#contact" class="smoothScroll"><span>Contact</span></a></li>
        <li><a href="#" class="smoothScroll"><span>Log In</span></a></li>
        <li class="last"><a href="#" class="smoothScroll"><span>Help</span></a></li>
      </ul>
    </div>
  </nav>
</div>

Answer №1

Use this as a starting point to help you out. Added some CSS for the submenu.

Take a look at the About Link now.

#cssmenu {
  background: #88BC18;
  width: auto;
  z-index: 1;
  position: relative;
}

#cssmenu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  line-height: 1;
  display: block;
  zoom: 1;
}

#cssmenu ul:after {
  content: " ";
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

#cssmenu ul li {
  display: inline-block;
  padding: 0;
  margin: 0;
}

#cssmenu.align-right ul li {
  float: right;
}

#cssmenu.align-center ul {
  text-align: center;
}

#cssmenu ul li a {
  color: #ffffff;
  text-decoration: none;
  display: block;
  padding: 15px 25px;
  font-family: 'Open Sans', sans-serif;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 14px;
  position: relative;
  -webkit-transition: color .25s;
  -moz-transition: color .25s;
  -ms-transition: color 25s;
  -o-transition: color .25s;
  transition: color .25s;
}

#cssmenu ul li a:hover {
  color: #004900;
}

#cssmenu ul li a:hover:before {
  width: 100%;
}

#cssmenu ul li a:before {
  content: "";
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  background: #004900;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width 25s;
  -o-transition: width .25s;
  transition: width .25s;
}

#cssmenu ul li a:after {
  content: "";
  display: block;
  position: absolute;
  right: -3px;
  top: 19px;
  height: 6px;
  width: 6px;
  background: #ffffff;
  opacity: .5;
}

#cssmenu ul li.last>a:after,
#cssmenu ul li:last-child>a:after {
  display: none;
}

#cssmenu ul li.active a {
  color: #004900;
}

#cssmenu ul li.active a:before {
  width: 100%;
}


/* New CSS for Sub Menu */
#cssmenu ul li {
  position: relative;
}

#cssmenu ul ul {
  position: absolute;
  top: 100%;
  left: 0;
  background: #88BC18;
  display: none;
}

#cssmenu ul li:hover ul {
  display: block;
}

#cssmenu ul ul li a {
  white-space: nowrap;
}
<div id="navWrap">
  <nav class="nav">
    <div id="cssmenu" class="centered">
      <ul>
        <li class='first'><a href="#welcome" class="smoothScroll"><span>Home</span></a></li>
        <li><a href="#portfolio" class="smoothScroll"><span>Features</span></a></li>
        <li><a href="#services" class="smoothScroll"><span>About</span></a>

          <ul>
            <li><a href="#">Sub Item</a></li>
            <li><a href="#">Sub Item</a></li>
            <li><a href="#">Sub Item</a></li>

          </ul>
        </li>
        <li><a href="#prices" class="smoothScroll"><span>Prices</span></a></li>

      </ul>
    </div>
  </nav>

Answer №2

I have modified the menu by adding a second item to create a submenu within it, and included two selectors at the bottom of the CSS. Here is a sample

HTML

<div id="navWrap">
    <nav class="nav">
        <div id="cssmenu" class="centered">
            <ul>
                <li class='first'><a href="#welcome" class="smoothScroll"><span>Home</span></a></li>
                <li><a href="#" class="smoothScroll has_child"><span>Have submenu</span></a>
                  <ul class="submenu">
                  <li><a href="#">submenu one</a></li>
                    <li><a href="#">submenu two</a></li>
                  </ul>
                </li>
                <li><a href="#services" class="smoothScroll"><span>About</span></a></li>
                <li><a href="#prices" class="smoothScroll"><span>Prices</span></a></li>
                <li><a href="faq.html" class="smoothScroll"><span>F.A.Q</span></a>
                </li>
                <li><a href="how-to-uses.html" class="smoothScroll"><span>How to Use</span></a></li>
                <li><a href="#contact" class="smoothScroll"><span>Contact</span></a></li>
                <li><a href="#" class="smoothScroll"><span>Log In</span></a></li>
                <li class="last"><a href="#" class="smoothScroll"><span>Help</span></a></li>
            </ul>
        </div>
    </nav>
</div>

CSS

#cssmenu {background: #88BC18;width: auto;z-index: 1; position: relative;}
#cssmenu ul {list-style: none; margin: 0; padding: 0; line-height: 1; display: block; zoom: 1; }
#cssmenu ul:after {content: " ";display: block;font-size: 0;height: 0; clear: both;visibility: hidden;}
#cssmenu ul li { display: inline-block;padding: 0;margin: 0;}
#cssmenu.align-right ul li {float: right;}
#cssmenu.align-center ul {text-align: center;}
#cssmenu ul li a {color: #ffffff;text-decoration: none;display: block; padding: 15px 25px;font-family: 'Open Sans', sans-serif;font-weight: 700; text-transform: uppercase;font-size: 14px;position: relative;-webkit-transition: color .25s;-moz-transition: color .25s;-ms-transition: color 25s;-o-transition: color .25s;transition: color .25s;}
#cssmenu ul li a:hover {color: #004900;}
#cssmenu ul li a:hover:before {width: 100%;}
#cssmenu ul li a:before {content: "";display: block;position: absolute; left: 0;bottom: 0; height: 3px;width: 0;background: #004900;-webkit-transition: width .25s;-moz-transition: width .25s;-ms-transition: width 25s; -o-transition: width .25s;transition: width .25s;}
#cssmenu ul li a:after {content: "";display: block;position: absolute; right: -3px; top: 19px;height: 6px; width: 6px;background: #ffffff;opacity: .5;}
#cssmenu ul li.last > a:after,
#cssmenu ul li:last-child > a:after {display: none;}
#cssmenu ul li.active a {color: #004900;}
#cssmenu ul li.active a:before {width: 100%;}
#cssmenu.align-right li.last > a:after,
#cssmenu.align-right li:last-child > a:after {display: block;}
#cssmenu.align-right li:first-child a:after {display: none;}
#cssmenu .submenu{
  display:none;
}
#cssmenu li:hover > ul{
  display:block;
  width:200px;
  z-index: 10;
  position: absolute;
  left: 100px;
  top: 44px;
  background: #88BC18;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Syncing user information with MySQL database

I have created a user profile page that pulls in the information of the logged-in users. The issue I am facing is that when I click on the submit button, all the updated information gets sent to an edit_profile.php file except for the password field. I hav ...

Challenge with shifting list items

The issue arises when applying margin to any element within the product or slider div, causing all list items to shift downwards. .product-slider { margin-top: 16px; text-align: center; } .slide-item { list-style-type: none; display: inline; ...

Using the combination of z-index and visibility:hidden makes the button go completely undetect

Can someone please review this code? .parent { visibility: hidden; position: relative; z-index: 1; } .child { visibility: visible; } <button class="parent"> <span class="child">content</span> </button> I' ...

Make sure to maintain the hover effect even after clicking

How can I make my div retain the hover effect even after clicking on it? Here is the code snippet: #work_btn{ position:absolute; top:60%; left:60px; color:grey; background: -webkit-linear-gradient(0deg, #ff956c, #ff3644); -webkit-background-clip: text ...

Troubleshooting issues with lxml Xpath functionality

Struggling to extract information from a webpage code provided below. While I managed to retrieve the users using xpath, I am facing difficulty in obtaining their scores. Any suggestions on what might be going wrong here? import requests from lxml impor ...

The Lighthouse tool consistently indicates a high Cumulative Layout Shift (CLS) score, even after adjusting

On the Lighthouse report, Cumulative Layout Shift shows a high number: 0.546 Upon analyzing the trace, I noticed that the image block initially does not take up the required space and pushes down the content below. Refer to the image: https://i.sstatic.ne ...

Clicking on a JQuery dropdown menu will always result in it staying open

After creating a dropdown menu click, I noticed some strange behavior. When I click the dropdown button, the menu appears as expected. However, if I move my cursor away without clicking the button again, the dropdown menu disappears and behaves like a hove ...

The mysterious case of the vanishing jQuery traversal box

I'm currently navigating using previous and next buttons, but it seems to be malfunctioning if I click too quickly. My goal is for the navigation to remain smooth without breaking. var $currDiv = $("#start"); $("div").hide(); $currDiv.c ...

Ensuring consistent column heights in Bootstrap across all devices

There is an HTML element containing an image and text positioned side by side using Bootstrap. Here is the code: <div class="row"> <div class="col-md-6"> <div class="avatar"> <img src="<?= get_template_directory_uri(); ...

How come my image is not aligned to the right of the header in my HTML code?

My attempt to align an image to the right side of my heading using code was unsuccessful. Here is the HTML snippet I used: /* Aligning with CSS */ #colorlib-logo img { display: flex; align-items: center; width: 30px; height: 30px; } #colorli ...

PHP Multiple Filter Options

Currently, I am developing a filtering system for a mySQL database that utilizes dropdown menus. While the system functions correctly when using one filter, I am encountering difficulties in stacking multiple filters. To achieve this, I am retrieving the c ...

Bootstrap scrollspy feature experiencing malfunction

I am encountering an issue with scrollspy on Bootstrap version 5.1. Despite following the examples in the documentation (links provided below), I am unable to make it work; there are no visible changes when scrolling. My goal is for the corresponding link ...

Display the accurate duration based on the dates selected in an HTML form automatically

If someone has office hours on Monday, Wednesday, and Friday from 7:00 am to 7:00 pm, and on Tuesday and Thursday from 10:00 am to 9:00 pm, the dropdown menu should display only the timings of 7:00 AM to 7:00 PM if the selected date is a Monday, Wednesda ...

Storing Iframe content without the need for a "save button" in a colorbox

I'm currently working on a small project that involves a main HTML page and a separate settings.html file. I've set it up so that the settings page pops up when clicked, using the following code: jQuery('#settings').colorbox({iframe:tr ...

I have configured my CSS styles in the module.css file of my Next.js application, but unfortunately, they are not being applied

I recently developed a nextjs with electron template application: Here is the link to my code on CodeSandbox: https://codesandbox.io/s/sx6qfm In my code, I have defined CSS classes in VscodeLayout.module.css: .container { width: '100%'; he ...

I successfully developed a unique custom accordion feature in React.js, where the toggle functionality is fully functional. However, I am currently facing an

import React, { useState, useEffect } from "react"; import axios from "axios"; const Faq = () => { const [faq, setFaq] = useState([]); const [activeIndex, setActiveIndex] = useState(null); const fetchFaqs = async () => { ...

Submission form not functional at this time

I'm having trouble with this form submission, can someone offer assistance? My issue involves using bootstrap css to submit a simple form. I'm facing a small problem that I can't seem to figure out, any help would be appreciated. <di ...

Call upon the web request using `invoke-webrequest`, create a new object with the ComObject "InternetExplorer.Application", and instantiate another object using `

In an attempt to create a task scheduler that retrieves an embed link from a website every hour, I am utilizing PowerShell to fetch the information from the web. $ie = New-Object -ComObject "InternetExplorer.Application" $ie.navigate("https://servicenow. ...

"Implementing a responsive design with a background image and radial gradient - how to

The image currently in use: https://i.stack.imgur.com/piK6l.jpg Here is how I've implemented it using Vue.js, Bootstrap, and CSS: Code Snippet <div class="landing"> <div class="container text-white details h-100"> ...

Struggling to show labels in the correct position for each button?

I need help with aligning labels under buttons. I have 3 buttons, each with a corresponding label. I want the labels to be displayed in line under the buttons, and if the length of a label exceeds the button width, it should wrap to the next line. I'v ...