Placing the submenu nested within the main menu item for mobile devices, while positioning it below the main menu for desktop

This is a general question. Here is the proposed menu design for desktop: https://i.sstatic.net/4Ynfy.png

and this is how it would look on mobile:

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

I have experimented with different approaches:

1: Utilizing two separate headers and toggling their visibility using CSS, which makes styling easy but may not be ideal for screen readers.

<header class="mobile-header">
</header>
<header class="desktop-header">
</header>

2: Using a single header structure:

<header>
<h1 aria-label="Brandname">
  <a class="" href="/">
    Brandname
  </a>
</h1>

<nav class="main-menu-wrapper" role="navigation">
    <button class="toggle-mobile-menu" aria-expanded="false">Menu</button>
        <ul class="main-menu">
          (menu items here)
        </ul>
</nav>

</header>

I like the syntax of the second approach, and it works well on mobile devices. However, I am unsure how to style it effectively for desktop views. One idea was to use position: absolute for submenus, but this might require JavaScript for calculating heights. Is there a simpler solution?

For reference, here is an example website with similar functionality:

Desktop View:

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

Mobile View:

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

It appears that they have used two separate header elements, but I am looking for a way to achieve this without duplicating headers.

Answer №1

.main-menu{
display:flex;
flex-wrap: nowrap;
}
  ul{
      display: flex;
      gap:50px

    }
  
<header>
<h1 aria-label="Brandname">
  <a class="" href="/">
    Brandname
  </a>
</h1>

<nav class="main-menu-wrapper" role="navigation">
    <button class="toggle-mobile-menu" aria-expanded="false">Menu</button>
        <ul class="main-menu">
            <li><a href="/">Menu Item 1</a>
                <ul id="addclasshere">
                    <li>Submenu Item 1</li>
                    <li>Submenu Item 2 </li>
                </ul>       
            </li>
            <li><a href="/">Menu Item 1</a>
                <ul id="addclasshere">
                    <li>Submenu Item 1</li>
                    <li>Submenu Item 2 </li>                    
                </ul>
            </li>
            <li><a href="/">Menu Item 3</a>
                <ul id="addclasshere">
                    <li>Submenu Item 1</li>
                    <li>Submenu Item 2 </li>                    
                </ul>
            </li>   
        </ul>
</nav>

</header>
<body></body>

To create a dropdown menu with only one visible item, you can use JavaScript with

document.getelementById().addclass()
and define another CSS class with visibility="visible. Alternatively, you can refer to W3Schools for guidance.

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

Are Your Padding Styles Missing?

I've noticed that the text under the photos on this page in the main section (a.event) is not displaying the 5px padding top and bottom. Any suggestions for fixing this? Thank you! =) a.event { width:315px; height:auto; border:0; padding: 5px 0; } ...

Position the ion-radio element in the middle of the layout

As I work on creating a privacy page in HTML using Ionic 3, my desired output should resemble the following: https://i.sstatic.net/lxzc9.png However, with my current code, the result I'm seeing is different from what I expected: https://i.sstatic.net ...

Enhance Animation Fluidity with HTML/CSS

I am experimenting with creating a floating animation for my iceberg logo by adjusting the top margin. Below is the CSS code I have implemented: img { height: 60px; padding: 5px; -webkit-animation: logofloat 1s ease-in-out infinite alternate; -moz ...

Banner with video background (not taking up the entire page)

I'm currently working on a website project that involves using a YouTube video as the banner, but I'm facing some challenges in making it work correctly. So far, this is what I have: #video-background { position: absolute; right: 0; top:1 ...

Only scroll the div if it is not within the visible window

I've been looking at this snippet: The sidebar division is what I'm focusing on right now. I want the div to scroll along as I scroll down the page, but I need it to stop scrolling when its bottom is in view. The same should apply when scrollin ...

Confused by navigating with php

I've been attempting to convert this link into a PHP foreach loop without any success, and I'm new to Stack Overflow. I've used an array to generate the code, but I am uncertain of how to transform this code into a foreach loop. <ul class ...

Notifications for AngularJS tabs

I need help finding a method to incorporate "tab notification" using AngularJS, in order to show that there are important alerts that require attention. For example: (1) (3) TAB_ONE TAB_TWO TAB_THREE Could you provide any recom ...

Adjusting the spacing of the first letter using CSS, leave room

I'm encountering issues with the alignment of titles and text using the 'Lato' Google font. It seems like there is a space or kerning issue on the first letter causing them not to align properly to the left. Any suggestions on how to fix thi ...

"Maximizing Space: A Guide to Setting BootStrap Navbar on the Right Side of the Page with Dropdown Width

Take a look at the image below. There is a navbar toggle button on the upper right corner of the page. When I expand it, the dropdown items occupy the entire width of the page, causing an issue where clicking anywhere within the red box triggers the dropdo ...

Alignment of Validation Summary in a Vertical Layout

Currently, I am facing an issue with a two-column layout page that includes a Validation Summary (VS) control above the columns. Although the VS is centered in the container above the columns as desired, I am struggling to achieve vertical alignment for th ...

flask admin template not displaying javascript

Looking for guidance on implementing this template found at https://github.com/secondtruth/startmin. You can view a live demo here: The HTML file functions correctly when opened locally in a web browser, but there seems to be an issue with loading the Ja ...

Show an Angular Mat Card beneath the Input Field and position it on top of all other div elements

I am designing a signup page and I need to include a material card below the password field with checkboxes for password requirements. The challenge is that when the card appears, it pushes down all other elements such as the sign-up button. I want the ca ...

Display all list items in a single line with position set to absolute

Hey there, I'm currently working on creating a menu using the absolute position CSS rule for each list item. Unfortunately, setting display: inline-block for the parent doesn't seem to be doing the trick. Here's a snippet of the markup I&apo ...

Arrow Buttons for Expanding and Collapsing Sections on an

I'm attempting to include a downward arrow when the section is closed and an upper arrow when it's open - at the right side of each tab's head in the accordion. Here is the HTML I have implemented. It contains for-each loops. -- JavaScri ...

The menu bar fails to maintain its responsiveness once the breakpoint is reached

The issue I am facing is that the menu bar does not resize properly when the browser window size is less than 900px. This results in the bar being too wide, some content going off the screen, and a horizontal scrollbar appearing. I have been trying to tro ...

Using absolute positioning for child elements within a table cell division in CSS

Similar Question: Is it possible to position items relatively or absolutely within a display: table-cell? I am working with a display: table layout example. You can view the demo here. In the second panel, there is a #pos div that has a position: abs ...

Website issues being displayed by Internet Explorer

Greetings! Let me begin by saying how wonderful this page is - I have already found many answers here before. Please forgive any errors in my English :) In my efforts to optimize my website for SEO and increase its speed, I made several changes. The site ...

"Troubleshooting: Why is my Bootstrap modal window only printing a

I've encountered an issue with printing the content of a Bootstrap modal window. Previously, the code I had was working fine but now it seems to be malfunctioning. Content is dynamically added to the form using appendChild() in a separate function. Ho ...

Displaying a pyramid of numbers with JavaScript

Is there a way to create a pyramid number structure in JavaScript? for (i = 1; i <= 5; i++) { var k = ' '; var myspace = ''; for (j = 0; j < i - 0; j++) { k += i; myspace += ' '; } console.log(myspace + ...

Transpiling Sccs/Sass code into CSS

Is there a way to create a compiler that can convert SCCS/SASS code into CSS without using a VSCode extension? I'm curious about what programming language and basic concepts are needed for this task. Any insights would be greatly appreciated. Thanks! ...