Not perfect, but here is my solution:
this is html:
<div class="base"> <div class="container"><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child"></div><div class="child2"></div><div class="child"></div>
Since you are using an inline block, and I want your divs to fall apart nicely, I don't need to have spaces between them.
And CSS:
.container, .base { width: 500px; height: 400px; } .base { border: solid 1px black; } .container { -webkit-transition: 1s all linear; } .child, .child2 { width: 120px; height: 80px; display: inline-block; margin: 10px; } .child { -webkit-transition-duration: 1s; -webkit-transition-property: height, width, margin; -webkit-transition-timing-function: linear; background-color: lightblue; } .child2 { -webkit-transition: 1s all linear; background-color: lightgreen; } .base:hover .container { width: 20px; } .base:hover .child { width: 5px; height: 0px; margin: 0px; background-color: silver; } .base:hover .child2 { margin-right: -120px; }
The idea is to have an extra div between your container and children, the size of which changes at about the same rate as children. Thus, we keep the same layout for the entire transition, in this case 3 divs per line, and we avoid all the elements going on the same line and reset the top of the visible div.
There is a problem in this approach when the container is smaller than a div that is not shrinking; to avoid this, we set the margin-right of the visible element to a negative value, which will allow it to fit well into the container.
I have a problem in the final state, where there is a space between the lines (and the element does not fall into the upper position). This problem does not occur when I have the same code outside the violin; I do not know what causes this.
source share