Building a drop down menu with HTML and CSS: a step-by-step guide

Looking for a way to create a dropdown menu for the list item with the id "gameDesignPage" in my HTML structure:

<div class="MenuContainer">
    <div class="Menu">
        <div class="MenuContents">
            <ul>
                <li><a id="index" href="index.html">Home</a></li>
                <li><a id="gamePage" href="gamePage.html">Game</a></li>
                <li><a id="gameDesignPage" href="gameDesignPage.html">Game Design</a></li>
                <li><a id="devRolesPage" href="devRolesPage.html">Developer Roles</a></li>
                <li class="float-right"><a id="about" href="about.html">About</a></li>
            </ul>
        </div>
    </div>
</div>

Interested in creating a dropdown similar to the one on this example, but struggling to implement it in my current structure despite trying solutions from other sources.

Answer №1

Instead of placing the dropdown trigger in a list and the dropdown menu in a nested list, I prefer creating a self-contained dropdown component with a hidden nested list within the parent visible list. This setup keeps things organized and tidy.

To achieve this, you can create a nested list with display: none and then use CSS to apply display: block styling on hover of the parent list item. While the dropdown menu can be shown directly under the list item, I prefer styling it to be positioned absolutely below the content upon hover for a more visually appealing effect.

This approach allows the nested list to be visible when hovering over the parent list item or button. Note that the button styling is inspired by the example on the listed w3 page - https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button.

I have also simplified the structure by removing unnecessary layers of nested divs for this solution.

.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown-menu {
 list-style: none
}

.dropdown-menu-item {
 position: relative
}

.dropdown-menu-list {
 display: none;
 border: solid 1px #e1e1e1;
 background: #fff;
 position: absolute;
 left: 0;
 top: 50px;
 padding:0;
 list-style: none
}

.dropdown-menu-list-item {
 background: #fff;
 padding:  8px 16px;
}
.dropdown-menu-list-item:hover {
 background: #fafafa;
}


.dropdown-menu-list-item  a,
.dropdown-menu-list-item:hover a{
 text-decoration: none;
 display: block;
 cursor: pointer
}

.dropdown-menu:hover .dropdown-menu-list ,
.dropdown-menu-item:hover .dropdown-menu-list:hover{
 display: block;
}
<div class="menu-container">
  <ul class="dropdown-menu"> 
    <li class="dropdown-menu-item">
      <button class="dropbtn">Dropdown</button>
      <ul class="dropdown-menu-list">
        <li class="dropdown-menu-list-item"><a id="index" href="index.html">Home</a></li>
        <li class="dropdown-menu-list-item"><a id="gamePage" href="gamePage.html">Game</a></li>
        <li class="dropdown-menu-list-item"><a id="gameDesignPage" href="gameDesignPage.html">Game Design</a></li>
        <li class="dropdown-menu-list-item"><a id="devRolesPage" href="devRolesPage.html">Developer Roles</a></li>
         <li class="dropdown-menu-list-item" class="float-right"><a id="about" href="about.html">About</a></li>
       </ul>
     </li>
   </ul>    
</div>

Answer №2

Hopefully this meets your expectations.

