Create a captivating dropdown hover menu with animated CSS

My latest project involves creating a sleek CSS dropdown button/menu that I am now attempting to add animation to.

Initially, the menu transitioned from display: none to display:block on hover, but I encountered issues animating it. So, I decided to switch to opacity: 0, height: 0 transitioning to opacity: 1, height: auto, however, this led to some unexpected behavior with the menu. Let me demonstrate what I mean. Here is the original menu code:

HTML:

<div className="account-dropdown">
      <button className="dropbtn">
        <FormattedMessage
          id="header.account"
          defaultMessage={`Account`} />
      </button>
      <div className="dropdown-content">

        <a>Order History</a>
        <a>Settings</a>
        <a onClick={logOut}>Logout</a>

      </div>
    </div>

The SCSS:

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

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

      }

      .dropbtn {
        color: red;
      }
    }

    .dropbtn {
      border: none;
      cursor: pointer;
      margin: 2rem 1rem;
      transition: all 0.3s;
      color: black;
      background-color: #fff;
    }

    .dropdown-content {
      display: none;
      padding: 3rem 3rem 1rem 3rem;
      position: absolute;
      top: 5rem;
      left: -17.6rem;
      background-color: #fff;
      min-width: 250px;
      width: 100%;
      box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);
      border: solid 1px #e8e8e8;
      z-index: 1;
      color: rgba(0, 0, 0, 0);
      transition: all 0.3s;

      &:after, &:before {
        bottom: 100%;
        border: solid transparent;
        content: " ";
        height: 0;
        width: 0;
        position: absolute;
        pointer-events: none;
      }

      &:after {
        border-color: rgba(255, 255, 255, 0);
        border-bottom-color: #ffffff;
        border-width: 7px;
        left: 75%;
        margin-left: 26px;
      }
      &:before {
        border-color: rgba(113, 158, 206, 0);
        border-bottom-color: #e8e8e8;
        border-width: 8px;
        left: 75%;
        margin-left: 25px;
      }


      a {
        text-align: left;
        color: black;
        padding-bottom: .8rem;
        text-decoration: none;
        display: block;
        cursor: pointer;
        transition: all 0.3s;

        &:hover {
          color: black;
        }

        &:last-child {
          margin-top: .4rem;
          padding-top: .8rem;
          border-top: solid 1px #e8e8e8;
        }
      }
    }
  }

View the working version in this fiddle (excluding rems or SCSS vars) https://jsfiddle.net/tqf1r0mm/7/

Additionally, here's a fiddle showcasing the switch from display: none to opacity and height -> https://jsfiddle.net/tqf1r0mm/10/.

Upon observation, you'll notice that the animation is not smooth and the menu opens when hovering below the account button (instead of only opening when hovering over account). Any suggestions on resolving these issues for a polished animation outcome? Your insights would be greatly appreciated. Thank you!

Answer №1

It appears that the issue with the animation is caused by using the attribute transition: all.

To fix this problem, try using visibility:hidden; instead of height:0; and then use visibility:visible on hover.

Here is an example:

.test {
padding-left: 300px;

.account-dropdown {
position: relative;
display: inline-block;
&:hover {
  .dropdown-content {
    opacity: 1;
    visibility:visible;
  }
  .dropbtn {
    color: black;
  }
}
.dropbtn {
  border: none;
  cursor: pointer;
  margin: 20px 10px;
  transition: all 0.3s;
  color: black;
  background-color: #fff;
}
.dropdown-content {
  opacity: 0;
  visibility:hidden;
  padding: 3rem 3rem 1rem 3rem;
  position: absolute;
  top: 50px;
  left: -176px;
  background-color: #fff;
  min-width: 250px;
  width: 100%;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.2);
  border: solid 1px #e8e8e8;
  z-index: 1;
  color: rgba(0, 0, 0, 0);
  transition: all 0.3s;
  &:after,
  &:before {
    bottom: 100%;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    pointer-events: none;
  }
  &:after {
    border-color: rgba(255, 255, 255, 0);
    border-bottom-color: #ffffff;
    border-width: 7px;
    left: 75%;
    margin-left: 26px;
  }
  &:before {
    border-color: rgba(113, 158, 206, 0);
    border-bottom-color: #e8e8e8;
    border-width: 8px;
    left: 75%;
    margin-left: 25px;
  }
  a {
    text-align: left;
    color: black;
    padding-bottom: 8px;
    text-decoration: none;
    display: block;
    cursor: pointer;
    transition: all 0.3s;
    &:hover {
      color: black;
    }
    &:last-child {
      margin-top: 4px;
      padding-top: 8px;
      border-top: solid 1px #e8e8e8;
    }
  }
}
}
}

Answer №2

Consider using visibility in place of height, along with opacity to allow for smoother animations. It's important to apply the trigger on .dropbtn instead of .account-dropdown to prevent unintended activation of hover state outside the button.

Since I am not familiar with SCSS, I utilized CodePen to convert the code into regular CSS. Below is the CSS code snippet used:

.test {
  padding-left: 300px;
}

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

.test .account-dropdown .dropbtn:hover~.dropdown-content {
  visibility: visible;
  opacity: 1;
}

------ (CSS code continues) ------

<div class="test">
  <div class="account-dropdown">
    <button class="dropbtn">
      Account
    </button>
    <div class="dropdown-content">

      <a>Order History</a>
      <a>Settings</a>
      <a>Logout</a>

    </div>
  </div>
