Display issue with the responsive navigation bar

I am currently facing challenges while trying to create a responsive navigation bar. The way it is displayed on the page is not what I intended.

Below is an image of how it appears when the window is maximized:

https://i.sstatic.net/6NogJ.png

And here is how it looks when the window is resized:

https://i.sstatic.net/Fv6rv.png

This is the desired layout and functionality that I am aiming for:

https://i.sstatic.net/EPAEW.jpg

Current Issues:

  • The current display of links in the header, shown as "stretches, mobility" etc., should actually show "Join / Log In", as seen in Image 3.
  • Upon clicking on the menu, the navigation should dynamically display other links.

So far, I have attempted the following: https://jsfiddle.net/hudnybux/

Answer №1

After some adjustments, I was able to replicate the look of your screenshots almost perfectly. One important change I made was repositioning your nav-trigger element higher up in the HTML structure.

<div id="header-main">
    <div id="nav-trigger"><span>Menu</span></div>
    <nav id="main-navigation" role="navigation">
        <ul>
            <li><a href="#">Stretches</a></li>
            <li><a href="#">Mobility</a></li>
            <li><a href="#">Posture</a></li>
        </ul>
    </nav>
    <!--<nav id="nav-mobile"></nav>-->
</div>

I also removed the need for the nav-mobile navigation and adjusted the caret triangle next to "menu" by setting its width and height to 0.

width: 0;
height: 0;

Edit:

Upon further review, I have a new suggestion. Instead of using jQuery's slideDown and slideUp, consider implementing CSS transitions for dynamic animations. By simply applying a class, you can achieve the desired effect with more flexibility compared to inline styling from jQuery methods.

https://jsfiddle.net/qnco3x7e/8/

Answer №2

To achieve the desired layout on smaller screens, consider including an additional media query

@media all and (max-width: 460px) {
  nav#main-navigation li {
    display:block;
    border-bottom: 1px solid #fafafa;
  }
}

Answer №3

Utilizing flexbox CSS properties can greatly enhance your website layout capabilities. Check out this helpful resource for more information:

Answer №4

Assisting others with their code is not typically encouraged on Stack Overflow, but in the spirit of teaching through demonstration, I took the liberty of completing the task for you. Take note of the changes I made to your implementation and try to learn as much as you can from it!

The Approach

  • Maintain consistent HTML markup for the main menu (Stretches, Mobility, Posture) across different screen sizes instead of using JavaScript to duplicate it.
  • Start off with the same CSS styling for both menus; adjust the main menu to be horizontal in the media query for smaller screens.
  • Show everything by default and only use display: none on specific screen sizes where certain elements should not be displayed.

JSFiddle link

$(document).ready(function() {
    $("#main-nav-mobile-trigger span").click(function() {
        $(this).toggleClass("open");
        if ($(this).hasClass("open")) {
            $("#main-nav").addClass("open").slideDown(250);
        } else {
            $("#main-nav").removeClass("open").slideUp(250);
        }
    });
});
.pageOverlay {
    width: 900px;
    margin: 0 auto;
}

/* Remaining CSS styles provided in the original snippet */

