HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<div class="row r_title mb-4 w-100">
<div class="col-12">
<div class="title p-2 text-center">
<h1>A Dice Game</h1>
</div>
</div>
</div>
<div class="main">
<div class="row">
<div class="col-12">
<h3 class="text-capitalize" id="np1"></h3>
</div>
</div>
<div class="row">
<div class="col-6 ">
<div class="pad border right" id="p1_1"></div>
</div>
<div class="col-6">
<div class="pad border" id="p1_2"></div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="throw_b right">
<button type="button" data-player="p1_1" class="btn btn-sm btn-secondary throw_btn"
id="buttonp1_1">ROLL!</button>
</div>
</div>
<div class="col-6 ">
<div class="throw_b">
<button type="button" data-player="p1_2" class="btn btn-sm btn-secondary throw_btn"
id="buttonp1_2">ROLL!</button>
</div>
</div>
</div>
<div class="row contres" id="contres"></div>
<div class="row mt-4">
<div class="col-12">
<h3 class="text-capitalize" id="np2"></h3>
</div>
</div>
<div class="row">
<div class="col-6 ">
<div class="pad border right" id="p2_1">
</div>
</div>
<div class="col-6">
<div class="pad border" id="p2_2"></div>
</div>
</div>
<div class="row">
<div class="col-6">
<div class="throw_b right">
<button type="button" data-player="p2_1" class="btn btn-sm btn-secondary throw_btn"
id="buttonp2_1">ROLL!</button>
</div>
</div>
<div class="col-6 ">
<div class="throw_b">
<button type="button" data-player="p2_2" class="btn btn-sm btn-secondary throw_btn"
id="buttonp2_2">ROLL!</button>
</div>
</div>
</div>
</div>
body {
background-color: #085b08;
color: white;
display: flex;
flex-direction: column;
min-height: 100vh;
align-items: center;
justify-content: center;
}
.main {
width: 100%;
text-align: center;
}
.row {
margin: 0 0 0 0;
}
.r_title {
background-color: #024505;
}
.right {
float: right;
}
.pad{
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
background-color: #024505;
margin-top: 10px;
margin-bottom: 10px;
}
.border {
border: 1px solid #116b11!important;
}
img {
width: 100%;
}
.throw_b {
width: 200px;
}
.main > div:nth-child(1) {
background-color: #007780;
}
.main > div:nth-child(5) {
background-color: #800014;
}
.text-capitalize {
padding-top: 5px;
}
.contres{
margin:0;
padding:0;
}
#res{
margin:0;
padding:30px;
display: block;
z-index: 99;
width: 100%;
position: fixed;
background-color:rgba(0, 0, 0, 0.7);
bottom: 50%;
transform: translate(0, 50%) skewY(-5deg);
}
.throw_btn {
color: #28a745;
background-color: #f8f9fa;
border-color: #024505;
font-weight: bold;
}
.throw_btn:hover {
color: #f8f9fa;
border-color: #28a745;
background-color: #024505;
font-weight: bold;
}
@media screen and (max-width: 500px) {
.pad{
width: 130px; height: 130px;
}
.throw_b{
width: 130px;
}
}
// Get players name with prompt
var np1 = prompt('Player 1, insert your name');
var np2 = prompt('Player 2, insert your name');
// Check names
if (np1 && np2) {
var names = (np1 + np2).replace(/\s/g, '');
if (!names) {
defaultNames();
} else {
alert('LET\'S GO, good luck!');
}
} else {
defaultNames();
}
(function () {
// Get the players' name divs and show those names
document.getElementById('np1').innerHTML = np1;
document.getElementById('np2').innerHTML = np2;
// Add event listener to all the Roll Buttons
let rollingButtons = document.querySelectorAll(".throw_btn");
rollingButtons.forEach(item => {
item.addEventListener('click', Roll);
});
})();
// Set initial variables
let rollsCounter = 0; // Rolls counter
let rollsValues = { // Players rolls values
p1_1: 0,
p1_2: 0,
p2_1: 0,
p2_2: 0,
};
// Function called at every roll
function Roll(event) {
// Get the button that was clicked and disable it
let button = event.target;
button.removeEventListener('click', Roll);
button.classList.add('disabled');
// Get the data-player attribute to know wo rolled the dice
let player = event.target.getAttribute('data-player');
// Get a random nr from 1 to 6
let thisRoll = Math.floor(Math.random() * 6) + 1;
// Get the right div where the dice should appear and show it
let diceWrapper = document.getElementById(player);
diceWrapper.innerHTML = '<img src="https://www.pgxstudio.com/dev/dice/' + thisRoll + '.png"/>';
// Update player rolls value
rollsValues[player] = thisRoll;
// Increment rolls counter
rollsCounter++;
// When er reach the 4th roll end the game
if (rollsCounter == 4) {
endGame();
}
}
function endGame() {
// Calculate the sum of the rolls for each player
totp1 = rollsValues.p1_1 + rollsValues.p1_2;
totp2 = rollsValues.p2_1 + rollsValues.p2_2;
// Check who won
if (totp2 > totp1) {
winner = np2 + ' <br>YOU WON';
} else if (totp2 < totp1) {
winner = np1 + ' <br>YOU WON';
} else {
winner = 'TIE!';
}
// Grab the div where the result will be shown
divres = document.getElementById("contres");
// Create result html and show it
divres.innerHTML = `
<div class="col-12" id="res">${np1}: ${totp1} - ${np2}: ${totp2}
<h3 class="text-capitalize">${winner}</h3>
<a href="#"><button class="tn btn-sm btn-secondary">PLAY AGAIN</button></a><br />
</div>`;
}
function defaultNames() {
np1 = 'Player 1';
np2 = 'Player 2';
alert('Ok then, I\'ll call you Player 1 and Player 2 :(');
}
Also see: Tab Triggers