<html>
<head>
<style>
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {background-color: #f1f1f1}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
</style>
</head>
<body>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
  <a href="index.html">Home</a>
  <a href="gamePage.html">Game</a>
  <a href="gameDesignPage.html">Game Design</a>
  <a href="devRolesPage.html">Developer Roles</a>
  <a href="about.html">About</a>
  </div>
</div>

</body>
</html>

For more information, visit the following link: https://www.w3schools.com/css/tryit.asp?filename=trycss_dropdown_button

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

The code in the head section is not running as expected

I've been exploring the possibilities of using lambda on AWS in combination with api gateway to create a contact form for a static S3 website, all inspired by this informative blog post: https://aws.amazon.com/blogs/architecture/create-dynamic-contact ...

The act of looping with for loops can be quite disruptive to

I need to display some basic 3D objects (specifically THREE.Sphere which contains 1-30 other THREE.Spheres with RGB color and no texture) in a somewhat real-time animation. The rendering process is quick, but I am facing issues with simple iterative calcu ...

Opacity: When the value is set to 0, the element remains invisible even when hovered over

I'm encountering an issue with configuring an element to have absolute positioning and a width and height of 100% while setting its opacity to 1.0. To summarize, here is the HTML code I am working with: <ul class="properties"> <li style ...

Can an entire application built with a combination of PHP, JavaScript, and HTML be successfully converted to Angular 7?

After creating a complex application that heavily relies on JavaScript, PHP, HTML, and numerous AJAX calls, I am considering migrating the entire codebase to Angular 7. Is it feasible to achieve this transition without requiring a complete rewrite in Ang ...

Issue with Email Signature on replies

Recently, I had some email signatures designed and they appear great when sent out, but encounter issues upon receipt or reply: The logo gets distorted and occasionally enlarges significantly during replies. The font color switches to black when receivin ...

Unable to function in simplistic html/php code, 'Require' fails to operate as intended

I am facing an issue while attempting to import a header.php file into my index.php file. For some reason, it is not working as expected. header.php: <!DOCTYPE html> <html> <head></head> <body> <header> & ...

Do HTML buttons without values get sent in a form submission?

When you have a button with the following code: <button class="button yellow" type="submit" name="button">Log in</button> and you submit it, what exactly gets sent to the server for this specific button that has a name attribute but no value ...

Place the HTML images in the center of the page

I would like the two images to be centered on the page. Please refer to this visual representation for guidance: Here is the code snippet: * { box-sizing: border-box; } .column { float: left; width: 28%; height: 28%; padding: 5px; } /* Cle ...

Tips for getting the sidebar to move down on mobile devices

When viewing my website on a mobile device, the sidebar appears above my products. I would like it to be positioned below my products. The sidebar is currently on the left side with content on the right. Is there a more effective way to create a sidebar ...

Firefox not rendering responsive YouTube embed properly

This method of embedding seems to be functioning well on all browsers except for Firefox; I took advantage of a free trial at crossbrowsertesting.com to verify. I’m not using a direct iFrame embed, and all the solutions I’ve come across are related to ...

The angular2-webpack-starter kicks off with a simple static page

When starting my project using angular2-webpack-starter, the initial loading of index.html allows us to create our own components. However, in my case, I am looking to first direct users to a static page without any Angular code before clicking a button th ...

Clicking on text triggers image display

My journey in coding is just starting and I have a good understanding of the basics of HTML, CSS, Javascript, and jQuery. I am trying to make an image appear when I click on text but struggling with the implementation. I'm working on a restaurant web ...

What is the best technology to implement for a band website design?

I'm in the process of creating a website for my friend's band, and I need some creative input. The site will feature minimal content such as a bio, news, and embedded audio/visual material. While my web development skills are decent, I'm loo ...

The functionality for dragging elements is not functioning properly in JavaScript

Here is the code I used in an attempt to make a div element draggable: let div = document.querySelector('div') div.onmousedown = function() { div.addEventListener('mousemove', move, true) } window.onmouseup = function() { window. ...

Assign a CSS class to a specific option within a SelectField in a WTForms form

Could someone explain the process of assigning a CSS class to the choices values? I am looking to customize the background of each choice with a small image. How can this be done using wtforms and CSS? class RegisterForm(Form): username = TextField( ...

What is the best way to center the image on a page?

How can I center an image on the screen with a caption? <div id="team-area"> <div class="container"> <div class="row"> <div class="col-12"> <h3 class="main-title">Our Team</h3> < ...

Implementing two background images and dynamically adjusting their transparency

With the challenge of loading two fixed body background-images, both set to cover, I encountered a dilemma. The text on the page was extending below and scrolling, along with top and bottom navigation icons. As expected, the second background covered the f ...

Apply a class to a div element when the WooCommerce cart is devoid of any items

Is there a way to apply a class or style to an element that displays the cart count only when the cart is empty? Currently, I have the following code in my header.php file which shows the cart count correctly, but does not update the color: header.php ( ...

Activate the textbox without utilizing `.focus()` method

I am encountering an issue with a small iframe on my page. The content within the iframe is larger than the window itself, requiring users to scroll around to view it in its entirety. Within this iframe, there is a button that, when clicked, triggers an an ...

The operation of 'val' is not supported by HTMLInputElement

While working with a table row, I am iterating through each cell. Inside every cell lies a text box containing some value that I need to store in an array. function dothing() { var tds = $('#'+selected+' td'); var submi ...