Incorporating a CSS drop-down menu that appears or changes upon clicking

I am in the midst of designing a webpage with a main div that is 1000px by 1000px. Within this main div, there is a horizontal bar located at the top, divided into four sections - each occupying 1/4 of the space. In the center of each section, there is text enclosed in an h2 tag that should be both horizontally and vertically centered. Additionally, I want each section to trigger a drop-down menu when clicked.

While attempting to implement the drop-down menu using checkboxes (for visibility control on both mobile and desktop), I encountered a problem. The checkbox itself is quite small, making it difficult for users to click and toggle its visibility effectively. My goal is to have the drop-down menu appear whenever a user clicks anywhere within the 1/4 section area.

The layout of the row of 1/4 section drop down menus can be seen in the following image: https://i.sstatic.net/a3IdR.png

Please note that the functionality shown in the image is not yet operational.

Here is the HTML code snippet:

<div id="Media_Choices">
    <div id="Video" class="media_choice"> <h2>Video▼</h2> </div>
    <div id="Pictures" class="media_choice"> <h2>Pictures▼</h2> </div>
    <div id="Audio" class="media_choice"> <h2>Audio▼</h2> </div>
    <div id="Stories" class="media_choice"> <h2>Stories▼</h2> </div>
</div>

And here is the CSS code snippet:

#Media_Choices {
    width: 100%;
    max-height:40px;
    min-height:40px;
}
.media_choice {
    display: inline;
    float: left;
    width: 24.5%;
    max-height: 38px;
    min-height: 38px;
    text-align: center;
    vertical-align: middle;
    line-height: 38px;       /* the same as your div height */
}
#Video {
}
#Pictures {
}
#Audio {
}
#Stories {
}

If you are able to create a dynamic arrow (changing from ▼ to ▲) based on the state of the drop-down menu, that would be greatly appreciated. Feel free to explore alternative methods beyond the checkbox approach, as long as they are compatible across different platforms.

Prior attempts at utilizing checkboxes for dropdown menus were based on the code provided below [obtained from another source], but simply replacing the text inside the box was not sufficient:

<input class="dropdowninput" type="checkbox" id="dropdownbox1"/>
<div class="dropdownbox">
    <label for="dropdownbox1">Open dropdown</label>
    <ul class="dropdown">
        <li>...</li><li>etc</li>
    </ul>
</div>
with CSS:

.dropdowninput, .dropdown {display:none;}
.dropdowninput:checked + .dropdownbox .dropdown {display:block;}

Answer №1

If I understand your request correctly, you are looking to develop a dynamic dropdown menu that changes its arrow icons when the menu expands or collapses. One way to achieve this is by implementing event listeners on the menu items to toggle the visibility of submenus upon click. Using a combination of CSS and JavaScript, you can accomplish this:

.media_choice > h2:after {
    display: inline-block;
    content: '▼';
}
.media_choice.dropped > h2:after {
    content: '▲';
}
.media_choice > ul {
    display: none;
}
.media_choice.dropped > ul {
    display: block;
}

To add event listeners in JavaScript:

$(document).ready(function()
{
    $('.media_choice').on('click', function()
    {
        $(this).toggleClass('dropped');
    });
});

View JSFiddle example

Answer №2

Check out this implementation with checkboxes and no JavaScript.

