I'm trying to achieve a little effect that I thought would be an easy one: make a child div expand in width when its parent is hovered, and having the parent's width transitionning alongside.
The emphasis emphasises what my problem is ; with what I've achieved so far, when hovering the parent, it expand instantly to the final total width, as if it knew beforehand the final width of its child after the transition, while the child does indeed transition to this final width.
Here's a sample that illustrates the problem:
.profile {
cursor: pointer;
background-color: red;
border-radius: 100px;
padding-right: 10px;
display: inline-flex;
align-items: center;
}
.profile:hover .name {
width: 100%;
}
.profile .picture {
margin-right: 3px;
display: inline-block;
padding: 3px;
width: 27px;
height: 27px;
}
.profile .picture img {
border-radius: 50px;
width: 27px;
height: 27px;
}
.profile .name {
transition-property: width;
transition-duration: 4s;
width: 0;
display: inline-block;
overflow: hidden;
white-space: nowrap;
}
<div class="profile">
<div class="picture">
<img src="https://s.gravatar.com/avatar/090a196b0a7db8a888d3b1af34e55cdc?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fja.png" alt="Avatar" />
</div>
<div class="name">John Doe</div>
</div>
I don't understand why the parent does not follow the child width. Is this related to the fact that the parent is a flex
box? Am I missing some property on the parent? I've tried adding a transition: all 4s
on the parent too, without success.
Would I be better of using the translate
function somehow?
Any help appreciated!