Flipping a div (part 2)<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Flipping a div (part 2)

Posted on: July 15, 2013

In a previous post, we have discussed about how to flip a division. However, Javascript was involved with CSS3 while only CSS3 could have been enough. For the curious, here is the code/JsFiddle (most of the code is not from me, but I cannot find the source).

First of all, the Html structure changes a little with two divisions with the same class ("side") within 2 divisions. One is the card container and the second is card. The card will be the one animated while the card container act as a placeholder.

1<div class="card-container">
2 <div class="card">
3 <div class="side">
4 <img src="thisisanimage.png"/>
5 </div>
6 <div class="side back">This is some text</div>
7 </div>

As for the CSS, the perspective is also set to have a 3d look of depth. This is what the card-container is for.

After, the card is assigned to have a transition of 1 second. Nothing is trigged until the card has a transformation applied, which is the case with the next CSS statement.

1.card-container {
2 -webkit-perspective: 600;
3 margin-bottom: 20px;
4 position: relative;
5 width: 150px;
8.card {
9 position: absolute;
10 -webkit-transform-style: preserve-3d;
11 -webkit-transition: all 1s ease-in-out; width: 100%;
14.card:hover {
15 -webkit-transform: rotateY(180deg);
16 }
18.card .side {
19 -webkit-backface-visibility: hidden;
20 position: absolute; width: 100%;
23.card .back {
24 color: #404853;
25 font-size: 14px;
26 font-weight: bold;
27 line-height: 150px;
28 text-align: center;
29 -webkit-transform: rotateY(180deg);
30 background-color:yellow;

The :hover statement raise the transition to be executed (with the 1 second time). As you may have notice, the use of backface-visibility lets the rendering to not display the back of the div but instead display the other division which rotate at the same time. This create the illusion that we are seeing the back of the division, but in fact, we are showing the other division. This is possible because of the position set to absolute. This put both division over each others.