Creating CSS-only drop-down menus with CSS3 transitions

I am in the process of creating a dropdown menu that works perfectly on desktop. I also want it to be responsive, where all list items are stacked underneath each other when a user clicks a menu icon.

Currently, when a user hovers over 'menu item 1', the sub-menu appears and disappears on hover. It doesn't look very elegant. Is there a simple way to incorporate a CSS3 transition to make this dropdown more visually appealing?

Thank you!


CSS

.sub-menu{
 display:none;
}

li.sub-menu-parent:hover .sub-menu {
 display: block;
}

HTML

 <nav>
   <ul>
     <li class="sub-menu-parent"><a href="#">Menu Item 1</a>
       <ul class="sub-menu">
         <li><a href="#">Sub Item 1</a></li>
         <li><a href="#">Sub Item 2</a></li>
         <li><a href="#">Sub Item 3</a></li>
         <li><a href="#">Sub Item 4</a></li>
       </ul>
     </li></ul>
     <li><a href="#">Menu Item 2</a></li>
     <li><a href="#">Menu Item 3</a></li>
     <li><a href="#">Menu Item 4</a></li>
     <li><a href="#">Menu Item 5</a></li>
   </ul>
 </nav>

Answer №1

If you want a smooth transition while hiding content, avoid using display: none. Browsers cannot smoothly transition from none to block, resulting in an instant switch.

An alternative method to hide the .sub-menu is to utilize visibility: hidden; followed by visibility: visible; on hover. However, this approach might complicate transitioning. By simply applying transition: all 0.5 ease, the menu will fade in smoothly but quickly disappear when :hover is removed.

For a more detailed explanation, refer to a comprehensive article that covers this technique. Essentially, it involves incorporating a transition-delay for visibility and then eliminating that delay on hover.

.sub-menu-parent { position: relative; }

.sub-menu { 
  visibility: hidden; /* hides sub-menu */
  opacity: 0;
  position: absolute;
  top: 100%;
  left: -10%;
  transition: all 0.5s ease 0s, visibility 0s linear 0.5s; /* final value sets transition-delay for visibility */
}

.sub-menu-parent:hover .sub-menu {
  visibility: visible; /* displays sub-menu */
  opacity: 1;
  left: 0;
  transition-delay: 0s; /* removes transition delay for immediate display during other style transitions */
}

DEMO: http://codepen.io/shshaw/pen/gsFch

Answer №2

Great for creating beautiful navigation menus, especially when incorporating sub-level navs that appear on hover.

Check out this helpful resource for animations and effects.

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

Once the form is submitted, it is not validated and instead just refreshed

