These answers all use JS, and even though this post is old it still pops up in search results, so I would like to provide an update with a pure CSS solution. You can now use the @view-transition rule to do this, without need for JavaScript.