๐ ๏ธ Section project: a card that flips
Section 5: Pro CSS: shadows and movementLesson 5.5 ยท โฑ 12 min
๐ฏ Project goal
A card with โ on it, and when the mouse is over it, it flips and shows its back: ๐ฆ Lion.
perspectivegives a feeling of depth, as if the card is in space.- The two faces are on top of each other (
position: absolute), and the back is turned round from the start:rotateY(180deg). backface-visibility: hiddenhides the back of each face.- When the mouse is over it we turn the inside box 180 degrees.
HTML
<div class="card">
<div class="inner">
<div class="front">โ</div>
<div class="back">๐ฆ Lion</div>
</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; display: flex; justify-content: center; padding-top: 24px; }
.card { width: 160px; height: 200px; perspective: 600px; }
.inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover .inner { transform: rotateY(180deg); }
.front, .back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
border-radius: 16px;
backface-visibility: hidden;
}
.front { background: #2557c9; color: white; }
.back { background: #ffd23f; font-size: 30px; transform: rotateY(180deg); }
๐ฅ๏ธ The page in Robo Web:

๐ฅ๏ธ What happens on the stage?
In the picture the mouse is over the card, and it flipped and showed the lion.
- Make three cards side by side for three animals.
- Make it flip up and down:
rotateXinstead ofrotateY.
๐ Another example: a question card that flips when clicked
Instead of the mouse going over it: clicking adds a class called flipped, and the card flips.
HTML
<div class="card">
<div class="inner">
<div class="front">5 ร 7 ?</div>
<div class="back">35 โ
</div>
</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; display: flex; justify-content: center; padding-top: 24px; }
.card { width: 160px; height: 200px; perspective: 600px; }
.inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card.flipped .inner { transform: rotateY(180deg); }
.front, .back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
border-radius: 16px;
backface-visibility: hidden;
}
.front { background: #2557c9; color: white; }
.back { background: #ffd23f; font-size: 26px; transform: rotateY(180deg); }
JavaScript
const card = document.querySelector(".card");
card.onclick = function () {
card.classList.toggle("flipped");
};
๐ฅ๏ธ The page in Robo Web:

โ๏ธ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
โ๏ธ Practise here in the lessonโ Close the labor open the lab in a new page โ
๐ฏ Challenge yourself
Easy
Change the two faces: ๐ on the front and โThe Earthโ on the back.
๐ Show the solution๐ Hide the solution
HTML
<div class="card">
<div class="inner">
<div class="front">๐</div>
<div class="back">The Earth</div>
</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; display: flex; justify-content: center; padding-top: 24px; }
.card { width: 160px; height: 200px; perspective: 600px; }
.inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover .inner { transform: rotateY(180deg); }
.front, .back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
border-radius: 16px;
backface-visibility: hidden;
}
.front { background: #2557c9; color: white; }
.back { background: #ffd23f; font-size: 26px; transform: rotateY(180deg); }
๐ฅ๏ธ The page in Robo Web:

Result: it flips and shows โThe Earthโ.
Medium
The card flips from top to bottom (rotateX).
๐ Show the solution๐ Hide the solution
HTML
<div class="card">
<div class="inner">
<div class="front">โ</div>
<div class="back">๐ฌ Dolphin</div>
</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; display: flex; justify-content: center; padding-top: 24px; }
.card { width: 160px; height: 200px; perspective: 600px; }
.inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover .inner { transform: rotateX(180deg); }
.front, .back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
border-radius: 16px;
backface-visibility: hidden;
}
.front { background: #2557c9; color: white; }
.back { background: #ffd23f; font-size: 26px; transform: rotateX(180deg); }
๐ฅ๏ธ The page in Robo Web:

Result: it flips up and down.
Hard
Three question-and-answer cards in a row, each flips on its own.
๐ Show the solution๐ Hide the solution
HTML
<div class="row">
<div class="card">
<div class="inner">
<div class="front">2 + 2</div>
<div class="back">4</div>
</div>
</div>
<div class="card">
<div class="inner">
<div class="front">3 ร 3</div>
<div class="back">9</div>
</div>
</div>
<div class="card">
<div class="inner">
<div class="front">10 โ 4</div>
<div class="back">6</div>
</div>
</div>
</div>
CSS
body { font-family: Tahoma, sans-serif; padding-top: 24px; }
.row { display: flex; gap: 12px; justify-content: center; }
.card { width: 110px; height: 140px; perspective: 600px; }
.inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.card:hover .inner { transform: rotateY(180deg); }
.front, .back {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
border-radius: 16px;
backface-visibility: hidden;
}
.front { background: #2557c9; color: white; }
.back { background: #ffd23f; font-size: 26px; transform: rotateY(180deg); }
.front { font-size: 24px; }
๐ฅ๏ธ The page in Robo Web:

Result: only the middle card flips.

