Unable to generate a navigation panel using HTML/CSS and jQuery

I recently completed the basic courses in HTML/CSS, JavaScript, jQuery, and PHP on Codecademy. I'm currently working on creating a website using HTML/CSS and jQuery in Codecademy's codebits. However, I'm facing some issues with my navigation menus. The main navigation menus appear to be fine, but when I hover over a menu that has a sub-menu, it causes the primary menus to shift down along with the dropdown menus. I've spent the whole day trying to fix this issue without success. I've experimented with different display properties such as inline, inline-block, and block, as well as different positioning settings, but nothing seems to work. I believe my HTML is correct, but there may be some errors in my CSS. Despite my efforts throughout the day, I am feeling exhausted and frustrated.

Here is my HTML code:

<!DOCTYPE html>
<html>
<head>
<title>My First Website</title>
<link type = "text/css" rel = "stylesheet" href = "style.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type = "text/javascript" src = "script.js"></script>
</head>

<body>
<div class = "header">  <!--Header Area -->
    <ul class = "mainMenu">  <!-- The main nav menu -->
        <li><a href = "#">Home</a></li>
        <li>About Us</li>
        <li>Contact Us</li>
        <li id = "services">Services 
        <!--Creating sub menu-->
            <ul class = "servicesSubMenu">
                <li>Basic Web Design</li>
                <li>Pro Web Design</li>
                <li>Advanced Web Design</li>
                <li id = "wordpressWebDesign">Wordpress Web 
                Design
                <!--Creating Sub-sub menu-->
                    <ul class = "wordpressSubMenu">
                        <li>Wordpress Installation</li>
                        <li>Wordpress Customization</li>
                    </ul> 
                </li>     
            </ul>
        </li>    
    </ul>

</div>

<!--creating div class wrapper for sliderArea-->
<div class = "wrapper">
    <div class = "slideArea"> <!--Creating the slider-->

    </div>
</div>

<!--Creating the main footer-->
<div id = "mainFooter">
    <p>Copyright &copy; 2022 <a href = "https://www.mywebsite.com">My Website</a></p>
</div>

</body>

</html>

Here is my CSS:

/* ==== Settings for the main body
================================================= */
body {
    background-color: #454545;
}

/*======  Settings for the main nav menu
================================================== */
.mainMenu {
  border-radius: 10px; 
  background-color: #555555;
  font-family: serif;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  height: 50px;
  line-height: 30px;
  padding-right: 80px;
}

.mainMenu li {
    position: relative;
    display: inline-block;
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 10px;
    list-style: none;
    padding: 0 20px;
}

.mainMenu a {
    text-decoration: none;
    display: block;
    color: #389803;
    height: 40px;
}

/* === Settings for the sub menu of the 'Services' Button
===================================================== */
.servicesSubMenu {
  font-family: serif;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  margin-top: 10px;
}

.servicesSubMenu li {
    border: 1px solid red;
    border-radius: 8px;
    list-style: none;
    background-color: #454545;
    height: auto;
    width: 200px;
    padding: 10px;
    margin: 1px;
    display: block;
}

/* ======== Settings for the slider area of home-page.
==================================================== */
.slideArea {
    border-radius: 15px;
    background-color: #909090;
    height: 500px;
    width: auto;
    margin: 10px, 10px, 10px, 10px;
}

/* === Settings for the main footer area 
==================================================== */
#mainFooter {
    text-align: center;
    font-weight: bold;
}

#mainFooter a {
    color: #FEFE79;
}

And here is my jQuery code:

var main = function() {
    $(".servicesSubMenu").hide(); 

    $("#services").hover(function() {
        $(".servicesSubMenu").slideToggle(80);
        $(".wordpressSubMenu").hide();
    });

    $("#wordpressWebDesign").hover(function() {
      $(".wordpressSubMenu").slideToggle(80);
    });
}

$(document).ready(main);

You can view my codes and the test page here. Please check both the full-screen and smaller screen layouts for different problematic displays. Any assistance would be greatly appreciated.

Thank you!

Answer №1

After reviewing your code on the website, I noticed that you have not specified the position value. The positioning of an element is crucial as it determines where it will appear on the webpage. Typically, the default position is relative, but changing it to absolute ensures that the element remains unaffected by its surrounding elements.

I recommend implementing the following code in your CSS file:

.servicesSubMenu {
  font-family: serif;
  font-weight: bold;
  color: #389803;
  font-size: 17px;
  margin-top: 10px;
  position: absolute;
}

Answer №2

Check out this example code snippet for a custom navigation bar.
This is a CSS-only solution, no Javascript involved.

.navigation {
  background-color: gray;
  list-style-type: none;
  height: 50px;
}
.menu-item {
  display: inline-block;
  width: 80px;
  text-align: center;
  height: 100%;
}
.menu-item {
  padding-top: 15px;
}
.menu-item:hover {
  background-color: #555;
}
.menu-item .sub-item {
  display: none;
}
.menu-item:hover .sub-item {
  position: absolute;
  width: 80px;
  display: block;
    border-bottom: 1px solid black;
  background-color: red;
}
.sub-item: hover {
  display: block;
}
.menu-item .sub-item a {
  display: block;
  height: 20px;
  border-top: 1px solid black;
}
<nav>
  <div class="navigation">
    <div class="menu-item">
      <div class="name">Home</div>
      <div class="sub-item">
        <a>link</a>
        <a>link</a>
      </div>
    </div>
    <div class="menu-item">
      <div class="name">Contact</div>
      <div class="sub-item">
        <a>link</a>
        <a>link</a>
      </div>
    </div>
  </div>
