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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Rock, Paper, Scissors Game</title>
<link rel="stylesheet" href="mobile.css" />
<!-- Don't use web fonts in future, download fonts instead -->
<link href="https://fonts.googleapis.com/css?family=Play:400,700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed:700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
</head>
<body>
<div class="container">
<header class="header">
<div class="header__container">
<h1 class="header__title">Rock Paper Scissors</h1>
</div>
</header>
<main>
<div class="main__container">
<!-- Used data-behavior instead of classes or ids for JavaScript -->
<p class="user--prompt" data-behavior="game_winner">Choose a weapon, Rock, Paper or Scissors!</p>
<div class="img--choices" data-behavior="game_actions">
<button class="rock__img" data-behavior="button_clicked" value="rock"></button>
<button class="paper__img" data-behavior="button_clicked" value="paper"></button>
<button class="scissors__img" data-behavior="button_clicked" value="scissors"></button>
</div>
<div class="img--choices__reset" data-behavior="game_actions--reset">
<button class="reset__button" data-behavior="game_reset" onclick="refreshPage()">Play Again!</button>
</div>
<div class="scoreboard">
<div style="margin-left: auto; width: 400px;">
<div class="player--score">
<p class="players__score" data-behavior="players_score">0</p>
</div>
<p class="score__label--player">Player score</p>
</div>
<div style="margin-right: auto; width: 400px;">
<div class="computer--score">
<p class="computers__score" data-behavior="computers_score">0</p>
</div>
<p class="score__label--computer">Computer score</p>
</div>
</div>
<p class="round--result" data-behavior="round_result">Click on a weapon.</p>
</div>
</main>
<footer>
<div class="footer__container">
<h2>Lets Play!</h2>
</div>
</footer>
</div>
<script>
// Selects each of the buttons with images using querySelectorAll
const gameActions = document.querySelectorAll("[data-behavior='button_clicked']")
// Links the buttons to the playGame function
gameActions.forEach((button) => {
button.addEventListener('click', (event) => {
// Links the buttons to the playGame function using target and value
playGame(event.target.value)
})
})
// Resets the game using a button
const restartGame = document.querySelector("[data-behavior='game_reset']")
restartGame.addEventListener('click', () => {
location.reload()
})
let playerWins = 0
let computerWins = 0
let ties = 0
// Used instead of a number directly in determineWinner, can be changed to any number easily this way
const noOfPointsToWin = 5
const computerOptions = ['rock', 'paper', 'scissors']
// Math.floor returns numbers 0, 1, 2 for the random numbers
const playGame = (userChoice) => {
const computerChoice = computerOptions[Math.floor(Math.random() * 3)]
compareSelections(userChoice, computerChoice)
determineWinner(playerWins, computerWins)
}
// Compares player selection to computer selection
const compareSelections = (playerSelection, computerSelection) => {
if (playerSelection === computerSelection) {
ties++
document.querySelector("p[data-behavior='round_result']").innerHTML = "It's a draw!"
} else if ((playerSelection === 'rock' && computerSelection === 'scissors') ||
(playerSelection === 'paper' && computerSelection === 'rock') ||
(playerSelection === 'scissors' && computerSelection === 'paper')) {
playerWins++
document.querySelector("p[data-behavior='round_result']").innerHTML = 'Player Wins!'
} else {
computerWins++
document.querySelector("p[data-behavior='round_result']").innerHTML = 'Computer Wins!'
}
// Updates the score on the scoreboard
document.querySelector("p[data-behavior='players_score']").innerHTML = playerWins
document.querySelector("p[data-behavior='computers_score']").innerHTML = computerWins
}
const determineWinner = (playerWins, computerWins) => {
if (playerWins === noOfPointsToWin || computerWins === noOfPointsToWin) {
const winner = playerWins === noOfPointsToWin ? 'Player' : 'Computer'
// Changes text to show who won the game
document.querySelector("p[data-behavior='game_winner']").innerHTML = `${winner} won the game! Play again?`
// Removes the div containing the button images
document.querySelector("div[data-behavior='game_actions']").style.display = "none"
// Replaces the game_actions div with a button to restart the game
document.querySelector("button[data-behavior='game_reset']").style.display = "block"
}
}
</script>
</body>
</html>
/* Reset
=================================================*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
/* Base Styles
=================================================*/
body {
background-color: blanchedalmond;
font-size: 16px;
}
.container {
max-width: 405px;
margin: 0 auto;
padding: 25px;
}
h1 {
text-align: center;
font-family: 'Roboto Condensed', sans-serif;
font-size: 2.2em;
font-weight: 700;
color: #bb6e16;
padding-top: 10px;
}
h2 {
text-align: center;
font-family: 'Roboto Condensed', sans-serif;
font-size: 1.5em;
font-weight: 700;
color: blanchedalmond;
padding: 10px 0 10px 0;
}
p {
text-align: center;
font-family: 'roboto', sans-serif;
font-size: 1.em;
font-weight: 600;
color: #521806;
padding-bottom: 25px;
}
.user--prompt {
font-size: 1.5em;
line-height: 1.3;
padding-top: 15px;
}
footer {
background-color: #bb6e16;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
}
/* Images
=================================================*/
.rock__img, .paper__img, .scissors__img {
width: 130px;
height: 130px;
border: 0;
outline: none;
}
.rock__img {
background: url(https://res.cloudinary.com/dnobvlm11/image/upload/v1561714188/Rock%20Paper%20Scissors/images/rock_n3fgbe.svg);
float: left;
margin-left: 105px;
margin-bottom: 15px;
}
.paper__img {
background: url(https://res.cloudinary.com/dnobvlm11/image/upload/v1561714188/Rock%20Paper%20Scissors/images/paper_euwuv0.svg);
float: left;
margin-left: 30px;
}
.scissors__img {
background: url(https://res.cloudinary.com/dnobvlm11/image/upload/v1561714188/Rock%20Paper%20Scissors/images/scissors_nlzpsd.svg);
}
.rock__img:hover, .paper__img:hover, .scissors__img:hover {
cursor: pointer;
}
.img--choices {
width: 340px;
text-align: center;
margin: 0 auto;
}
/* Scoreboard
=================================================*/
.scoreboard {
display: flex;
justify-content: center;
margin-top: 25px;
}
.player--score {
display: block;
width: 75px;
height: 50px;
background-color: #8a280a;
float: right;
margin-right: 15px;
border-radius: 10px;
}
.computer--score {
display: block;
width: 75px;
height: 50px;
background-color: #8a280a;
float: left;
border-radius: 10px;
margin-left: 15px;
}
.computer--score p {
font-family: 'Play', sans-serif;
font-weight: 700;
font-size: 2em;
color: #fff;
text-align: center;
vertical-align: middle;
line-height: 45px;
}
.player--score p {
font-family: 'Play', sans-serif;
font-weight: 700;
font-size: 2em;
color: #fff;
text-align: center;
vertical-align: middle;
line-height: 45px;
}
.round--result {
font-family: 'roboto', sans-serif;
font-size: 1.6em;
margin-top: 10px;
}
.score__label--player {
font-family: 'roboto', sans-serif;
font-size: 0.8em;
font-weight: 600;
padding-top: 10px;
padding-right: 5px;
margin-right: 15px;
float: right;
text-align: center;
vertical-align: middle;
}
.score__label--computer {
display: inline-block;
font-family: 'roboto', sans-serif;
font-size: 0.8em;
font-weight: 600;
padding-top: 10px;
padding-left: 5px;
padding-bottom: 15px;
margin-left: 15xp;
text-align: center;
vertical-align: middle;
}
/* Buttons
=================================================*/
.start__btn {
display: block;
margin: 0 auto;
margin-top: 35px;
width: 200px;
height: 35px;
border-radius: 10px;
background-color: #bb6e16;
color: #fff;
font-family: 'Roboto Condensed', sans-serif;
font-size: 1.6em;
outline: none;
}
.start__btn:hover {
cursor: pointer;
}
.reset__btn {
display: block;
margin: 20px auto;
width: 200px;
height: 35px;
border: 1px solid rgb(255, 215, 155);
border-radius: 10px;
background-color: #521806;
color: #fff;
font-family: 'Roboto Condensed', sans-serif;
font-size: 1.6em;
outline: none;
}
.reset__btn:hover {
cursor: pointer;
}
/* Main reset button */
.reset__button {
display: block;
width: 300px;
height: 70px;
border: 1px solid rgb(255, 215, 155);
border-radius: 10px;
background-color: #521806;
color: #fff;
font-family: 'Roboto Condensed', sans-serif;
font-size: 2em;
outline: none;
margin: 103px auto 103px;
display: none;
}
.reset__button:hover {
cursor: pointer;
}
@media only screen and (min-width: 376px) {
.container {
max-width: 767px;
margin: 0 auto;
padding: 25px;
}
h1 {
font-size: 2.5em;
}
.user--prompt {
font-size: 1.7em;
line-height: 1.3;
padding-top: 25px;
}
.img--choices {
padding: 35px 0 25px 0;
width: 360px;
}
.rock__img, .paper__img, .scissors__img {
width: 150px;
height: 150px;
}
.rock__img {
margin-bottom: 25px;
}
.paper__img {
clear: both;
float: left;
margin-left: 20px;
margin-right: 20px;
}
.scoreboard {
display: flex;
justify-content: center;
margin: 25px 0;
}
.round--result {
font-size: 1.8em;
padding-top: 15px;
}
.score__label--player {
font-family: 'roboto', sans-serif;
font-size: 0.9em;
font-weight: 600;
padding-top: 10px;
padding-right: 5px;
margin-right: 15px;
float: right;
text-align: center;
vertical-align: middle;
}
.score__label--computer {
display: inline-block;
font-family: 'roboto', sans-serif;
font-size: 0.9em;
font-weight: 600;
padding-top: 10px;
padding-left: 5px;
padding-bottom: 15px;
margin-left: 15xp;
text-align: center;
vertical-align: middle;
}
.scoreboard {
display: flex;
justify-content: center;
margin-top: 25px;
max-width: 340px;
margin: 0 auto;
}
}
@media only screen and (min-width: 768px) {
.container {
max-width: 767px;
margin: 0 auto;
padding: 25px;
}
.header__container {
max-width: 767px;
margin: 0 auto;
}
.main__container {
max-width: 767px;
margin: 0 auto;
}
h1 {
font-size: 4em;
padding-bottom: 25px;
text-align: center;
}
.user--prompt {
font-size: 1.8em;
line-height: 1.3;
padding-top: 15px;
}
p {
font-size: 2.2em;
padding-bottom: 25px;
}
.img--choices {
width: 725px;
margin: 0 auto;
padding-top: 50px;
}
.rock__img, .paper__img, .scissors__img {
width: 175px;
height: 175px;
}
.rock__img {
float: none;
margin: 0 50px 0 0;
}
.paper__img {
float: none;
margin-right: 50px;
}
.scissors__img {
float: none;
}
.scoreboard {
clear: both;
display: flex;
justify-content: center;
padding-top: 50px;
margin: 0 auto;
width: 760px;
}
.player--score {
display: inline-block;
width: 100px;
height: 75px;
background-color: #8a280a;
float: right;
margin-right: 20px;
border-radius: 10px;
}
.computer--score {
display: inline-block;
width: 100px;
height: 75px;
background-color: #8a280a;
float: left;
border-radius: 10px;
margin-left: 20px;
}
.computer--score p {
font-family: 'Play', sans-serif;
font-weight: 700;
font-size: 2.4em;
color: #fff;
text-align: center;
vertical-align: middle;
line-height: 70px;
}
.player--score p {
font-family: 'Play', sans-serif;
font-weight: 700;
font-size: 2.4em;
color: #fff;
text-align: center;
vertical-align: middle;
line-height: 70px;
}
.round--result {
font-family: 'roboto', sans-serif;
font-size: 1.6em;
margin-top: 20px;
}
.score__label--player {
font-family: 'roboto', sans-serif;
font-size: 1em;
font-weight: 600;
padding-top: 10px;
margin-right: 20px;
vertical-align: middle;
}
.score__label--computer {
display: inline-block;
font-family: 'roboto', sans-serif;
font-size: 1em;
font-weight: 600;
padding-top: 10px;
margin-left: 10px;
vertical-align: middle;
}
.round--result {
padding-top: 25px;
font-size: 2.3em;
}
}
@media only screen and (min-width: 960px) {
.container {
width: 960px;
max-width: 960px;
margin: 0 auto;
padding: 25px;
}
.header__container {
width: 960px;
margin: 0 auto;
padding: 0 30px;
}
.main__container {
width: 960px;
margin: 0 auto;
padding: 0 30px;
}
.footer__container {
width: 960px;
margin: 0 auto;
padding: 0 30px;
}
h1 {
font-size: 4.5em;
padding-bottom: 15px;
}
p {
font-size: 2.5em;
padding-bottom: 25px;
}
.img--choices {
max-width: 960px;
text-align: center;
margin: 0 auto;
}
.rock__img, .paper__img, .scissors__img {
width: 200px;
height: 200px;
}
.rock__img {
margin-right: 45px;
padding: 0;
display: inline-block;
}
.paper__img {
margin-right: 45px;
padding: 0;
display: inline-block;
}
.scissors__img {
margin: 0;
padding: 0;
display: inline-block;
}
.user--prompt {
font-size: 2.3em;
}
.scoreboard {
padding-top: 15px;
width: 1280px;
}
}
@media only screen and (min-width: 1280px) {
.container {
width: 1280px;
max-width: 1280px;
margin: 0 auto;
padding: 25px;
}
.header__container {
width: 1280px;
max-width: 1280px;
margin: 0 auto;
padding: 0 30px;
}
.main__container {
width: 1280px;
max-width: 1280px;
margin: 0 auto;
padding: 0 30px;
}
.footer__container {
width: 1280px;
max-width: 1280px;
margin: 0 auto;
padding: 0 30px;
}
.img--choices {
width: 1280px;
text-align: center;
margin: 0 auto;
padding-top: 15px;
}
h1 {
font-size: 5em;
}
h2 {
margin: 0 auto;
padding-left: 70px;
}
.user--prompt {
font-size: 2.3em;
padding-top: 15px;
}
.rock__img, .paper__img, .scissors__img {
width: 250px;
height: 250px;
}
.rock__img {
margin-right: 20px;
padding: 0;
display: inline;
}
.paper__img {
margin-right: 20px;
padding: 0;
display: inline;
margin: 0 75px;
}
.scissors__img {
outline: none;
margin: 0;
padding: 0;
display: inline;
}
.round--result {
padding-top: 5px;
font-size: 2.3em;
}
}
function refreshPage() {
window.location.href = window.location.href;
}
Also see: Tab Triggers