nav {
  width: 80%;
  margin: 20px auto;
}
nav ul {
  margin: 0;
  padding: 0;
  list-style-type: none;
  overflow: none;
  /* to contain the floats */
}
nav li {
  padding: 0;
  margin: 0;
  width: 25%;
  float: left;
  position: relative;
  white-space: nowrap;
}
nav input {
  display: none;
}
nav label {
  display: block;
  border: 1px solid black;
  padding: 10px;
  cursor: pointer;
}
nav label:hover {
  background: #ccc;
}
nav a {
  display: block;
  padding: 10px;
}
nav a:hover {
  background: #ccc;
}
nav label:after {
  content: '▼';
  font-size: 10px;
}
nav ul ul {
  display: none;
  position: absolute;
  top: 100%;
  border: 1px solid red;
  box-sizing: border-box;
  background: #fff;
  width: 100%;
}
nav ul ul li {
  width: 100%;
  float: none;
}
nav input:checked ~ ul {
  display: block;
}
nav input:checked ~ label:after {
  content: '▲';
}
<!-- http://codepen.io/allicarn/pen/gPPmZZ -->
<nav>
  <ul>
    <li>
      <input type="checkbox" id="navitem1" name="navinputs" />
      <label for="navitem1">Menu Item #1</label>
      <ul>
        <li><a href="#">Sub Menu Item #1a</a></li>
        <li><a href="#">Sub Menu Item #1b</a></li>
        <li><a href="#">Sub Menu Item #1c</a></li>
        <li><a href="#">Sub Menu Item #1d</a></li>
      </ul>
    </li>
    <li>
      <input type="checkbox" id="navitem2" name="navinputs" />
      <label for="navitem2">Menu Item #2</label>
      <ul>
        <li><a href="#">Sub Menu Item #2a</a></li>
        <li><a href="#">Sub Menu Item #2b</a></li>
        <li><a href="#">Sub Menu Item #2c</a></li>
        <li><a href="#">Sub Menu Item #2d</a></li>
      </ul>
    </li>
    <li>
      <input type="checkbox" id="navitem3" name="navinputs" />
      <label for="navitem3">Menu Item #3</label>
      <ul>
        <li><a href="#">Sub Menu Item #3a</a></li>
        <li><a href="#">Sub Menu Item #3b</a></li>
        <li><a href="#">Sub Menu Item #3c</a></li>
        <li><a href="#">Sub Menu Item #3d</a></li>
      </ul>
    </li>
    <li>
      <input type="checkbox" id="navitem4" name="navinputs" />
      <label for="navitem4">Menu Item #4</label>
      <ul>
        <li><a href="#">Sub Menu Item #4a</a></li>
        <li><a href="#">Sub Menu Item #4b</a></li>
        <li><a href="#">Sub Menu Item #4c</a></li>
        <li><a href="#">Sub Menu Item #4d</a></li>
      </ul>
    </li>
  </ul>
</nav>

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 the Form's data-url Attribute

My form tag is set up like this: <form data-id="213" method="post" onsubmit="javascript: startAjax(); return false;"> <input type="submit" value="submit"> </form> When I submit the form, an ajax script runs to validate some information ...

Tips for sharing a variable with an external JavaScript file in ASP.NET

