Tips for ensuring child li pushes parent li down in mobile navigation

Hello there,

I'm having trouble with mobile navigation. I have a main navigation menu with some hidden subnavigation items. When I click on a parent item, it opens the corresponding subnav, but it covers up all other parent items that come after it.

Is there a way for the child item to "squeeze in" between the parent items by pushing them downward?

$(".main-nav").find("li").click(function() {
  $(this).find(".subnav").toggle();
});
.main-nav {
  border: 1px solid red;
  position: relative;
}

.main-nav li {
  display: inline-block;
  width: 100%;
}

.subnav {
  border: 1px dashed blue;
  position: absolute;
  display: none;
  background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="main-nav">
  <li>Nav 1</li>
  <li>Nav 2</li>
  <li> Nav 3
    <ul class="subnav">
      <li>Subnav 1</li>
      <li>Subnav 2</li>
    </ul>
  </li>
  <li>Nav 4
    <ul class="subnav">
      <li>Subnav 1</li>
      <li>Subnav 2</li>
      <li>Subnav 3</li>
    </ul>
  </li>
  <li>Nav 5</li>
</ul>

Thank you for your help!

Answer №1

Modify the .subnav position to be relative:

$(".main-nav").find("li").click(function() {
  $(this).find(".subnav").toggle();
});
.main-nav {
  border: 1px solid red;
  position: relative;
}

.main-nav li {
  display: inline-block;
  width: 100%;
}

.subnav {
  border: 1px dashed blue;
  position: relative;
  display: none;
  background-color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="main-nav">
  <li>Nav 1</li>
  <li>Nav 2</li>
  <li> Nav 3
    <ul class="subnav">
      <li>Subnav 1</li>
      <li>Subnav 2</li>
    </ul>
  </li>
  <li>Nav 4
    <ul class="subnav">
      <li>Subnav 1</li>
      <li>Subnav 2</li>
      <li>Subnav 3</li>
    </ul>
  </li>
  <li>Nav 5</li>
</ul>

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

Exploring etoro data through python web scraping

Attempting to automate the process of logging into my etoro account and extracting data from my portfolio using Selenium. Currently working on Google Colab, here is what I have so far: from selenium import webdriver options = webdriver.ChromeOptions() opt ...

Cheerio Package in Node.js: Parsing HTML Data to Extract Specific Attributes

basically, this is the specific page source that caught my attention. I am attempting to extract the "data-productsku" value for all items within the entire page source. Here is what I have tried so far: var sku = ($(".productListItem .itemContainer spa ...

Data loss occurring in Gridview populated by jQuery after postback

I am facing a challenge with a gridview that I am populating clientside using javascript (jQuery). I need to find a way to retain the client-side data when the page posts back to the server. Is there a recommended method to achieve this? Below is the jQue ...

What are some alternative ways to position-style anchor elements without using absolute positioning or a wrapping div?

Here is the HTML code snippet that I am working with: <div class="top"> <div class="header title">Some Big Header Goes Here</div> <div class="sub-header title">The fancyness enters here.</div> <a hr ...

TinyMce editor's WYSIWYG settings are not functioning properly on IE9

Incorporating TinyMce into my application has been a success. However, one problem I encountered was that when users typed something in the editor and hit enter, an extra blank line would be inserted. To resolve this issue, I utilized force_p_newlines: fal ...

Adding a 'dot' to the progress bar in Bootstrap enhances its visual appeal

I am currently working on a progress bar and I would like it to look similar to this: https://i.sstatic.net/TSDEy.png However, my current output looks like this: https://i.sstatic.net/rQhYc.png I'm puzzled as to why the tooltip is floating there, ...

Retrieving JSON data using the fetch method in JavaScript

Currently, I am attempting to retrieve a JSON array using AJAX. Upon calling my test.php file, the following content is returned: [{"name":"James","age":24},{"name":"Peter","age":30}] This is the JavaScript code that I am using: var persons = new Array( ...

What is the reason for Safari 13 not displaying the outline during focus when a transition is applied?

In the current scenario, focusing on the button in Safari 13.0.5 does not display the outline until a repaint is forced (such as changing screen size). This issue does not occur in other browsers. Are there any workarounds or hacks to ensure the outline ...

Leveraging the power of map in an Angular typescript file

I've been attempting to populate a Map in Angular by setting values dynamically. When certain buttons are clicked, the onClick function is invoked. typeArray: Map<number,string>; Rent(movieId: number){ this.typeArray.set(movieId,"Rental ...

Unable to perform the 'setSelectionRange' function on the 'HTMLInputElement' due to the input element's type being 'number', which does not allow selection

My attempt to enable text selection in an input box upon user click by using the code snippet below was unsuccessful: <input type="number" onclick="this.setSelectionRange(0, this.value.length)" name="quantity" /> Instead of achieving the desired ef ...

The alignment of my divs is all out of whack -

Currently, I have styled my website to work perfectly in Firefox, but it's not appearing correctly in Chrome. You can see the layout at . My goal is to maintain the layout as seen in Firefox while also making sure it displays properly in Chrome and IE ...

Do iframes not utilize parental jquery?

I have a homepage that utilizes jQuery by loading it from the ajax google APIs in the head> tag. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.js" type="text/javascript"></script> I attempted to use jQuery functio ...

The non-disappearing nature of -webkit-appearance

I have been attempting to eliminate the -webkit-appearance:none; property using jQuery, but all my efforts have been unsuccessful. Here are the methods I have tried: jQuery(document).ready(function(){ jQuery("select").removeAttr('style'); j ...

retrieve the parent frame's request URL

I am faced with the task of writing a JSP code that retrieves the getRequestURL() of the parent frame (the URL shown in the address bar) from a child frame. However, when I attempt to do this, I only obtain the URL of the child frame. Is there anyone who ...

Notify when a certain element's ID is visible on the screen using the jQuery appear method

Having trouble getting this plugin to cooperate: https://github.com/morr/jquery.appear I attempted to reference the plugin from a CDN using Ajax: http://cdnjs.cloudflare.com/ajax/libs/jquery.appear/0.3.3/jquery.appear.js Why isn't it working as expe ...

Prevent horizontal scrolling on mobile devices

To prevent vertical scrolling on mobile devices, I attempted using the CSS code: body {overflow-x:hidden} This successfully disables vertical scrolling in regular browsers. However, if there is an element wider than the screen on mobile, it still allows ...

Discovering the quantity of pagination tabs in Selenium WebAutomation

A critical element in the table "ContentPlaceHolder1_gvChannelList" is Pagination, located at the last row. I need to determine how many pages/columns are present in this pagination table. The code driver.findElements(By.id("//*[@id='ContentPlaceHold ...

HTML Techniques: Flipping the Script - Writing Characters from Right to Left

In order to showcase the temperature in degrees Celsius, I have implemented a placement technique using absolute positioning. Consequently, the °C symbol will persist in its designated spot. To achieve the desired presentation, the text should be displaye ...

Issues with JQuery `.click()` event

Check out this snippet of code I'm working with: $(".item").click(function () { alert("clicked!"); }); I also have (hypothetically; in reality it's more complex) the following HTML on my page: <a href="#" class="item"> ...

Offering various language options on a website determined by the URL

I've been contemplating how to add multi-language support to my personal website, which I developed using ExpressJS and NodeJS with EJS as the template engine. Currently, the website is only available in English, but I want to add a German version as ...