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

Elastic canvas to always match the full size of the container or screen

I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio. The problem arises when th ...

Unable to place within div

Utilizing HTML5 to implement the "DnD" function. There is a button that adds divs. I want to be able to drag items into these newly added divs. Currently, I can only drag into existing divs and not the ones added later. How can I resolve this issue ...

Ways to extract data from a chosen radio button choice

Excuse me, I have a form with an input field and radio buttons. Can someone please help me with how to retrieve the value of the selected radio button and then populate that value into the input text field? For reference, here is the link to the jsfiddle: ...

What is the best way to switch back and forth between two div elements?

I've been attempting to switch between displaying div .cam1 and div .cam2, however, I can't seem to get it to work. Here's the code snippet in question: HTML: <div class="cam1"></div> <div class="cam2"></div> CS ...

Show using foreach, with a modification of the spacing between rows and columns

For my project, I need to display the same image 24 times with a 3x8 matrix on an A4 size page using a foreach method. The issue I'm encountering is that I have to manually add space between each row and column, but it cannot cause the page to break. ...

The outcome from the PHP file was JSON data, refrain from interpreting it as plain text

I've written PHP code in the file update_customer.php to save customer information. <?php date_default_timezone_set('Asia/Ho_Chi_Minh'); $response = array( 'status' => 0, 'message' => '', 'ty ...

The Ajax success callback unexpectedly displays the number "1" in the top left corner of the empty page instead of updating the specified div

Currently, I am in the process of developing a Yii2 application. I have encountered an issue where I select data from a Bootstrap modal popup and submit it to a controller action that contains an insert query. The problem arises after submitting the data f ...

Manipulating the information pulled from an external PHP script that is currently being displayed

Although I don't consider myself a professional, I am determined to learn some web languages this summer to be able to turn my design ideas into prototypes. Currently, I am struggling to figure out how to manipulate elements that are being echoed bac ...

I'm having trouble with fixing the TypeError that says "Cannot read properties of undefined (reading 'style')." How should I address this issue

I'm having trouble with a slideshow carousel on my website. When the site loads, only the first image is shown and you have to either click the corresponding circle or cycle through all the images using the arrows for the image to display. I'm al ...

Designing the child table layout using grid grouping in ExtJS4

I have customized my Extjs4 grid grouping implementation to hide the expand-collapse sign and prevent group header expansion for empty groups. However, I am facing an issue where a white 1px border is being created due to the following CSS class: .x-grid- ...

The issue of ColdFusion JSON not being handled by JQuery

I am working on a straightforward ColdFusion component that interacts with a database and returns a JSON object <cfcomponent> <cfsetting showdebugoutput="false" enablecfoutputonly="true"> <cffunction name="getNew" access="remote" returntype ...

Rotating a CSS div causes the page to scroll horizontally

Currently, I am working on creating a unique design for the footer of my website. However, when implementing it, I encountered an issue that is causing a horizontal scroll (red background added for clarity). To address this problem and prevent the page fr ...

You can eliminate the display flex property from the MuiCollapse-wrapper

Having trouble removing the display flex property from the MuiCollapse-wrapper, I did some research and found the rule name for the wrapper in this link https://material-ui.com/api/collapse/ I have been unsuccessful in overwriting the class name wrapper t ...

Unveiling the mystery of extracting information from a string post serialization

When working with a form, I am using this jQuery code to fetch all the field values: var adtitletoshow = $("#form_data").serialize(); After alerting adtitletoshow, it displays something like this - &fomdata1=textone&fomdata2=texttwo&fomdat ...

Making a linked div with background image

I'm attempting to turn the small icons on this particular page () into clickable links. Each icon is contained within its own div, but no matter what I do: <div class="social-btn pinterest"><a href="http://www.pinterest.com/etc.etc.">&l ...

Consistent Row Heights for Dynamic Width Elements

I've relied on Chris Coyier's Equal Height Blocks in Rows jQuery script for various projects, and it has consistently delivered great results. However, this time I am facing a new challenge as I work on a responsive website with a fluid width ma ...

Can someone please tell me the specific HTML entity that Flickr uses for the arrow icons in the action menu?

When viewing an image on FLickr () and clicking on the Actions menu button, a pop-up menu with an arrow icon appears. Interestingly, this icon is not a simple image, but rather a combination of two characters (◢◣). Has anyone been able to determine th ...

substituting symbols with colorful divs

I'm looking to add some color to my text using specific symbols. (), ||, and ++ are the symbols I'm using. If a text is enclosed in | symbols, it will appear in blue, and so on... Here is the code in action: const text = "|Working on the| i ...

Choose to automatically update the page after adding new information

The information is displayed in this format: <select class="custom-select col-md-10" id="routeList" size="8"> <option selected>Choose...</option> <?php include( '../cnn.php' ); $query= $db ...

In a remarkable design with an array of buttons and an individual div assigned to each, a fascinating functionality unfolds. Whenever a

https://i.stack.imgur.com/emhsT.png When I click on the first "Respond" button, I want the adjacent text box to disappear. Currently, if I click on the first "Respond" button, both text boxes will disappear instead of just the first one. $('.comment ...