<html> <head> </head> <body> <script type="text/javascript" language="javascript"> function isUpperCase(str) { return str === str.toUpperCase(); } function validate() { var flag=0; var x=document.getElementById("f ...

Implementing Mouse Scroll Click Functionality in ASP.NET with C# (Without JavaScript)

How can I add a Mouse Scroll Click (Middle Button) event in ASP.NET and C#? I have already looked into the MouseWheel Event, but it did not provide the solution I was looking for. This is the order in which mouse events occur: MouseEnter MouseMo ...

Restricted footage accessible through RESTful API

Hey there! I'm currently working on implementing authentication protected audio/video streaming in an Angular app using REST API. The main objective is to ensure that the audio/video content is secure and not accessible to users who are not logged in. ...

Incorporating JSTree Into Your Website's Heading: A Step-By-Step

I'm in search of a way to integrate the following code snippet as a JSTree into the heading section of a webpage, depicted below: <div id="jstree"> <ul> <li>Core 1 <ul> & ...

Having trouble getting the Bootstrap 5 Modal to function properly within an Electron app

Facing an issue with my web app using Bootstrap 5, as the modal is not displaying properly. Below is the HTML code for the modal and the button that triggers it: <div class="modal" tabindex="-1" id=&quo ...

Tips for positioning a btn-group within a btn-toolbar to the right in Bootstrap 4

I'm facing an issue with my btn-toolbar. It contains two btn-group, and by default, they are aligned to the left. However, I need the second toolbar to be aligned to the right. This is the snippet of my code: <link href="https://stackpath.boots ...

Saving HTML form data to a server-side text file

All. I've discovered various scripts that allow you to save form input to your local computer, such as downloading a text document with the submitted information. However, I am trying to create an HTML form that, when the 'submit' button is ...

I am facing issues with the CSS in my EJS file when it is being rendered within an if statement

Having trouble applying CSS to this EJS file, no matter what styling I use. Here's the code snippet: itemEdit.ejs <%- include("partials/header.ejs", {title: ` - Items`, body_id: `items`} )%> <%- include("partials/navbar.ejs&qu ...

The functionality of overflow:scroll is not functioning properly on Android smartphones

Hey there! I've been trying to implement scrolling in my application using overflow:scroll, but it seems that it's not working on Android mobile phones. After some research, I discovered that Android version 3.0 and below does not support overflo ...

Adjusting background-position-x while scrolling

I came across Ian Lunn's Parallax tutorial and found it really interesting. The tutorial can be accessed at . My goal is to enhance the effect by not only changing the y-position of the background but also adding horizontal movement based on scroll in ...

The functionality of Javascript ceases to operate once I fill out the table

UPDATE: I managed to solve the issue on my own and will mark my answer as accepted in 24 hours according to the system's rules. I've been experimenting with Bootstrap using this repository: https://github.com/BlackrockDigital/startbootstrap-sb-a ...

AngularJS and Bootstrap combined with a dropdown and input text feature

I'm looking to create a field that can function as both a drop-down menu and an input text field. Users should be able to either select a value from the drop-down or manually input text. I came across something similar, here is a code snippet for ref ...

How to attach onclick events to newly created div elements using JavaScript

Having some trouble adding click events to multiple dynamically created div elements. After creating divs with ids a0 through an, I want to assign them click events using a for loop. The problem is that when the click event occurs, I can't identify w ...

What is the best approach to utilizing BeautifulSoup to locate all the links on a page containing the string "/pl/oferta/"?

Currently, I am delving into Python with a focus on web scraping. Specifically, I am utilizing BeautifulSoup to extract only the links from: while searching for the string "/pl/oferta/'". import logging import requests from bs4 import BeautifulSoup ...

Implementing PHP echo alerts using Javascript

My current task involves validating the IP address entered in a textbox using PHP. $('#check_ip').click(function() { var iptext = $('#Ip_Txt').val(); $.ajax({ type : "POST", url : "mypage.php", data : { iptext : ip ...

Tips on showcasing the elements within a div container using flexbox

Seeking advice on positioning items/cards using flexbox in my initial react app. The issue lies with the div within my card component where applying display: flex; turns every card into a block (column-like) structure, flexing only the content within each ...

The div element is not rendering the variable

I'm currently following a guide from Microsoft on creating metro apps using JavaScript. Here's the link. According to the tutorial, when you click the button, it should display "Hello nameentered!" But for some reason, this isn't happening i ...

Unable to set the height of WebDataRocks as a percentage

I've recently started using WebDataRocks and I'm having trouble getting the height to fill the window. Despite what the documentation says (), which states, "The height of the pivot table in pixels or percent (500 by default)". The code snippet ...

Creating an eye-catching loading animation for page content using jQuery and CSS

Many websites incorporate animations to enhance user experience when content loads in the background. LinkedIn is one example of a site that features a nice animation during content loading. Below is a screenshot for reference, showcasing the type of anima ...

Maintaining Image Aspect Ratio with CSS Max-width Property

I'm currently facing an issue where I am using max-width to resize images, but they are not scaling proportionally. Is there a way to achieve this with JavaScript/jQuery? Additionally, is it possible to do this without initially knowing the image&apos ...