I have a div
with position: relative
, on hover I want to move the div
, achieving this with position: absolute
with animation, and then I want to go back to its original position with an animation again. I managed to animate when the mouse enters the div
, but not when it exits. Can you please help?
Code:
.team-member {
&.hovered {
.name-container {
top: 30%;
position: absolute;
transition: top 0.7s ease-in-out;
}
}
.name-container {
transition: top 0.7s ease-in-out;
transition: position 0.7s ease-in-out;
top: 90%; // Just so the animation can be seen
position: relative;
}
}
3
Answers
To achieve the effect where a div animates both when the mouse enters and exits, you need to ensure that the CSS transitions are correctly applied to the element in both states (default and hovered). The main issue in your current code is likely due to the attempt to transition the position property, which is not animatable in CSS. Instead, you should focus on animating properties that can interpolate values over time, like top, transform, etc.
Given your description, it seems you want the .name-container to move when .team-member is hovered and then move back when the hover is removed. Here’s how you can adjust your CSS for the desired effect:
In this setup, .name-container will move to top: 30% when .team-member is hovered and will smoothly transition back to top: 90% when the hover state is removed. The transition is applied to the top property of .name-container in both states, ensuring that the movement is animated both ways.
Remember, CSS transitions work by interpolating values between states, and thus the property you’re transitioning needs to be present in both states. The position property, while crucial for defining how elements are laid out, doesn’t interpolate between its keyword values (like absolute, relative, etc.), so the animation should focus on properties that define the element’s position in a more granular way, like top, left, right, bottom, or the transform property for more complex movements (e.g., transform: translateY(-50%);).
To achieve the desired effect of animating the movement of the div both on hover and when the mouse leaves, you need to apply the transition properties to both the base state and the hovered state. Here’s how you can modify your CSS:
With this modification, the transition properties are applied to .name-container in its base state as well as in its hovered state. This should produce the desired animation effect both when hovering over the div and when the mouse leaves.`
There are a couple of things that are confusing in your questions, more specifically:
.hovered
class? It seems you are applying a class in some ways (maybe via javascript?), but you don’t really need it, because you can rely on the:hover
pseudo-class.That said, perhaps you want to use the
:not(:active)
pseudo-class to control the animation when the element goes back in position, the following example should help you in achieving what you are looking for: