1

When hovering over the word it comes out with a smooth animation but when we exit the hover state instead of reversing the animation it just cuts out. How could I make the animation smooth both when entering the hover state and exiting it?

Here is the example:

*{
  font-size: 50px;
}
.container a{
    position:relative;
    float:left;
    font-family: 'Montserrat', sans-serif;
    font-weight:bold;
    margin-left: 5%;
    padding-right: 10px;
}
.child{
    position:relative;
    float:left;
    display:none;
    padding-left: 10px;
    font-weight:bold;
}
.container:hover > .child{
    transition: .4s;
    color:#F0C930;
    border-left: 4px solid #F0C930;
    animation: effect .5s;
    display:block;
}
@keyframes effect{
from{
position:relative;
padding-left: 0px;
opacity:0;}
to{
position:relative;
padding-left: 10px;
opacity:1;
    }
}
}
<p>hover over "Word"</p>
<div class="container">
  <a>Word</a>
  <div class="child">Word</div>
 </div>

3

1 Answer 1

2

Use CSS transition to handle the opacity and padding. CSS Transition

.container {
  display: flex;
  font-size: 50px;
  cursor: pointer;
}

.container .child {
  transition: opacity .5s, padding .5s;
  border-left: 4px solid #F0C930;
  opacity: 0;
  margin-left: 20px;
  color: #F0C930;
}

.container:hover .child {
  opacity: 1;
  padding-left: 20px;
}
<div class="container">
  <a>Word</a>
  <div class="child">Word</div>
</div>

1
  • @Sakumi: Glad you got some direction. Please consider accepting the answer if this helped. Commented Sep 19, 2021 at 15:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.