The simple CSS trick to transforming a horizontal menu into a vertical menu for mobile

In my website publishing class, I am working on designing a responsive template. Leveraging my knowledge of jquery, I decided to switch the horizontal navigation menu on the desktop version to a vertical menu on mobile devices. Instead of displaying all navigation list items which break at a certain width, I want the menu to appear only when clicking on the "Navigation" text. I have successfully hidden the menu on mobile until clicking on the nav text. However, the issue I'm facing is that when the menu appears, it expands the height of the nav element which is its parent. How can I modify the code so that the height of the nav element remains unchanged and the list appears outside of the nav element directly below the Navigation text?

Check out this screenshot for reference: https://i.sstatic.net/jXKQU.jpg

CSS:

#navTrigger {
    display: none;
}
nav {
    margin-bottom: 10px;
    margin-left: 0px;
    background: url(../images/wood_dark.png);
    -webkit-box-shadow: 10px 2px 10px;
    box-shadow: 0px -5px 5px 0px rgba(0, 0, 0, 1), inset 0px 5px 10px 0px rgba(255, 255, 255, .3);
    border: 1px solid #555;
    border-bottom-color: #888;
    position: absolute;
    z-index: 0;
    width: 960px;
}
nav ul {
    list-style-type: none;
    margin-top: 0;
    margin-right: 35px;
    margin-left: 73px;
    margin-bottom: 0;
}
nav ul li {
    float: left;
    display: inline;
    border-right: 1px solid #888888;
    border-left: 1px solid #444444;
    margin: 0;
    padding: 0;
}

nav ul li a {
    color: #FFFFFF;
    text-decoration: none;
    font-size: 1.25em;
    padding: 10px 15px;
    display: block; 
}
@media only screen and (max-width:790px){
    #navTrigger {
        display: block;
        color: #fff;
        margin-left: 124px;
        padding: 10px 15px;
        font-size: 1.25em;
    }
    nav ul {
        display: none;
    }
    nav ul li {
        display: block;
        border-top: 1px solid rgba(255, 255, 255, .2);
        float: none;
    }
}

HTML:

<nav>
      <span id="navTrigger">Navigation</span>
      <ul>
        <li><a href="index.html" title="Home Page">Home</a></li>
        <li><a href="products.html" title="Our Products">Products</a></li>
        <li><a href="services.html" title="Our Services">Services</a></li>
        <li><a href="about.html" title="About Us">About</a></li>
        <li><a href="support.html" title="Help and Support">Support</a></li>
        <li><a href="contact.html" title="Contact Us">Contact</a></li>
      </ul>
    </nav>

Answer №1

"@media only screen and..." may not be effective in all cases...

Consider using @media(max-width: 790px)

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

I'm curious if there is a method to validate HTML elements within AngularJS. You can check out my app on Plunker [here](https://jsfiddle.net/4jttdczt/)

I'm a newcomer to angularjs and I am attempting to test HTML elements, but my tests are failing. Specifically, I would like to test the value or text contained within an HTML element. Can anyone provide guidance on how I can achieve this? Below is my ...

Using AngularJS to dynamically apply a CSS class to message text depending on the HTTP post response