</nav>

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

Enhance your interface with stunning JQuery code snippets

We are in the process of developing a web application that will be fully 2.0 compliant (utilizing AJAX and more). Although I am primarily a rich-client and server-side developer, my knowledge of Javascript is limited to a functional level. However, based ...

Guidelines for incorporating Angular variables into jQuery scripts

I have a form with an input field that dynamically generates a list of names using Angular. My goal is to turn each name into a clickable link that will submit the form with that specific name as the input value. <form method="POST" action="/results/" ...

Toggle visibility of multiple DIVs with identical classes using radio buttons

I'm looking to streamline my use of radio buttons in HTML. How can I implement a single set of radio buttons at the top that will toggle the visibility of all DIVs with the same class on my webpage? <form> <input type="radio" name="csr_sel ...

Ways to retrieve JSON objects from the Controller using ajax

Currently, I am developing a mail validation controller using jQuery AJAX and PHP. Once the message is validated in the PHP controller, I attempt to retrieve it using AJAX and display it on the screen using jQuery. Here is an excerpt of my AJAX code: .. ...

Chrome browser not triggering first click event for AJAX jQuery checkbox

My Java application is built with Struts 1.1, utilizing actions, forms, and business objects. To enhance performance, I have integrated AJAX into my code to fetch data quickly and display it in a grid. However, I encountered an issue where clicking on a ch ...

Retrieving the current value of the selected option using JQuery

I am working on a feature where I have different quantities in selects after each button click. <button type="button" class="btn btn-sm btn-primary" id="addtocart2" >Button1</button> <select id="quantity1" class="ml- ...

Enhance the current model in backbone.js by incorporating additional data

When a user selects an item on the webpage, more details need to be fetched and displayed. The API function /api/full_details has been implemented to return the additional data for that item. Challenge: How can I retrieve the additional data and append it ...

Page not found: unique error on alternate route

My website has different paths and pages in hbs format, such as root, help, and 404. The problem arises when I navigate to localhost:3000/wrong, the site displays correctly, but when I try localhost:3000/help/wrong, the CSS is not applied to the 404 page b ...

outdoor checkboxes indicate completion

Whenever I click on the email address example [email protected], my first checkbox gets checked inexplicably... I have created a fiddle to demonstrate this issue... http://jsfiddle.net/pZ8jY/ <table class="checkboxes"> <tr> <td ...

Encountering AJAX Error 0 with jQueryUI Autocomplete upon pressing enter key

Currently, I am facing an issue with a search box that utilizes the jqueryUI .autocomplete feature to retrieve data through AJAX for providing suggestions. The problem arises when a user presses the enter key before the AJAX call to the source completes, r ...

End of container Bootstrap 4 row with two columns at the edge

I'm currently working on an angular project with bootstrap 4. Within the container, there are two rows. I specifically want one row to always be positioned at the bottom of the container. Despite trying methods like justify-content: flex-end;, botto ...

Menu bar placed atop a carousel

I am currently in the process of developing a website and I have encountered an issue with the navigation bar. I have a slider that I would like to appear below the navbar but the navbar is pushing it down instead. Ideally, I want the navbar to be transpar ...

Is VS Code highlighting compatible with template string CSS in Emotion?

Is there a way to enable syntax highlighting for emotion's template string css in VS Code? I've been looking for plugins, but all of them seem to focus on snippets. I have tried using css({ camelCaseCssProps:...}), which works. However, I am att ...

"Experiencing issues with the Ajax search functionality, need assistance

I am working on a JavaScript code to create search suggestions for a music search engine. The issue I am facing is that when the first character is typed, everything works fine. However, if I type more characters, all the results are displayed in a singl ...

Creating tooltips using React and Tailwind CSS

I'm currently working on creating tooltips for icons in my header. I have created a component that combines the icon and tooltip into one div, with Tailwind CSS styles applied. JSX Component: const HeaderIcon = ({ icon, redirect = window.location.pat ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

Step-by-step guide on positioning a div below another div and centering both elements horizontally

Trying to center the "input" div below the "InputBelow" div using "flex-direction: column" is causing issues with "justify-content; center." The goal is to center the main "border" div in the middle of the screen and align the other elements inside it. ...

I am facing an issue where the JsonResult is not recognizing the ICollection within an object when making a post

Struggling with transferring Json data from the client to Controller Action in MVC 4 using Knockout.Js. The Json is posted successfully, but upon binding in the JsonResults Action, an ICollection present in the Json is missing in the object produced. A sn ...

Spooky 'outline' emerges with rounded corners in Internet Explorer 11 and Microsoft Edge

I encountered an unusual issue in IE11 and Edge (on Windows 10) where a strange, transparent border appears in the HTML/CSS code. <!DOCTYPE html><html> <head> <style> body { background-color:red; ...

Use jQuery to compare the input values whenever they are modified

I am trying to synchronize the input values of two inputs as they are typed in. The code I have seems to work sometimes, but not consistently. Here is the code snippet: $('#google-querynav').keypress(function() { var text = $(this).val(); ...