Achieving a smooth transition effect when hovering over an element using only CSS

I am currently facing a challenge in trying to override a running transition using pure CSS. The CSS code I have attempted to overwrite the transition with does not seem to work as expected, and I am unable to comprehend the behavior.

Below, you will find an example code featuring 3 links, each followed by a div. Additionally, there are 3 more div elements added for testing purposes.

The issue arises when a new hover occurs, where I desire all hover effects on subsequent boxes to cease while only the current one remains open. It's akin to a menu scenario; upon hovering over a new menu point, I want all other sub-menus to close except for the current one that is opened.

  • My initial hope was to utilize the ~ selector to revert all following div elements back to their normal width. Subsequently, the a:hover + div would expand the width of the appropriate box. However, this approach did not yield the desired outcome.

Upon further investigation, it became evident that the issue lies within the transitions themselves. By removing all transition lines from the code, the result aligns perfectly with expectations but lacks the delays originally intended. For a visual representation, please refer to the following:

It was discovered that without the transition code, all successive div elements turn red, including the additional ones at the bottom. Conversely, with the transitions in place, only the hovered element turns red, indicating that the ~ selector does not function as intended due to the interfering nature of the transition properties.

Through this exploration, it became apparent that the transitions were causing interference. Deactivating a running transition seemed unattainable, leading to unexpected behavior from the conflicting code snippet rather than its outright ignorance.

This raises the question: What specific aspect of the transition code lines contributes to this peculiar behavior?


To provide insight, the original width before hovering in my actual project is set to 0. Therefore, a successful workaround involves:

a:hover ~ div {
  display:none;
}

Instead of resetting the width property, removing it entirely resolves the issue. The inquiry posed earlier delves into understanding the underlying cause behind the complications faced.

Answer №1

Just one more step! Remember to include transition: none in your CSS for a:hover ~ div

Check out this helpful link: https://jsfiddle.net/e3p97ewj/

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

Is there a way to retrieve the background URL from CSS using Selenium Webdriver?

I am attempting to verify the URL of an image within a div element's background property in the CSS for the webpage. However, when I retrieve the CssValue and display it, it appears to be blank. The HTML structure for the div is as follows: <div ...

Creating a new project in ASP Net Core Angular using Bootstrap 4 for the SideMenu layout instead of Bootstrap 3

I am currently working on developing a website using Angular6, where I aim to have a vertical navbar for large screens and a top navbar with dropdown functionality for mobile devices. While searching online, I came across several examples that seemed overl ...

Achieving a smooth sliding motion with the help of jQuery animation

Here is the code snippet: In HTML: <div> <div id="div1">12345</div> <div id="div2">67890</div> <div id="div3"><a href="#" id="slide">abcde</a></div> <div id="pad" style="display:non ...

Why won't the div move when I click it?

Could you please explain why my JavaScript code isn't functioning as expected? The intended behavior is for the 'mark' div to move to the current mouse coordinates upon clicking within the map. ...

JQuery hover effect for dynamically added elements

Currently, I am working on a webpage that will trigger an ajax call upon loading. The response data in JSON format will be processed and the elements will then be added to the DOM as shown below: $.ajax({ type: 'POST', url: "http://mysite.de ...

Tips for customizing the blinking cursor in a textarea

I am experimenting with creating a unique effect on my website. I have incorporated a textarea with transparent text overlaying a pre element that displays the typed text dynamically using JavaScript. This creates an illusion of the user typing in real-tim ...

Display the remainder of an image's height within a div container

Here is the code snippet I am working with: HTML: <div id="mapWrapper" style="height: 624px;"> <div class="box" id="map"> <h1>Campus</h1> <p>Description Campus Map.</p> <img src="abc.png" ...

Can you tell me the standard CSS easing curves?

When working with css, we have the option to incorporate easing for transitions, like this: transition: all .5s ease-in-out CSS provides predefined easings such as ease, ease-in, ease-out, and ease-in-out. We can also create a customized easing using a ...

Building a website using Bootstrap: A step-by-step guide

Wondering how I can incorporate all the cool Bootstrap components into a webpage. Is there a tool that offers live preview? Back in the day (web 1.0), Dreamweaver was my go-to for creating HTML and basic CSS, but what are some current tools where I can si ...

Image Filter: Only Display Selected Images, Not All

I have designed a Filter Image Gallery where all images are initially displayed when the page loads. There are four different tabs available - Show all, Nature, Cars, and People. However, I want to change this default behavior so that only Nature filter i ...

Creating a personalized image resizing function for WordPress

I have implemented the following code in functions.php to fetch the first image from WordPress posts: function grab_first_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/< ...

The sequence of CSS and deferred JavaScript execution in web development

Consider this scenario: you have a webpage with a common structure in the <head>: <link rel="stylesheet" href="styles.css"> // large CSS bundle <script defer src="main.js"></script> // small JS bundle with defer attribute There is ...

Unable to see Primereact styles reflected on components

After some investigation, I've pinpointed the issue to be related to preflight from tailwind. Upon reviewing the documentation, I came across this helpful information: CSS Layer It seems that using Tailwind CSS with styled or unstyled modes of PrimeR ...

Is there a way to determine the negative horizontal shift of text within an HTML input element when it exceeds the horizontal boundaries?

On my website, I have a standard HTML input field for text input. To track the horizontal position of a specific word continuously, I have implemented a method using an invisible <span> element to display the content of the input field and then utili ...

Is it possible to apply a personalized scrollbar design to the Autocomplete Listbox component in Mui?

I am struggling to implement a custom scroll style in Autocomplete Listbox. https://i.sstatic.net/MifSm.png After multiple attempts and thorough research, I have been unsuccessful in achieving the desired result. Here is my most recent code: <Aut ...

What is the proper way to set a margin on a fixed div that has a width

I am looking to create a fixed header that allows the content (body) to scroll underneath it. The challenge arises when the parent element has some margin-right, causing the fixed header to extend beyond the parent's width and occupy 100% of the windo ...

Different style sheets & Susy: Tailored designs for varying orientations

I have the task of designing two layouts - one for Landscape and one for Portrait. The Landscape layout needs wider columns. I am utilizing the latest version of Susy to accomplish this. Below is the code snippet I have written: /* iPads (landscape) ---- ...

Issues with Animations in Animate.css

Seeking to achieve fluid transitions using Vue.js and Animate.css, encountering issues with certain animations not functioning as expected! Successfully implementing animations such as: bounceIn, bounceOut, and hinge. Experimenting with this on codepen.i ...

Why isn't the main body of CSS extending across the entire page?

I feel like I'm running headfirst into a wall trying to figure out why the code I wrote for this specific website isn't quite working properly. The main content area of my pages (the white space in the link below) is supposed to extend from the ...

Is there a way to display the full text of a lengthy select option?

I'm facing a challenge with long <option>s in my <select> box - I don't want the box to be too wide, but when I style it smaller, the full text gets cut off. One idea is to hover a duplicate of the text exactly over the option the mou ...