I am looking to implement a method for adding a CSS class to the text color of a message based on the response. Currently, I achieve this by: if (response.data.status == 201) { angular.element(document.getElementById("msg")).addClass("text-green"); ...

Adjust the color and border of the checkbox using CSS styling

Looking for a checkbox with a white background and no curve Here is what I have tried: .cb { background-color: white; border: 1px; } <input type="checkbox" class="cb">Normal <input type="checkbox" class="cb">power <input type="checkb ...

Unable to change the background color of h1 tag in a ReactJS project

import React from 'react' export default function Home(){ return<div> <h1 className="bg-dark text-white p-3">Home Page</h1> </div> } I am attempting to modify the background color of the h1 tag in my Reac ...

Is there a way to fix the close button on the menu so that it works more than once?

I have developed a JavaScript-powered navbar, but I am facing an issue with the closing button. It seems to work only once. If I try to open and close the menu repeatedly, the closing button stops working. I suspect there might be an error in my code, but ...

How can I stop the serverclick event of an HTML button from being triggered when form validation fails?

I am facing an issue where I need to validate a form before the HTML button's serverclick event fires. However, the client and server events are triggering at the same time. HTML <button runat="server" id="imgLogin" type="submit" class="btn" styl ...

Is there a way to implement two distinct JavaScript functions - one for desktop and another for mobile - within the same onclick event attribute in an HTML document?

I'm currently working on a responsive HTML page that has versions for desktop and mobile. In order to properly execute specific functions based on the device's screen width, I have two separate functions - one for desktop and one for mobile - bot ...

Unable to access content from a dictionary in Django templates

Currently, I am attempting to retrieve the value of the class method that returns a dictionary. The function is structured as follows: class GetData: def __init__(self, api_key, ip, interface): self.api_key = api_key self.asa_ip = ip ...

Achieve a top position in CSS layout without resorting to negative values

This may seem like a simple thing, but I'm struggling to achieve it without using negative values for padding-top. Here's how my site looks: Here is the CSS code I am using: .center { width: 1030px; margin: 0 auto; } .top-panel { backgr ...

CSS problem with padding and width

Currently, I'm working on a conditional rendering issue where I want to give a div full width but due to padding applied to its parent, it's not expanding to the container's full width. Here is the snippet of the code: import Image from &qu ...

Is there a way to identify the specific list item that a draggable element has been dropped onto within a jQuery sortable with connected draggable lists?

Take a look at these two sets of items: user's inventory <ul id='list1'> <li>Apple</li> <li>Banana</li> </ul>available products <ul id='list2'> <li>Orange</li> ...

Can you explain how to set a title to align with the object on the far left within a Grid component using justify="space-between" in Material UI?

I am currently working on a Grid layout project that involves a Grid container with four Grid items. The first Grid item takes up the full width and contains a title, while the other three Grid items contain avatars. My goal is to align the avatars to the ...

choose the initial element within a class

Seeking a way to target the first element within each group of the same class? Consider the following scenario: <div class="group"> <div class="element"></div> <div class="element"></div> <div class="element"&g ...

Am I incorrectly linking Javascript/CSS files/pages?

<script src="scripts/lib/angular.min.js"></script> <script src="scripts/lib/angular-route.min.js"></script> <script src="scripts/lib/angular-animate.min.js"></script> <script src="scripts/lib/jquery.min.js"></sc ...

Achieve a parallax effect with background images while scrolling, all without relying on the

Is there a way to achieve a parallax effect on an image without using the css background and background-attachment properties? I have an img tag nested inside a div and I want to implement a parallax scroll effect on the image. Here is the code snippet: ...

Keep the bootstrap label text fixed below the input field

I'm working on creating a file upload style using bootstrap. The code below is functional, but I'm facing an issue where the label Choose file appears below the input field rather than in the middle. How can I adjust the positioning of Choose fil ...

The pop-up orientation is not being adjusted according to my CSS when it changes

I am currently working with Cordova 3.1.0 and Android 17. Here is the HTML code for my pop-up: <div id="rightNavPrpPopup1" data-role="popup" class="r-menu-dropdown"> <div class="r-menu-triangle"></div> <a class=" ...

What could be causing my background image to vanish or flicker in Bootstrap?

Something unusual is happening with my website. There's a section with a static background image, and as you scroll, a quote and button (which will eventually link to a store) appear on top of it. Previously, this setup was working fine. However, yest ...

Adding a MySQL-PHP combination programmatically

Currently, I am working on a custom HTML page that sends data to PHP variables which are then utilized to generate customized SQL queries. function load_table($db, $old_table, $startRow, $nameColumn, $ipColumn, $addressColumn, $cityColumn, $stateColumn, $ ...

"Refine Your Grid with a Pinterest-Inspired

I've set up a grid of images in columns similar to Pinterest that I would like to filter. The images vary in height but all have the same width. The issue arises when a taller image is followed by a shorter one, causing the short image to float right ...