The navigation bar I designed does not adapt well to mobile devices with a maximum width of 480px

I have implemented a custom drop-down navigation bar that is responsive to screens above 480 px in width. I have included a hidden menu icon for screens below 480 px, which when clicked should display the navigation bar along with the drop-down list. However, I am facing an issue where the drop-down list does not display after clicking the icon on screens below 480 px. My implementation is based on HTML5 and CSS3.

/*Styles the background-color of an active link*/
.menu ul .active{
color: #ffffff;
background: #red; /* For browsers that do not support gradients */
background: -webkit-linear-gradient(red 20%, green); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(red 20%, green); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(red 20%, green); /* For Firefox 3.6 to 15 */
background: linear-gradient(red 20%, green); /* Standard syntax */
background: linear-gradient(red 20%, green); /* Standard syntax */
}

.navbar{
width:100%;
max-width:1000px;
text-align:center;
margin-top:-8px;
margin-bottom:60px;
margin-left:160px;
}

.menu ul{
/*Removes bullets*/
list-style:none;
}

/*Styles each list within ul*/
.menu ul li{
background-color:green;
border:1px solid #ffffff;
width:100%;
max-width:173px;
height:35px;
line-height:35px;
margin:auto;
text-align:center;
/*Makes the list dispaly in a horizontal maneer*/
float:left;
position: relative;
border-radius:8px;
font: 15px;
font-weight:regular;
}

.menu ul li a{
text-decoration:none;
color:white;
display:block;
}


.menu ul li a:hover{
background-color:red;
border-radius:8px;
}

