Having trouble getting a CSS transition to work when triggered by hovering over a different element

I'm currently working on designing a menu for a website project. I have a beautiful graphic set up for the menu button in the top left corner, which functions perfectly when hovering over the bars themselves. However, I've encountered an issue with the transition not running smoothly when the mouse is hovering over the menu as opposed to the bars. Can anyone provide insights into why this might be happening?

Here's the CSS code:

      .bars:hover .line-top { transform: translateY(9px) rotateZ(45deg); }
      .bars:hover .line-middle { transform: rotateZ(135deg); }
      .bars:hover .line-bottom { transform: translateY(-9px) rotateZ(135deg); }

      .menu-holder:hover ~ .bars .line-top { transform: translateY(9px) rotateZ(45deg); }
      .menu-holder:hover ~ .bars .line-middle { transform: rotateZ(135deg); }
      .menu-holder:hover ~ .bars .line-bottom { transform: translateY(-9px) rotateZ(135deg); }

      .menu-holder {
        width: 200px;
        position: absolute;
        top: 0;
        left: -150px;
        height: 100vh;
        background: red;
        transition: transform 1s;
      }

      .bars:hover ~ .menu-holder{
        transform: translateX(150px);
      }
      .menu-holder:hover {
        transform: translateX(150px);
      }

      .line-top, .line-middle, .line-bottom {
        width: 30px;
        height: 8px;
        border-radius: 28px;
        background: black;
        margin: 1px;
        transition: transform 1s;
      }

      .bars {
        width: 30px;
        z-index: 2;
        position: absolute;
      }

And here's the HTML markup:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
  </head>
  <body>
    
    <div class="bars">
      <div class="line-top"></div>
      <div class="line-middle"></div>
      <div class="line-bottom"></div>
    </div>

    <div class="menu-holder slider">
    </div>

    <!-- <script src="script.js"></script> -->
  </body>
</html>

P.S. I'm new to CSS and open to any suggestions or advice.

Answer №1

Remember, CSS selectors follow a one-way path. For instance, using .x ~ .y signifies that all .y elements appearing after .x elements will inherit the specified properties. Therefore, if there are no elements with the class bars following your menu-holder element, then your .menu-holder:hover ~ .bars rule will have no impact.

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

What are some effective strategies for avoiding empty fields showing on an email form?

My HTML form is linked to a PHP Email handler, and it's working well except for one issue. When the email is delivered, it includes all fields, even the empty ones. I want it to only display filled-in fields. I tried using an IF statement that checks ...

Utilizing conditional statements and time to dynamically alter the class of an object

I need to change the class of an object based on the time, with two classes available: recommendedspot and notrecommended. The code I've written so far is not displaying the image correctly. Can someone help me troubleshoot this issue? Here is my cur ...

Steps to correct color gradient on a fixed top navigation bar:

In my Bootstrap 4 project, I have a fixed navbar with a linear gradient background image. The navbar is transparent and fixed, but as I scroll down the page, the gradient disappears. Can anyone help me figure out how to keep the background image visible wh ...

Guide on creating a stationary side navigation bar in HTML with Bootstrap 5 and CSS

I am developing a website with a fixed side navigation bar that stays in place when scrolling through the content section. I experimented with both the fix-position in Bootstrap 5 and CSS methods, but no matter how I set it up, the side bar remains transpa ...

Customizing nvd3 chart colors with CSS: A step-by-step guide

Currently, I am faced with a challenge in SugarCRM as I try to modify the colors of nvd3 charts using CSS. The pie chart colors are structured differently compared to other nvd3 charts, making it difficult for me to figure out how to customize them through ...

Prolong the ultimate position of the initial animation for the translated object

I understand that this question has been asked multiple times before, but I am seeking a fresh approach to these animations. Issue: The second box contains multiple animations, and I am trying to achieve the same effect as the first one. However, it seem ...

Can the floating side of a div be switched after resizing?

Let's say I have two divs set up in the following configuration: |------------------------------| | div1 ------------------ div2 | |------------------------------| .div1{ float:left; } .div2{ float:right; } From my understanding, they w ...

The Jquery code for Fullpage.js is causing unexpected behavior on the HTML page

While following a tutorial at the given link, I encountered an issue. Around the 9:08 mark, the instructor adds some JavaScript and demonstrates that fullpage.js is working. However, when I refresh the page after implementing the new code, it doesn't ...

Click Function for Modifying the Content of a Popup Window

I'm a beginner in JavaScript and I need help figuring out how to achieve the following task. Essentially, my goal is to have lines of code like this: <a onclick="JavascriptFunction(0000000)"><img src="image1.png"></a> The number w ...

What is the best way to retrieve the object of the selected option from a single select input in Angular

I'm currently working with an array of objects that looks like this: let myArray = [{'id':1,'Name':"ABC"},{'id':2,'Name':"XYZ"}] I'm trying to retrieve object values based on a dropdown selection, but so ...

How can I extract the value of a JavaScript variable using jsoup in an HTML document?

<html> <script type="text/javascript"> var map = null; jQuery(function($) { L.marker([50.065407, 19.945104], {title: 'Cat'}) .bindPopup('<h3>Cat</h3> ...

Is it possible to save an entire webpage that infinitely scrolls without actually manually scrolling through it?

I'm dealing with a webpage that has infinite downward scrolling. I tried automating the scrolling, but eventually the page became too large to continue scrolling. To fix this, I manually removed some DIV blocks from the source code which decreased the ...

Mastering the use of Quasar variables in your scss styles is crucial for optimizing your

I recently set up a project using Vue3 and Quasar by running vue add quasar. However, I am struggling to figure out how to utilize Quasar sass/scss variables. According to the documentation, I should use the following syntax: <style lang="scss&quo ...

The visibility classes d-none and d-sm-block are not functioning properly on mobile devices or when toggling

I have been attempting to hide a card on mobile devices using the class "d-none d-sm-block". However, while this method works when I resize the page, it does not work on actual mobile devices or when using the toggle device toolbar in Chrome. S ...

Adjusting nearby parts when hovering in CSS

I'm curious as to why the size of the second div does not change when hovering over the third div, unlike when hovering over the first div. #block { width: 50%; white-space: nowrap; } .div { display: inline-block; text-align: center; } di ...

Adjust the transparency of a `<ul>` element as it descends on the webpage - CSS3

I have a list (<ul>) that I would like to fade out as it scrolls down the page. I attempted to use linear-gradient, but I was unsuccessful. Is this even possible? You can view an example of what I am trying to achieve on JSFiddle here: http://jsfidd ...

Navigating within fixed-positioned divs

I'm attempting to create something similar to the layout seen on: The desired effect is to have fixed content on the right side, with only the left side scrolling up to a certain point before the entire page scrolls normally. However, in my implement ...

How to properly adjust image size in a flex-direction column layout - tips and solutions

My mission is to place an <img> with text blocks above and below it inside a container of specified height. The exact heights of the text blocks and image are unknown. After extensive searching, my colleague provided a useful solution by using the ...

Next.js Image staying at the forefront

Currently, I am utilizing Tailwind CSS to style my Next.js website and incorporating the Next.js Image element for an image on the page. My goal is to display a modal over the existing page without completely blacking out the background; instead, I want i ...

How can I maintain consistent spacing between two areas in CSS3 across all screen sizes?

My question pertains to the alignment of an advertisement (on the left as a banner) and content sections on my website. I have noticed that as the screen size increases, the distance between these two areas also increases. How can I ensure that the distanc ...