Can CSS be used to create drop down menus?

Is it possible to create a dropdown menu using only CSS, similar to what you would find in most applications? I have a simple menu bar at the top of my screen and I want to be able to hover over an item like "Item1" and see sub-items appear below it.

I envision the dropdown menu as a complete feature that allows me to easily add new menu items through HTML or CSS. The main purpose of this menu is for users to click on an option (e.g. "Author") and then view a list of related items (e.g. Books) associated with that selection.

Here is the initial code snippet for creating a basic menu bar:

<!DOCTYPE html>
<html>
<head>
<style>
ul {
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li {
float:left;
}
a {
display:block;
width:60px;
background-color:#dddddd;
}
</style>
</head>

<body>
<ul>
<li><a href="#home">Menu1</a></li>
<li><a href="#news">Menu2</a></li>
<li><a href="#contact">Menu3</a></li>
<li><a href="#about">Menu4</a></li>
</ul>

</body>
</html>

A failed attempt at creating a dropdown menu by adding more elements to the original menu:

<!DOCTYPE html>
<html>
<head>
<style>
ul{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li{
float:left;
}
a{
display:block;
width:60px;
background-color:#dddddd;
}
</style>
</head>

<body>
<ul>
<li><a href="#home">Menu1</a>  <-- not working
<ul>
<li><a href="#home">Menu1</a></li>
<li><a href="#news">Menu2</a></li>
<li><a href="#contact">Menu3</a></li>
<li><a href="#about">Menu4</a></li>
</ul>
</li>
<li><a href="#news">Menu2</a></li>
<li><a href="#contact">Menu3</a></li>
<li><a href="#about">Menu4</a></li>
</ul>

</body>
</html>

Functional code for a dropdown menu sourced from fiddle:

Providing this code reference for future developers seeking a working example of a dropdown menu implementation.

<ul>
    <li class="mainmenu"><a href="#home">Menu1</a> 
        <ul class="submenu">
        <li><a href="#home">Menu1</a></li>
        <li><a href="#news">Menu2</a></li>
        <li><a href="#contact">Menu3</a></li>
        <li><a href="#about">Menu4</a></li>
        </ul>
    </li>
    <li><a href="#news">Menu2</a></li>
    <li><a href="#contact">Menu3</a></li>
    <li><a href="#about">Menu4</a></li>
</ul>

CSS styling for the dropdown menu:

ul {
    list-style-type:none;
    margin:0;
    padding:0;
    overflow:hidden;
}
li {
    float:left;
}
a {
    display:block;
    width:60px;
    background-color:#dddddd;
}
li ul{
    position:absolute;
    display:none;
}
li:hover ul{
    display:block;
}

Answer №1

To implement a vertical submenu, you can make the following CSS adjustments:

li ul{
position:absolute; // Ensures other menu items are not pushed to the right
display:none;
}

li:hover ul{
display:block;
}

For a live example, you can view this JSFiddle

Update:

If you prefer the submenu to be displayed vertically, remove the float:left from the li and include the following CSS rules:

li {
display:inline-block;
}
li ul li{
display:block;
}

For a demonstration, you can refer to this JSFiddle link

Answer №2

To hide the submenu, apply display:none;

When hovering over the main menu, make the hidden submenu visible with

.mainmenu:hover .submenu {display:block;}

Check out this JSFiddle for a demonstration: http://jsfiddle.net/abc123/

Answer №3

To create a hidden list that becomes visible on hover, simply add a new UL under the title and keep it hidden until hovered over. Here is an example:

<ul>
    <li><a href="index.php">index</a></li>
    <li><a href="faq.php">FAQ</a></li>
    <li>more services
          <ul>
                <li><a href="r.php">reseller</a></li>
                <li><a href="h.php">hosting</a></li>
          </ul>
    </li>
    <li><a href="contact.php">contact</a></li>
</ul>
<style>
     ul > li > ul{display:none;}
     ul > li:hover > ul{display:block;}
</style>

It's important to note that using only CSS and HTML will allow this action to occur on hover but not on click like in the code provided.

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 dynamics of Express.js functionalities

I am seeking to better understand how flow functions within an Express app using Routes, with the following set of Routes: app.use(require('./routes/reportsRouter')); app.use(require('./routes/crewsRouter')); app.use(require('./ro ...

Using table.column as a function in jQuery datatabbe is not supported

When using jquery datatable, I encountered an error stating "table.column is not a function." <script> $(document).ready(function() { var table = $('#lsotable').dataTable(); $("#lsotable thead th").each( function ( i ) { ...

"Resolving an inconsistency problem with the Vary header in Htaccess

Could someone help me identify the issues in my htaccess code? I'm encountering errors: This response is negotiated, but doesn't have an appropriate Vary header. The resource doesn't send Vary consistently. ## add vary header <IfModule ...

"Utilizing Jquery to extract data from a form field and combine it with a URL for maximum value

Today, I am delving into the world of jQuery for the first time and finding myself in need of some assistance with a particular problem. While this issue may seem trivial to experienced individuals, it is posing quite a challenge for me. The dilemma lies ...

Finding Text Between Two Distinct Strings Using Regular Expressions in JavaScript

I am currently utilizing AJAX technology. My approach involves sending a GET request that retrieves a raw HTML string representing the content of a webpage. I am grappling with the challenge of isolating all elements enclosed between div tags: <div ro ...

Execute JavaScript code prior to sending a Rails form

Before submitting the form, I would like to trigger the html5 geolocation javascript code. Here's how I envision the process: upon form submission, the user is prompted with a geolocation popup. After allowing access, the form data should be processed ...

Guide to setting up a click event for a group of input items, specifically radio buttons

I am looking to trigger some JavaScript code whenever a user clicks on any of the radio buttons in my web application. Despite my efforts, I am having trouble capturing a click event on the list of input elements. Currently, in my app, I have included the ...

Automate the process of launching the <select> picker through programming

In an Android WebView, I need a way to programmatically trigger the opening of the select element picker provided below. <select id="select1" multiple="multiple"> <option value="MDO1" selected="selected">MDO 1</option> <option val ...

Tips for configuring Visual Studio Code to utilize path mappings for handling automatic imports

In order to streamline my project and avoid messy paths, I am implementing absolute paths that will allow for consistent imports regardless of the file's location in the project tree. For this purpose, I made adjustments to the tsconfig.json: "paths ...

What is the best method for choosing all options in a select box in IE without experiencing the scrolling effect?

Here's the scenario: I have a select element with multiple options and a checkbox that allows users to select/deselect all options. The issue arises in Internet Explorer, where selecting all options using code causes the entire select box to scroll un ...

Struggling with CSS due to the INCLUDE Function

After downloading a Template + CSS File for the website I am working on, everything was going smoothly until I decided to break down the template into separate files for easier modification and editing in the future. However, once I cut out the head sectio ...

Node.js: Troubleshooting a forEach Function Error

I am encountering an issue with my nodejs script that is causing a "function not found" error after trying to insert data from json files into Firestore. How can I resolve this? Thank you for your help. Below is my code snippet: var admin = require("f ...

Tips on saving a cookie using universal-cookie

I followed a solution on Stack Overflow to set a cookie in my React application. However, the cookie expires with the session. Is there a way I can make this cookie persist beyond the session so it remains even when the browser is closed and reopened? ex ...

Best practices for refreshing the HTML5 offline application cache

My website utilizes offline caching, and I have set up the following event handler to manage updates: applicationCache.addEventListener('updateready', function () { if (window.applicationCache.status == window.applicationCach ...

What could be causing the lack of data with 'react-hook-form'?

I have encountered an issue while working with react-native and using 'react-hook-forms' for creating dynamic forms. The problem is that the data object returned is empty, even though it should contain the values entered in the input fields. When ...

Is there still excess top padding or margin in Firefox after clearing the default reset for the <ul> element?

My floated horizontal list is looking great on IE and Opera, but for some reason, Firefox is adding extra padding or margin at the top. I'm not sure how to fix it. Everything was fine until I had to add a display:inline to an image link next to it to ...

Using jQuery to convert JSON data into an array

My JSON data structure is as follows: { "default": [ [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ], [ 1325876000000, 0 ] ], "direct": [ [ 13 ...

Discover how to efficiently load and display a JSON array or object using JavaScript

I am new to learning about json and d3, having just started a few hours ago. While I have basic knowledge of javascript, I need help with loading a json file and displaying all the arrays and objects on the console using d3. I tried to do it myself but unf ...

Can the rxjs take operator be utilized to restrict the number of observables yielded by a service?

As someone who is just starting to learn Angular, I am working on a website that needs to display a limited list of 4 cars on the homepage. To achieve this, I have created a service that fetches all the cars from the server. import { Response } from &apos ...

Managing various names of child objects in a recursive manner

I'm currently facing a challenge while working on a nested JSON feed using D3.js Although my code is functioning properly when the child object is named children, I am interested in displaying nodes for other objects as well, not just the ones labele ...