.menu ul ul{
position:absolute;
...
</body>
</html>

Answer №1

The issue at hand is that the #show-menu checkbox is not functioning as intended. To resolve this, simply include the following line in your CSS:

#show-menu:checked ~ .navbar .menu ul {display: block;}

By adding this line, clicking on the main menu button will trigger the desired action without the need for additional JavaScript functions.

Pro Tip: Run the snippet in full-page mode to adjust the window width accordingly.

.menu ul .active {
  color: #ffffff;
  background: #red;
  /* Cross-browser gradient support */
  background: -webkit-linear-gradient(red 20%, green);
  background: -o-linear-gradient(red 20%, green);
  background: -moz-linear-gradient(red 20%, green);
  background: linear-gradient(red 20%, green);
  /* Standard syntax */
  background: linear-gradient(red 20%, green);
}

.navbar {
  width: 100%;
  max-width: 1000px;
  text-align: center;
  margin-top: -8px;
  margin-bottom: 60px;
  margin-left: 160px;
}

... (additional CSS styles)
<!-- Sample HTML structure with menu items and submenus -->

<label for="show-menu" class="show-menu"> Menu </label>
<input type="checkbox" id="show-menu">

<div class="navbar">
  <div class="menu">
    <ul class="list">
      <li class="active"> Home </li>
      <li> <a href="humanities.html"> Humanities <span class="arrow">&#9660; </span> </a>
        <ul>
          ... (submenus and nested links)
        </ul>
      </li>
      ... (more menu items)
      
    </ul>
  </div>
</div>

Answer №2

In order to make your menu button work, you will need to incorporate a click event in JavaScript. I have written the code for this functionality in jsfiddle: https://jsfiddle.net/capozzic1/qacyh592/4/. To run the fiddle successfully, resize the HTML page by selecting the middle part of the page (located on the lower right-hand side) to emulate a mobile device, then click "Run" on the upper left corner. Next, click on the large menu button within the HTML to reveal the remaining navigation bar links.

If you click on the menu bar while it is already displayed, it will disappear again.

To implement this script:


var menuBtn = document.querySelector(".show-menu");
var menu = document.querySelector(".menu ul");
menuBtn.onclick = function(){

if (menu.style.display == "none") { 
    menu.style.display = "block";
} else { 
    menu.style.display = "none";
}

}

By clicking on the menu button, the rest of the navbar should appear. You can save this script into a file named script.js, and then include it in your HTML by adding the following line at the bottom of the body section:

"<script src="script.js"></script>" 

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 am looking for a single-page website that I can access after refreshing the page at a specific point

I recently created a one-page website with a div called sitecontent that has a width of 4400 pixels, housing four distinct "pages". These pages are located at intervals of 0px, 1100px, 2200px, and 3300px within sitecontent. To ensure the correct text is d ...

Animating elements on a webpage can be achieved by using both

Is there a way to create an animation that moves objects based on button clicks? For example, when the "Monday" button is pressed, the object with the correct color will move down. If any other button like "Tuesday" is clicked, the previous object will mov ...

Place an image in the middle of a div with text aligned to the right side

How do I center an image within a div that has text on the right side? The page layout is based on percentages and I want the image to be perfectly centered with the text floated to the right. This is what I currently have: IMG.logo { display: block ...

Setting header details for optimal data transfer

Currently, there is a Java code snippet that I am working on which attempts to access the header information sent by an HTML page. Unfortunately, I do not currently have the specific HTML page in question. However, I still need to be able to access and m ...

Using the linear-gradient method in CSS for border images

I managed to successfully create a design using the CSS properties border-image and linear-gradient. However, I am struggling to understand the syntax involved. While I grasp the concept that the series of colors defines the gradient transition, as well as ...

What is the reason for the malfunctioning of this button upon clicking?

Attempting to customize my personal website by making the sidebar width change when the sidebar button is clicked. I'm puzzled as to why it's not working as intended. I prefer to figure it out independently but any helpful tips would be appreciat ...

Optimizing Divs for Maximum Layout Efficiency

My current issue involves a series of divs that contain varying amounts of content. These divs are floated left and I am striving to align them seamlessly without any vertical space between them (except for the 10px margin that I have included). You can v ...

Scraping LinkedIn with Python and Selenium: Iterating over Work and Education Experience

I'm currently utilizing Python with Selenium to scrape data from LinkedIn profiles. While most of my code is functioning as expected, I'm facing challenges in extracting information related to each employer or school listed in the history section ...

Move dots or points smoothly from a starting position to their destination

How can I achieve animation using SVG or CSS? The animation should involve the following: Initially, only lines will be visible (picture 1). After 2 seconds, dots (.) will appear and join the line curves with some animated movement to the dots (picture 2 ...

I am looking to adjust the height of my MUI Grid component

Recently exploring React, and I'm looking to set a height limit for MUI Grid. I've structured my project into 3 sections using MUI grid components. My goal is to restrict the page height in order to eliminate the browser scrollbar. If the conten ...

Adding flair to a fresh element and then removing it upon its inception

I'm working with a JavaScript code that creates a new element when a button is clicked. I have a question about it. Here's the JavaScript code snippet: var comment = document.querySelector("#AddComment"); var req = new XMLHttpRequest(); if(comm ...

Incorporate a Font Awesome icon into a Bootstrap 4 callout

I am having issues adding an icon to the left side of a Bootstrap 4 callout. Currently, the icon is appearing above the H4 heading. I would like the icon to be positioned on the left side before the message. I have attempted using .p:last-child but it doe ...

Button Hover Effect: Transition with Style

I am trying to implement a one-second transition from the default color scheme to the hover color for a Button element in Material UI. I am using the transitions.create(props, options) method as described in this article: https://medium.com/@octaviocoria/c ...

Adaptive navigation bar featuring a drop-down menu and a branded logo placed strategically amidst sections

I'm having trouble making my navbar responsive. I want the links to collapse into a hamburger icon when the screen width is adjusted, displaying only the logo and hamburger icon with the links in portrait mode. The example at this link comes close to ...

Difficulty in accurately identifying the current page on the appbar

I'm attempting to make the active page in my navbar stand out by giving it a white background color and skyblue text. However, for some reason, I can't get the text color to display as skyblue in the appbar. You can see how it currently looks in ...

I'm confused as to why the icon color isn't changing on the Blogger homepage for each individual user

I'm experimenting with changing the eye color based on visitor count. It works perfectly fine on static posts in my Blogger, but it fails to update the eye color properly on my homepage according to the set numeric values. Instead of displaying differ ...

Decreasing progress indicator

I'm facing an issue while trying to decrement the progress bar when all checkboxes are unchecked. The value of the checkbox remains and does not decrease to 0. https://i.sstatic.net/Tye85.png Access the fiddle here. $('input').on(' ...

Tips for applying/styling "All Items Center" and "Space Between Items" in CSS and ReactJS

justify-content: space-evenly; seems to be working correctly, but the desired output is not achieved I am aiming for something like this: https://i.stack.imgur.com/iQoWK.png However, this is what I currently have: https://i.stack.imgur.com/1PxGR.png ...

Leveraging the power of WordPress Rest API alongside AngularJS ui-sref and customized HTML

Currently, I am working on setting up an AngularJS application that retrieves content from a Wordpress back-end using the wp-rest api v2. Everything seems to be functioning smoothly, however, I have noticed that certain HTML attributes are being removed. ...

Using Selenium WebDriver to locate elements by their XPath within a WebElement

I am facing an issue with locating an element in my code using the following snippet: elements = driver.find_elements_by_xpath("//div[@class='Display']") After successfully finding the elements, I want to access the second "Display" element and ...