Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,500;0,600;1,400&display=swap" rel="stylesheet">
    <link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
    <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
    <link rel="manifest" href="/site.webmanifest">
    <link rel="stylesheet" href="style.css">
    <title>Dice Roll Simulator</title>
</head>
<body>
    <h1>Dice Roll Simulator</h1>
    <div class="container">
        <div id="dice" class="dice">&#9860;</div> 
        <!-- dice face 5 unicode &#9860; -->
        <button id="roll" type="submit">Roll Dice</button>

        <ul class="dice-history">
           <!-- dynamically gotten from JavaScript -->
           <li>Roll 1:<span>&#9860;</span></li>
           <li>Roll 2:<span>&#9860;</Roll></li>
        </ul>
    </div>
    
</body>
</html>
              
            
!

CSS

              
                *, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
  --step--3: clamp(0.61rem, calc(0.39rem + 1.82vw), 1.02rem);
  --step--2: clamp(0.69rem, calc(0.36rem + 2.62vw), 1.28rem);
  --step--1: clamp(0.78rem, calc(0.32rem + 3.66vw), 1.60rem);
  --step-0: clamp(0.88rem, calc(0.25rem + 5.00vw), 2.00rem);
  --step-1: clamp(0.98rem, calc(0.14rem + 6.74vw), 2.50rem);
  --step-2: clamp(1.11rem, calc(-0.01rem + 8.97vw), 3.13rem);
  --step-3: clamp(1.25rem, calc(-0.23rem + 11.83vw), 3.91rem);
  --step-4: clamp(1.40rem, calc(-0.53rem + 15.47vw), 4.88rem);
  --step-5: clamp(1.58rem, calc(-0.94rem + 20.12vw), 6.10rem);
}

image {
    width: 100%;
}

body {
    height: 100vh;
    background-image: url("https://img.freepik.com/free-photo/starry-night-sky_1048-11828.jpg");
    display: flex;
    flex-direction: column;
    padding: 2em 0;
    /* justify-content: center; */
}

.container {
    width: min(80%, 450px);
    align-self: center;
    padding: 2em;
    box-shadow: 3px 3px 5px rgba(255, 255, 255, 1);
    /* height: 70vh; */
}

h1 {
    color: #fff;
    font-family: 'Poppins', sans-serif;
    font-weight: 500;
    font-size: var(--step-2);
    text-align: center;
    
    margin-bottom: 5vw;
}

.dice {
    font-size: 5rem;
    color: #fff;
    text-align: center;
    margin-bottom: 0.5em;
    animation: rollDice duration 1s  forwards;
}

#roll {
    display: block; /* without display block, the button won't accept margin: 0 auto; effects that tens to center it */
    cursor: pointer;
    background: #000;
    color: #fff;
    font-size: 1.3rem;

    border-radius: 10px;
    padding: 0.5em 1em;
    margin: 0 auto;
    margin-bottom: 2em;
    transition: opacity 0.5s ease;
}

#roll:hover {
    opacity: 0.5;
}

.roll-animation {
    animation: rollDice 0.8s ;
}

@keyframes rollDice {
    0% {
        transform: rotateX(0deg) rotateY(0deg) rotateZ(-720deg)
    }
    100% {
        transform: rotateX(0deg) rotateY(0deg) rotateZ(720deg)
    }

}

.dice-history li {
    list-style: none;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #fff;
    color: #000;
    font-size: 1.3rem;

    margin-top: 0.8em;
    padding: 0 0.3em;
    border-radius: 5px;
    justify-content: space-between;
    box-shadow: 3px 3px 3px rgb(185, 163, 163);
} 

li span {
    font-size: 2.5rem;
}




              
            
!

JS

              
                

//Selectors
const buttonEl = document.querySelector('#roll');
const dice = document.querySelector('.dice');
const rollHistory = document.querySelector(".dice-history")


//EventListeners
buttonEl.addEventListener('click', diceRoll);


let historyList = [];

function rolledDice() {
    let rollResult = Math.floor(Math.random() * 6) + 1; 
    //creates a random number between 1 and 6, the 
    //Math.random() * 6 will generate a random number from 0 to 5 
    //and 1 will be added to it.
    let diceFace = getDiceFace(rollResult);
    dice.innerHTML = diceFace;
    historyList.push(rollResult);
    updateRollHistory();
}

function updateRollHistory() {
    rollHistory.innerHTML = "";

    for (let i = 0; i < historyList.length; i++) {
        const listItem = document.createElement("li");
        listItem.innerHTML = `Roll ${i + 1}: <span>${getDiceFace(historyList[i])}</span>`
        rollHistory.appendChild(listItem);
        
    }
}

function getDiceFace(rollResult) {
    switch (rollResult) {
        // Unicode is used to get the dice face number
        case 1:
            return "&#9856;"
            break;
        case 2:
            return "&#9857;";
            break;
        case 3:
            return "&#9858;";
            break;
        case 4:
            return "&#9859;";
            break;
        case 5:
            return "&#9860;"
            break;
        case 6:
            return "&#9861;";
            break;
    
        default:
            return "";
            break;
    }
}

function diceRoll() {
    dice.classList.add("roll-animation");
    setTimeout(() => {
      dice.classList.remove("roll-animation");
        rolledDice()
    }, 1000);
}


              
            
!
999px

Console