You are currently viewing Flip A Coin Using HTML, CSS & JavaScript (Source Code)

Flip A Coin Using HTML, CSS & JavaScript (Source Code)

Free Coding Ebook 👉 Get Now

Flip A Coin Using HTML, CSS & JavaScript (Source Code)

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a flip-a-coin project which is a toss basically and act on the click of the user and it will randomly decide that the result is heads or tails. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Project.

The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Flip A Coin.

Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Flip A Coin project.

ADVERTISEMENT

At last we will use JS (JavaScript) which will add a logic to make the Flip A Coin project responsive from the user end.

Using CSS we present Flip A Coin Using HTML, CSS & JavaScript (Source Code) Using Html and CSS projects with source code available for you to copy and paste directly into your own project.

I hope you have got an idea about the project.

HTML Code for Flip a Coin

<html lang="en">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flip A Coin</title>
    <!--Google Fonts-->
    <link href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="stats">
            <p id="heads-count">Heads: 0</p>
            <p id="tails-count">Tails: 0</p>
        </div>
        <div class="coin" id="coin">
            <div class="heads">
                <img src="https://i.postimg.cc/NFsp7c41/heads.png">
            </div>
            <div class="tails">
                <img src="https://i.postimg.cc/0230rmLG/tails.png">
            </div>
        </div>
        <div class="buttons">
            <button id="flip-button">
                Flip Coin
            </button>
            <button id="reset-button">
                Reset
            </button>
        </div>
    </div>
    <!--Script-->
    <script src="script.js"></script>
</body>
</html>

First we’ll start with creating the structure of the Flip A Coin project for that as you can see in the above code we have used all the necessary elements & attributes. Let us know code the CSS part to add styling and aligned the tags.

Create Instagram Logo Using HTML CSS (Source Code)

CSS Code for Flip a Coin

*{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: "Rubik",sans-serif;
}
body{
    height: 100%;
    background: linear-gradient(
        to right,
        #575ce5 50%,
        #f9fbfc 50%
    ) fixed;
}
.container{
    background-color: #ffffff;
    width: 400px;
    padding: 50px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    box-shadow: 15px 30px 35px rgba(0,0,0,0.1);
    border-radius: 10px;
    -webkit-perspective: 300px;
    perspective: 300px;
}
.stats{
    text-align: right;
    color: #101020;
    font-weight: 500;
    line-height: 25px;
}
.coin{
    height: 150px;
    width: 150px;
    position: relative;
    margin: 50px auto;
    -webkit-transform-style: preserve-3d;
            transform-style: preserve-3d;
}
.coin img{
    width: 145px;
}
.heads,.tails{
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
            backface-visibility: hidden;
}
.tails{
    transform: rotateX(180deg);
}
@keyframes spin-tails{
    0%{
        transform: rotateX(0);
    }
    100%{
        transform: rotateX(1980deg);
    }
}
@keyframes spin-heads{
    0%{
        transform: rotateX(0);
    }
    100%{
        transform: rotateX(2160deg);
    }
}
.buttons{
    display: flex;
    justify-content: space-between;
}
button{
    width: 120px;
    padding: 10px 0;
    border: 2.5px solid #424ae0;
    border-radius: 5px;
    cursor: pointer;
}
#flip-button{
    background-color: #424ae0;
    color: #ffffff;
}
#flip-button:disabled{
    background-color: #e1e0ee;
    border-color: #e1e0ee;
    color: #101020;
}
#reset-button{
    background-color: #ffffff;
    color: #424ae0;
}

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Flip A Coin project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now, let’s code the JavaScript part to make it responsive.

CreateAdd To Cart Button Using HTML , CSS & Javascript

JavaScript Code for Flip a Coin

let heads = 0;
let tails = 0;
let coin = document.querySelector(".coin");
let flipBtn = document.querySelector("#flip-button");
let resetBtn = document.querySelector("#reset-button");

flipBtn.addEventListener("click", () => {
    let i = Math.floor(Math.random() * 2);
    coin.style.animation = "none";
    if(i){
        setTimeout(function(){
            coin.style.animation = "spin-heads 3s forwards";
        }, 100);
        heads++;
    }
    else{
        setTimeout(function(){
            coin.style.animation = "spin-tails 3s forwards";
        }, 100);
        tails++;
    }
    setTimeout(updateStats, 3000);
    disableButton();
});
function updateStats(){
    document.querySelector("#heads-count").textContent = `Heads: ${heads}`;
    document.querySelector("#tails-count").textContent = `Tails: ${tails}`;
}
function disableButton(){
    flipBtn.disabled = true;
    setTimeout(function(){
        flipBtn.disabled = false;
    },3000);
}
resetBtn.addEventListener("click",() => {
    coin.style.animation = "none";
    heads = 0;
    tails = 0;
    updateStats();
});

Last stage of the project the JavaScript in which we have added the logic and coded as per the requirement with some conditions. Let us see the Final Output of the project Flip A Coin Using HTML, CSS & JavaScript (Source Code)

Final Output Flip A Coin 


We have successfully created our Flip A Coin Using HTML, CSS & JavaScript (Source Code)  You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Thank You For Visiting!!!

Written By – Harsh Sawant

Code By – @harshh9

Free Coding Ebook 👉 Get Now

Leave a Reply