Javascript Coin Flip Animation | Coin Flip Game Html Css Javascript

Javascript Coin Flip Animation | Coin Flip Game Html Css Javascript

Welcome🎉 to Code With Random blog. In this blog, we learn how we create a Javascript Coin Flip Animation. We use HTML, Css, and javascript for this Javascript Coin Flip Animation. I hope you enjoy our blog so let’s start with a basic HTML structure for the Javascript Coin Flip Animation.


Table of Contents

 <div class='container'>  
<img id="featureCoin" src=""/>a
<div id='coin'>
<div id='button'>
Flip coin
<div id='result'>
<div id='headsCounter'>
<div id='tailsCounter'>
</div </div

There is all the HTML code for the Javascript Coin Flip Animation. Now, you can see an output with Javascript Coin Flip Animation then we write javascript for the Javascript Coin Flip Animation.


CSS(SCSS) code

 body {  
font-family: 'Ubuntu', sans-serif;
#coin {
#button {
padding:10px 20px;
font-size: 23px;
margin-bottom: 30px;
box-shadow: 1px 1px 8px #DCDCDC;
&:hover {
.animate-coin {
animation: flip 1.4s 1;
@keyframes flip {
0% {
transform: scale3d(1,1,1) rotateX(0deg);
50% {
transform: scale3d(2,2,2) rotateX(3600deg);
100% {
transform: scale3d(1,1,1) rotateX(7200deg);
/* Centralise content */
.container {
margin:200px auto;
img {
#tailsCounter span, #headsCounter span {
#result {

Css Updated output

Javascript code

 var coin = document.getElementById('coin');  
var button = document.getElementById('button');
var result = document.getElementById('result');
var headsCounter = document.getElementById('headsCounter');
var TailsCounter = document.getElementById('TailsCounter');
var featureCoin = document.getElementById('featureCoin');
var heads = 0;
var tails = 0;
/* On click of button spin coin ainamtion */
function coinToss() {
/* Random number 0 or 1 */
var x = Math.floor(Math.random() * 2);
/* If x = 0 change coin html to image of heads along with animation for flipping effect */
if (x === 0) {
coin.innerHTML = '<img class="heads animate-coin" src=""/>';
/* Heads count increase by 1 */
heads += 1;
/* Display result of flip */
result.innerHTML = '<h2>You got heads</h2>';
/* Display number of heads */
headsCounter.innerHTML = '<h3> Number of heads:<span> ' + heads + '</span></h3>';
/* Else x = change coin html to image of tails along with animation for flipping effect */
} else {
coin.innerHTML = '<img class="tails animate-coin" src=""/>';
/* Tails count increase by 1 */
tails += 1;
/* Display result of flip */
result.innerHTML = '<h2>You got tails</h2>';
/* Display number of tails */
tailsCounter.innerHTML = '<h3> Number of tails:<span> ' + tails + '</span></h3>';
/* Add the coin toss function to the button using on click */
button.onclick = function() {

Final output

Now that we have completed our javascript section,  Here is our updated output with javascriptHope you like the Javascript Coin Flip Animation. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development. Thank you 🙏💕!

In this post, we learn how to create a Javascript Coin Flip Animation using simple HTML & CSS, and javascript. If we made a mistake or any confusion, please drop a comment to reply or help you in easy learning. 

Written by – Code With Random/Anki 

Code by – Harry Beckwith

Check out more…..

Leave a Reply