0

I want to use a transition + transform when someone hovers in my banner. Here the example:

#banner:hover {
transition: all 300ms;
transform: scale(1.01, 1.01); }

But when I quit the mouse over the banner, it suddenly transforms again to scale(1,1) but with no transition. I've tried with:not(hover) but what happens is that when I refresh the page, the banner makes a transition. Idk how to fix this. All I want is a transition when I hover on it and a transition when I put my mouse away from the banner, anything else.


I see some doubts so I'm going to clarify them. I ONLY want a transition when I hover the banner and when I quit the mover hover, a transition returning to his first scale. I have this code in #banner and in #banner:hover :

#banner {
transform: scale(1, 1); }

#banner:hover {
transition: all 300ms;
transform: scale(1.01, 1.01);}

The problem here is, if I put the transition: all 300ms; on my #banner, when I refresh the website it makes a transition.

Solved: adding transition: 300ms; in the #banner and not #banner:hover.

Rikku ESP
  • 13
  • 5
  • Hi! add `transition: all 300ms` to `#banner` only. Remove it from hover so that it will animate on both. – Debsmita Paul Jul 31 '20 at 14:19
  • Look, if I use the transition: all 300 ms in my #banner, the same thing happens. When I update the website, the banner makes a transition to putting itself on your site. – Rikku ESP Jul 31 '20 at 14:26
  • 1
    You need to set the scale to (1,1) by default: `#banner { transform: scale(1,1); transition: all 300ms;"}` – disinfor Jul 31 '20 at 14:27
  • So your question is not "Which command is the opposite of hover in CSS3?" but rather something like "how can I have a transition into the :hover state, but not back out"? – Heretic Monkey Jul 31 '20 at 14:35
  • 1
    Also, please include a [mre] with code showing the behavior. You can use [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) (icon looks like `<>` in the editor toolbar) to provide a runnable example here on Stack Overflow. – Heretic Monkey Jul 31 '20 at 14:37
  • Just the opposite. Imagine that: I have a box with the background red with a transition: all 300ms;. When I hover it I want to change it to blue, and when I quit the mouse over that box, I want to change it background color to yellow in the 300ms that it lasts to return to the red background. – Rikku ESP Jul 31 '20 at 14:37
  • 2
    Okay... But that's not at all what your question asks. There's no third state mentioned. Please [edit] your question to reflect exactly what you want, step by step. – Heretic Monkey Jul 31 '20 at 14:40
  • hope is better now :D – Rikku ESP Jul 31 '20 at 14:46

2 Answers2

1

To retain the animation both ways, use transition on the initial state, not on the :hover one:

#banner {
  background: red;
  transform: scale(1);
  transition: all 300ms;    /* HERE! */
}

#banner:hover {
  transform: scale(1.2);
}
<div id="banner">HOVER ME</div>
Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278
0

You should do something like this:

#banner {
  transform: scale(1, 1);
  transition: all 300ms;
}

#banner:hover {
  transform: scale(1.01, 1.01);
}
Emanuele Scarabattoli
  • 2,273
  • 1
  • 5
  • 13