Utilizing the variables from default.aspx.cs within the script of the default.aspx file has been achieved successfully with the following code: public partial class default: System.Web.UI.Page { public string _file = string.Empty; public string _i ...

When developing a website with HTML/CSS, is it recommended to utilize tables for organizing content

Seeking guidance as a novice in web development. I am planning to design a table with input fields that will be sent to the server upon submission. What is the preferred method for achieving this? Is it advisable to utilize html-tables or would it be more ...

Tips for enhancing the flexibility of the owl carousel

I've been trying to make four items fit on a medium screen and two on a mobile device, but no matter what I do - adjusting widths, heights, columns, and responsive classes like col-6, col-md-3, col-lg-3 - nothing seems to work well. I could really use ...

The ElementNotInteractableException was thrown because the element could not be accessed via the keyboard when trying to input text into the FirstName field on Facebook

The issue is as follows: Exception encountered in thread "main" org.openqa.selenium.ElementNotInteractableException: Element is not accessible via keyboard Here is the code snippet: System.setProperty("webdriver.gecko.driver"," ...

What could be causing this Ajax request to come back with an error?

I am facing an issue with an AJAX request to a WordPress PHP function. The strange part is that the response is being treated as an error. However, when I check the error log, the responseText contains the requested JSON data, the status is 200, and statu ...

How can I dynamically generate HTML based on the path of an SVG that is clicked, and what is the best approach to display or hide this generated HTML on the web page?

Question I'm facing an issue with my SVG implementation where Users can click on various body parts, causing the clicked body part to gain the 'active' class. This class styles my CSS and sets variables in my JS. The challenge I'm enco ...

Obtain the name of the current view in ASP.NET MVC 5 using Razor on the .cshtml side

As a student who is new to ASP.NET MVC and coming from ASP.NET Web Forms, I am accustomed to it. Here is the list: <ul class="sidebar bg-grayDark"> <li class="active"> <a href="@Url.Action("Index", "Home")"> < ...

Should I generate an array or pull data directly from the database?

Hey there, I've got this JavaScript app and could really use some input or tips. Here's the idea: Users log in to try and defeat a 'boss', with each player working together in the game. Let's say the 'boss' has 10 millio ...

What is the best way to organize two different menu types - one based on ID and the other in a more traditional format - in order to create

I am working on a website at where the menu is id based to scroll on the page for menus, except for the publication and forum pages. How can I ensure smooth sorting of the menu when moving from the publication section to education or other sections, sim ...

How to refresh a div in a dynamic Bootstrap modal using jQuery?

I am attempting to update a specific DIV element within a bootstrap modal by using a button with a Load Function from JQuery. However, when I click the button, the DIV remains hidden. More specifically, my aim is to incorporate a comment box within this m ...

jQuery - Comparing Nested and Unnested Transition and Animation Event Handlers/Listeners

At the moment, I am working on a block of code that triggers every time a specific ajax call is made. The snippet I'm using looks like this (although it might throw an error due to absence of html or css - but that's not my concern as I just wan ...

When you download a file through the unpkg CDN, the size of the npm package is

I am experiencing a discrepancy with the file size of a file in my npm package. The file is 307kb in size, but when I download it through unpkg, the same file is only 73.2Kb. I find it quite puzzling how the file can be smaller when downloaded over the net ...

Two divs with floated attributes and equal heights

My unique HTML is below: <div class="container"> <div class="box"> <div class="float"> <img src='http://media-cdn.tripadvisor.com/media/photo-s/03/9b/2f/db/miami-beach.jpg' /> </div> ...

Tips for ensuring all AJAX requests have finished before moving on to the next one when regularly using a function with AJAX requests

One of the key challenges I'm facing in my code is with an ajax request, shown below: function show_detail() { $('#product_'+index).html('<img src="/loading.jpg" style="margin-left:450px;"> loading...'); $.ajax({ ...

Issue with Ajax not functioning properly for Jquery autocomplete feature in PHP

The ajax request shown below is creating an array of values that I want to use in a jQuery autocomplete function: var whatever = []; $.ajax({ url: "myScript.php", success: function (response) { whatever = response.split(","); } }); Th ...

Convert HTML Tables to PDF Format

I am facing an issue with exporting my table data into a PDF using jQuery. I have made sure to install all the necessary library files, but my code doesn't seem to be working correctly. Can anyone spot any errors in my code or suggest what might be mi ...

The execution of Node.js callback function is not triggered

After setting up an API that queries a MongoDB and returns JSON Objects, I decided to modularize the code. I moved the MongoDB functionality to a separate file called queryMongo.js, which is now called in the POST request of main.js. Here is the main.js co ...

Strange problem with Mongoose and MongoDB arises while operating MEAN application on Openshift platform

My journey with Openshift has been quite eventful. From making drastic changes to learning about version-ing and nvm (Node Version Manager), it has been a rollercoaster ride. Dealing with npm versions to ensure compatibility with the server used express ve ...

Issues with CSS Min-Height in Various Web Browsers

Lately, I've been working on a website and trying to create a border that surrounds all my content and extends the full length of the page. The #Container div is supposed to expand to fill the entire page. I've tried using min-height:100%; in my ...