A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correctly. I have attempted to adjust z-index and explored various solutions online without any success. Can someone please point out what I might be doing wrong?

The two dropdowns causing the problem are labeled as "select a category" and "item-list" in the code snippet below. The dropdown coding was adapted from this source:

I have tried to simplify the code provided:

 /*CSS styling code*/
 
<!DOCTYPE html>
  <html>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <head>
    </head>

    <body>

      <div class="grid-container">

        <div class="header" style="display: inline-block">
        </div>
        <div id="navigation">
        </div>


        <div id="services">
          <div tabindex="0" class="dropdown-2-button">
            <div class="dropdown-2" id="dropdown3">
              <a href="item1.php">item 1</a>
              <a href="item2.php">item 2</a>
              <a href="item3.php">item 3</a>
              <a href="item4.php">item 4</a>
              <a href="item5.php">item 5</a>
            </div>
          </div>
        </div>


        <!-- More HTML code here -->
        
      </div>
    </body>

  </html>

Answer №1

The main issue revolves around focus. Maintaining hover functionality for mobile while focusing on desktop is crucial.

UPDATED CSS-

.grid-container {
    background: #e2e5aa;
    display: border-box;
    padding: 0px;
    margin: 0px;
    background-size: cover;
  }

  .header {
    grid-area: header;
  }

  #navigation {
    grid-area: navigation;
  }
  #services {
    grid-area: services;
  }
  .drop-down {
    grid-area: dropdown;
  }
  #introduction {
    grid-area: description;
  }
  #client-terminal {
    grid-area: client;
  }

  /*added for mobile*/
  .my-dropdown:hover .dropdown-content {
    opacity: 1;
    visibility: visible;
  }

  @media screen and (min-width: 250px) {
    .grid-container {
      width: 100%;
      height: 100%;
      display: grid;
      grid-template-areas:
        "header header header client"
        "navigation navigation navigation dropdown"
        "description description description description"
        "services services services services";
    }

    .my-dropdown {
      color: #e2e5aa;
      position: relative;
      padding: 4px;
      background: #790d76;
      border: none;
      cursor: pointer;
      font-size: 15px;
      font-weight: normal;
      float: right;
      z-index: 1;
    }

    .drop-down {
      position: relative;
      display: inline-block;
      background-color: #790d76;
    }

    .dropdown-content {
      visibility: hidden;
      position: absolute;
      background-color: #e2e5aa;
      overflow: hidden;
      pointer-events: auto;
    }

    #navigation {
      background-color: #790d76;
    }

    #client-terminal {
      background-color: #290c28;
    }
  }

  @media screen and (min-width: 640px) {
    .grid-container {
      width: 100%;
      height: 100%;
      padding: 0px;
      margin: 0px;
      display: grid;
      grid-template-areas:
        "header header header header"
        "navigation navigation navigation client"
        "dropdown dropdown description description"
        "dropdown dropdown services services"
        "footer footer footer footer";
    }
  }

  @media (min-width: 768px) {
    .my-dropdown:focus .dropdown-content {
      opacity: 1;
      visibility: visible;
    }
  }

UPDATED HTML-

<div class="grid-container">
      <div class="header" style="display: inline-block"></div>
      <div id="navigation"></div>

      <div id="services">
        <div tabindex="0" class="dropdown-2-button">
          <div class="dropdown-2" id="dropdown3">
            <a href="item1.php">item 1</a>
            <a href="item2.php">item 2</a>
            <a href="item3.php">item 3</a>
            <a href="item4.php">item 4</a>
            <a href="item5.php">item 5</a>
          </div>
        </div>
      </div>

      <div class="drop-down">
        <div tabindex="0" class="my-dropdown">
          <div class="dropdown-content">
            <a href="p1.php">p1</a>
            <a href="p2.php">p2</a>
            <a href="p3.php">p3</a>
            <a href="p4.php">p4</a>
            <a href="p5.php">p5</a>
          </div>
        </div>
      </div>

      <div id="client-terminal"></div>
      <div id="introduction">
        <div class="a_b-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="cr-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc <br /><a href="https://abc.php">test link</a></p>
        </div>
        <div class="sheq-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="c-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="m_m-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
      </div>
    </div>

HTML AND CSS ERRORS ARE LISTED BELOW-

this should be - display: inline-block
<div class="header" style="display: inline-block"></div>

CSS errors corrected:
.my-dropdown:before {
          content: "Price-List";
          color: #e2e5aa;
          position: relative;
          padding: 4px;
          background: #790d76;
          border: none;
          /* cursor: ; */ /*error*/
          font-size: 15px;
          font-weight: normal;
          float: right;
          z-index: 1;
        }
    
        .dropdown-content {
          visibility: hidden;
          position: absolute;
          background-color: #e2e5aa;
          overflow: hidden;
          pointer-events: auto;
        }

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

Page flickers when jQuery is used to scroll to an element

