<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>
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;
}
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);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.