</div>

An additional hover class has been included for better user experience when interacting with .dropdown-content so that it remains visible when trying to click on any links within.

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

Troubleshooting a problem with CSS styling in a jQuery bounce effect

In my current project, I am experimenting with creating a unique 'hovering' link. When the user hovers over an image and then moves the mouse away, I want the link to 'jump' back instead of using jQuery animation. +-----------+ | I ...

A plethora of color choices in a Multi-select Box

Hi there! I am facing an issue with dynamically adding multiple select boxes. Currently, I have 4 color-coded options under the selection boxes, which work fine when there is only one box. However, when I add more than one select box, the color coding doe ...

Navigation bar transforms color once a specific scroll position is reached

My website features a sleek navbar fixed to the top of the window. As users scroll past a specific div, the background color of the navbar changes seamlessly. This functionality has been working flawlessly for me thus far. However, upon adding anchor link ...

Are there any alternative approaches to handling frequent database updates in web development aside from using Websockets/AJAX for coding?

Currently, I'm in the process of developing an HTML and Javascript game and looking to implement a feature that displays the player's gold balance on the screen. The goal is to have this balance decrement by 1 every time the player clicks on a sp ...

Find the element that contains a specific substring in its value using JavaScript

I am trying to find a way to count how many input fields with the class name "text" contain a specific value. document.getElementById('c_files').getElementsByClassName('text').length; Currently, this code counts all textboxes with the ...

Tips on updating CSS styles for navigation bar links in real time

Currently, my homepage displays a CSS content hover effect when the user interacts with the icons. However, I am facing an issue where all icons show the same text "home" on hover. How can I customize the text output for each individual icon/link? Your hel ...

Preventing the dropdown options from appearing in Angular when clearing the input field

In a reusable component, I implemented some simple logic to clear the value of a select input when the 'x' button is clicked. select.component.ts @Input() selectFormControlName: string; @Input() selectAbstractControl: AbstractControl; ... ...

Tips for ensuring HTML5 <video> compatibility with CSP in Google Chrome

When I implement an HTML5 video element on my website with a strict Content-Security-Policy directive (default-src 'self'), I encounter an error in the Google Chrome console upon loading a page for the first time. The error message states: [Repo ...

Tips for ensuring a button stays anchored to the bottom of a flatlist while scrolling to the end in React Native

const Layout = () => { return ( <View style={styles.container}> <Text style={styles.heading}>Screen Data</Text> <View> <FlatList data={data} renderItem={({item}) => ( ...

Looking to target an element using a cssSelector. What is the best way to achieve this?

Below are the CSS Selector codes I am using: driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='append_numbers']")).click(); driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg- ...

Tips for utilizing the grid system in Bootstrap 4 to transform a dropdown menu into the desired shape shown in the image

I am struggling to create a dropdown menu similar to the one highlighted with a red border in the image, The screenshot I referenced is from the bootswatch website (a bootstrap theme). Although I attempted to use bootstrap flex utility, I was unable to a ...

"Enhance input functionality by updating the value of the text input or resizing the textbox

I've been facing a challenge with updating the value of my input type=text or textbox control text value using jQuery $(window).resize(function(){});. I am aware that the event is triggered because an alert pops up when I resize the browser. Additiona ...

Showcasing pictures with a prominent large image accompanied by two smaller ones on the right side

In my use of Material-ui-next, I am attempting to create images in a specific layout. ------ --------- | | | | 2 | | | | 1 |---| | | | | 3 | ------ --------- I have encountered some unusual issues. 1 - 3 appears below 1 ...

How can you make the browser window scroll horizontally when a div is clicked using jQuery?

I have an image of an arrow inside a div. The div is positioned fixed in the bottom right corner of an extremely wide page. Is there a way to utilize jQuery to scroll the window 600px to the right each time the arrow is clicked? Also, can I detect when th ...

Data bindings encapsulated within nested curly braces

I am currently utilizing Angular2 in my application. Within my html file, I have a *ngFor loop set up like so: <div *ngFor="let element of array"> {{element.id}} </div> Next, I have an array containing objects structured as follows: some ...

What is the best way to shift a list to the right within my navigation bar using ReactJS and CSS?

I'm looking to align a list to the right in my navbar, as shown in the image below Image Here's my navbar.js code: import React from "react"; import "./Navbar.css"; const Navbar = ({ isScrolling, information }) => { ...

Disordered arrangement of PHP and MySQL data being shown

When dynamically generating a page with various types of content, I am experiencing an issue where the "post" content is displaying above the static content that is being generated. I would like it to appear in the opposite order. Do you see anything in ...

What is the best way to evenly divide divs vertically on a Bootstrap website?

I am currently working on a responsive page design and have come across this useful resource: http://www.bootply.com/2sfiCehqac My main objective is to ensure that when the screen size is medium or large: 1) div_left and div_right (orange and blue) adjus ...

Is there a way to replicate this exact toggle selection functionality from Angular Material using just HTML and CSS?

I'm attempting to incorporate this functionality into my Angular project ( link ), but I'm facing challenges with styling it using CSS. Could this be due to limitations in overriding the default settings? I came across this helpful resource: sour ...

Issue with Dropup Menu not displaying on Safari 6

Experience a unique display when visiting the following URL in popular browsers such as FF, Chrome, and Opera- watch as the menu gracefully slides out while a red dropup menu appears at the bottom: A peculiar situation unfolds when viewing this page on Sa ...