The HTML Structure

  • I removed unnecessary container divs (#header and #header-main) since they didn't contribute to the layout.
  • The header area now consists of three parts:
    • #top-nav - Join/Login, Help, Shop links
    • #main-nav-mobile-trigger - MENU button
    • #main-nav - Stretches, Mobility, Posture menu items

The JavaScript Logic

  • Upon clicking the MENU button (#main-nav-mobile-trigger span):
    • Toggle the presence of the .open class.
    • If the class is added,
      • Apply the .open class to #main-nav and slide it down.
    • Otherwise,
      • Remove the .open class from #main-nav and slide it up.

The Styling Guidelines

  • Avoid redundant styling rules for multiple horizontal menus by consolidating them under a general selector like nav.
  • For large screens (>900px):
    • Right-align #top-nav and left-align #main-nav.
    • Hide the MENU button.
  • For small screens (<=900px):
    • If #main-nav doesn't have the .open class, hide it.
    • Display menu items in #main-nav horizontally.

Hopefully, this explanation proves beneficial for your understanding. All the best as you delve into more front-end development challenges!

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

What is causing the error when trying to parse a JSON with multiple properties?

Snippet: let data = JSON.parse('{"name":"dibya","company":"wipro"}'); Error Message : An error occurred while trying to parse the JSON data. The console displays "Uncaught SyntaxError: Unexpected end of JSON input" at line 1, character 6. ...

Enhancing search results with data retrieved from a jSON response

At the moment, I am using a logic to generate a list of titles. Now, I would like to include data from the response along with the code for each title in the list. const title = responseData.map(item => { return { label: item.title, val ...

Accidental delay upon mouse exit

Upon hovering over, the transition immediately begins, but there is a delay of about 300ms before the reverse transition starts upon hovering off. (https://example.com) <div id="isa-hover-gallery"> <a href="https://example-link.com"> <div ...

Encountered an Angular 1.6 Error: The requested URL at http://127.0.0.1:56035/[url] could not be found (404

I've hit a roadblock in my project and can't seem to figure out the issue. The file I'm working on is supposed to display an iframe video player with a YouTube embed. I tried using interpolation to construct the correct URL: <div class= ...

The functionality of cloning an object is experiencing issues on a webpage containing jQuery code generated by a PHP script

<li style="padding: 5px; width: 150px; overflow: hidden; float: left; height: 202px;"> <div class="title"> <a href="/index.php?option=com_virtuemart&amp;view=productdetails&amp;virtuemart_product_id=68&amp;virtuemart_category ...

When using AutoComplete in MUI, I encountered an issue while attempting to assign a default value to the checkbox from an API. Instead of achieving the desired result, I received an error stating "(inter

Within this snippet, I am seeking to retrieve a default value from the API to populate a checkbox initially. I have employed the Material-UI Autocomplete component, which includes a defaultValue prop. Despite my efforts to utilize this prop, I am encounter ...

How can I eliminate the default background of Mui tooltip and personalize it in react?

Below is an example of how to customize a nested tooltip within a default background tooltip in MUI. The challenge here is to remove the grey border in the customized tooltip and have only a white background with black text. Check out the code snippet be ...

Access an external URL by logging in, then return back to the Angular application

I am facing a dilemma with an external URL that I need to access, created by another client. My task is to make a call to this external URL and then return to the home page seamlessly. Here's what I have tried: <button class="altro" titl ...

The Route.get() function in Node.js is expecting a callback function, but instead received an unexpected object of type

Recently, I started coding with nodejs and express. In my file test.js located in the routes folder, I have written the following code: const express = require('express'); const router = new express.Router(); router.get('/test', (req ...

Stay connected with AJAX's latest updates on Twitter with just 13 bytes

Twitter sends a POST request of only 13 bytes when someone follows an account. This small amount of information helps to reduce latency and server load, providing advantages for web developers. However, removing unnecessary cookies and extra information f ...

The functionality of Jquery .slideToggle("slow") seems to be glitchy when used as a table expander

I attempted to implement the .slideToggle("slow"); feature to my table, following the instructions detailed here: W3Schools The toggle effect seems to be functioning, but not as expected. I want the effect to behave similar to the example on the W3School ...

The lathegeometry in Three.js is causing incorrect rendering of faces

Greetings! I am eager to delve into the world of webgl and three.js. My journey began smoothly, but encountered a bump when attempting to use lathe geometry for crafting a circular health bar. To better understand the concept, I stumbled upon this enligh ...

Comparing the use of Next.js API routes to Express.js API

What are the technical differences between using Next.js with API routes and running a Node.js server with Express.js? Can Next.js alone with API routes be used to develop a full-stack web application with MongoDB? Is it safe to connect to and modify the ...

Unable to include below the heading for the images

As I venture into the world of HTML, I am facing a challenge with placing titles below two images. Every time I attempt to add either h or p, the text ends up on the right side instead of below the images. What could I be doing wrong? This is all part of ...

"Reconfigure web page layout to be perfectly centered

Currently, I am utilizing Angular Drywall to construct my website. By default, the alignment is centered which does not suit the template requirements that call for it to occupy the full 100% body width. body{ width:100%; margin-left:0; paddin ...

A rapid tool for efficient HTML parsing based on libxml

Hey there, I've encountered an error message that's a bit puzzling - it says "Extra argument 'endocing' in call", even though it's within the method so technically not an extra argument. Any idea why this is happening and how I can ...

I aim to have just the footer section perfectly aligned within the screen, without the entire body extending beyond

I am encountering an issue with the footer tag once again. I need this section to occupy the entire screen, without affecting the rest of the body content. That is why I omitted setting margin:0; for body elements. I attempted to apply margin:0 to the foo ...

Web API 2 failing to deserialize JSON data

I have been facing an issue while calling a web service API in my application. I am passing a parameter, but the JSON is not deserializing as expected. Instead, it is treating the JSON as a string. Below is the code for the API: [Route("api/v1/images/GetM ...

does not output any console log statements

I am attempting to showcase the values of checkboxes on the console, however, it is not working. <input type="checkbox" id="id_price" value="1" onclick="display_img()">Under £200<br> <input type="checkbox" id="id_pr ...

JavaScript has the ability to sort tables using tags

I am currently working on a Vue project where I am attempting to filter my table based on the tags that users click. I have created a method that outputs all the rows containing the tag clicked by the user. <el-table-column prop="Keyword" labe ...