One issue I have encountered is with a link that, when clicked, should scroll down to a specific element on the page. However, every time I test this feature, there seems to be an inconsistency where the page may momentarily flash before scrolling as inte ...

Transfer groups between divisions

In my HTML structure, I have two main divs named Group1 and Group2. Each of these group divs contains at least two inner divs, all with the class .grp_item. Currently, the grp_item class is set to display:none, except for one div in each group that has a c ...

Utilizing Python to manipulate the 'select' tag in HTML

Currently, I am attempting to retrieve events from an HTML page located at In my efforts to choose different areas using python script, I encountered a challenge with the following snippet of HTML code: <select data-ng-options="key as value.name for ( ...

Refreshing the information in the database table

Upon receiving data from the server using ajax, I populate this table: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +data[i].B ...

Is it possible to add file content to the beginning rather than the end?

Looking for help with my form that allows staff to submit messages to our updates page. I currently have this code: $filename = "files/armaupdates"; $text = $_POST['update']; $updatesep = "<hr><br><hr><br>"; $fp = fopen ...

What is the best way to arrange these containers in a horizontal stack?

Check out the code snippet at: https://codepen.io/shayaansiddiqui/pen/YzGzeOj I'm looking to align these boxes horizontally, how can I achieve that? <div> <div class="container"> <img src="#" ...

Is there a way to create a responsive navbar using flexbox that automatically transitions into two rows at smaller screen sizes?

I've designed a sleek navbar located at the bottom of my webpage featuring a central logo leading to the home page, with two links on each side directing users to specific pages. Using @media CSS, I made it responsive by reducing its size as the scree ...

Steps to position an image without a background in the middle of a smaller container:

My HTML includes the following code: <div style="width:400px;height:300px;overflow:hidden;"> <img src="http://d39kbiy71leyho.cloudfront.net/wp-content/uploads/2016/05/09170020/cats-politics-TN.jpg" /> </div> Take a ...

Adjust the size of a div using absolute positioning

Can a div with absolute position be resized? I need this pink modal to maintain the same width as the input and resize accordingly. This modal will be utilized in a dropdown component, so it should resize along with the input. Moreover, is using absolute ...

If the div is devoid of content, then remove the class

<div class="slider">content goes here</div> <div id="slider-2" class="active inactive">also some content here</div> <script type="text/javascript"> function checkEmpty( element ){ return !$.trim(element.html()) ...

Hey there, I was wondering if it's possible to modify the color of the navbar when scrolling on a specific page

After the first 3 pages with a black background and a transparent navbar with white navigation items, I encounter visibility issues when scrolling to the colorful 4th page. Is there a way to change the navbar color on this specific page using ONLY HTML a ...

Tips for eliminating stuttering when fixing the position of an element with JavaScript

I am currently facing an issue with a webpage I created where the text freezes when scrolled down to the last paragraph, while the images continue to scroll. Although my implementation is functional, there is noticeable jankiness when using the mouse wheel ...

Customizing the size of dateBox icons in JQuery Mobile

Is there a way to increase the button size within the input box of a dateBox? Currently, it appears small and difficult to click accurately. Additionally, it would be beneficial if clicking anywhere in the input field could open the calendar, instead of ju ...

Exploring the distinctions among different material design frameworks

Confirming my grasp of the situation here. Material design is an interface building approach developed by Google. Material lite is Google's public implementation of this concept. serves as an independent open-source adaptation. In addition, Angul ...

Click to alter the style of an <li> element

I'm currently using Angular CLI and I have a menu list that I want to customize. Specifically, I want to change the background color of the <li> element when it is clicked. I am passing an id to the changeColor() function, but unfortunately, I a ...

A guide to changing the height of a column in Bootstrap

I've utilized Bootstrap to create features in the HTML, and I'm attempting to make each box's height consistent. Check out a screenshot of the boxes here #hr-1 { width: 100px; margin-left: 20%; } #features { background-color: #f0 ...

Adjusting the height of the Tinymce Editor in React Grid Layout after the initial initialization

I am facing a challenge with my React Component that displays a tinymce editor. The task is to dynamically adjust the height of the editor after it has been initialized. To achieve this, I am utilizing the "React Grid Layout" package for resizing the compo ...

Navigation menu consist of two horizontal rows

I'm facing a challenge. I have a total of 8 links (one being a button) on a navigation menu that I need to display in two rows on the right side. I'm having difficulty aligning them properly while also justifying them to the right. Essentially, t ...

Limit access to a specific route within the URL

Is there a way to ensure that users can access and download myfile.pdf without being able to see or access /root, /folder, or /subdirectory in the URL? This functionality can be implemented using HTML, Angular 6, ReactJS, or similar frameworks. ...

What is the proper way to execute a JavaScript function within a JavaScript file from an HTML file?

I have the following content in an HTML file: <!DOCTYPE html> <!-- --> <html> <head> <script src="java_script.js"></script> <link rel="stylesheet" type="text/css" href="carousel.css"> & ...