Dice Game Using HTML, CSS & JavaScript (Source Code)
Dice Game Using HTML, CSS & JavaScript (Source Code)
Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make a dice game in which the user will click on the button and the dice will randomly give the value. 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. Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment. At last, we will use JS (JavaScript) which will add logic to make the project responsive from the user end.
In this blog post, we will discuss Dice Game Using HTML, CSS & JavaScript (Source Code) with complete source code for you so that you can just copy and paste it into your own project. Happy exploring and learning !!
I hope you have got an idea about the project.
HTML Code for Dice Game
<div class="wrapper"> <div class="column"> <div id="dice-side-1" class="dice">0</div> <div id="dice-side-2" class="dice">0</div> </div> <div class="column"> <button type="button" class="dice-roll">roll dice</button> <h2 id="status"></h2> </div> </div>
First, we’ll start with creating the structure of the 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.

Do you want to learn HTML to React? 🔥
If yes, then here is our Master HTML to React 📚 In this eBook, you’ll Get Complete Free Hand Written Notes on HTML, CSS, JavaScript, and React 💪. It includes 450 Projects with source code. and 250+ Most Asked Interview Questions
Get your eBook now! 👇
Temperature Convertor using HTML, CSS &JavaScript
CSS Code for Dice Game
body { position: relative; background: orange; } .wrapper { width: 400px; margin: 0 auto; display: flex; justify-content: space-between; } .column { display: flex; align-items: center; } #status { position: absolute; top: 5rem; left: 50%; transform: translateX(-50%); min-width: 400px; margin: 0; text-align: center; } .dice { width: 32px; float: left; background-color: lightcoral; border: 1px solid black; padding: 10px; font-size: 24px; text-align: center; margin: 5px; border-radius: 5px; } .dice-roll { cursor: pointer; text-transform: capitalize; }
Second, comes the CSS code which we have styled for the structure we have padded as well as aligned the 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.
Palindrome Checker using HTML, CSS and JavaScript
JavaScript Code for Dice Game
window.addEventListener( 'DOMContentLoaded', function () { const buttonRoolDice = document.querySelector( '.dice-roll' ); function rollDice () { const diceSide1 = document.getElementById( 'dice-side-1' ); const diceSide2 = document.getElementById( 'dice-side-2' ); const status = document.getElementById( 'status' ); const side1 = Math.floor( Math.random() * 6 ) + 1; const side2 = Math.floor( Math.random() * 6 ) + 1; const diceTotal = side1 + side2; diceSide1.innerHTML = side1; diceSide2.innerHTML = side2; status.innerHTML = 'You rolled ' + diceTotal + '.'; if ( side1 === side2 ) { status.innerHTML += ' Doubles! You get a free turn!'; } } buttonRoolDice.addEventListener( 'click', rollDice, false ); }, false);
The last stage of the project the JavaScript in which we added the logic and coded as per the requirement with some conditions also a function is defined to delete the input in the list. Let us see the Final Output of the project Dice Game Using HTML, CSS & JavaScript (Source Code)
Final Output Dice Game Using HTML, CSS & JavaScript
We have Successfully created our Dice Game 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 codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.
Thank You And Keep Learning!!!
Written By – Harsh Sawant
Code By – @harshh9