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.
<!--Container-->
<div id="container">
<!--Row-->
<div class='row'>
<span id="music-control" class="btn btn-danger">
<i id='music-icon' class="fa fa-volume-up" aria-hidden="true"></i>
<span id="sound-tooltip"class='tooltip'>Toggle Sound</span>
</span>
<h1>Star Wars</h1>
<h3>Tic-Tac-Toe</h3>
<span id="reset" class="btn btn-primary">
Reset
<span id="sound-tooltip"class='tooltip'>Reset Game</span>
</span>
</div>
<!--End Row-->
<!--Row-->
<div class='row'>
<!--Wrapper-->
<div id='wrapper'>
<!--Game Board-->
<div id='board'>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
<div class='box'></div>
</div>
<!--End Game Board-->
</div>
<!--End Wrapper-->
</div>
<!--End Row-->
<!-- Music theme song -->
<audio id="theme-song">
<source src='http://www.digitpress.com/dpsoundz/mp3/dp_starwars.mp3'>
</audio>
<audio id="darkside-audio">
<source src="http://www.digitpress.com/dpsoundz/mp3/dp_starwars_darkside.mp3">
</audio>
<audio id="lightside-audio">
<source src="http://www.digitpress.com/dpsoundz/mp3/dp_starwars_theme.mp3">
</audio>
<!-- End Music theme song -->
</div>
<!--End Container-->
<!-- Modals -->
<div id="overlay"></div>
<div id="modal">
<span id="close-modal"><i class="fa fa-times" aria-hidden="true"></i>
</span>
</div>
// VARIABLES
@bg-image : 'https://mnmlwalls.files.wordpress.com/2015/01/star-wars.png';
@primary-color : #607d8b;
@danger-color: #f44336;
@light-gray: #d3d3d3;
@yellow: #ffc107;
@orange: #FF9800;
@black: #111;
@no-color: rgba(0, 0, 0, 0);
@box-bg-color: rgba(95, 108, 116, 0.44);
@box-hover-color: rgba(227, 227, 227, 0.5);
@overlay-color: rgba(0, 0, 0, 0.35);
// GENERAL
body {
font-family: 'Roboto', sans-serif;
background: #302f2f;
color: white;
.row {
width: 100%;
&:after {
content: "";
display: table;
clear: both;
}
}
.btn {
width: 75px;
padding: 5px;
transition: all .25s ease-in-out;
border: 2px solid @no-color;
&:hover {
background: none;
border: 2px solid @light-gray;
color: @light-gray;
}
}
.btn-primary {
background: @primary-color;
}
.btn-danger {
background: @danger-color ;
}
}
// MAIN CONTENT
#container {
width: 960px;
margin: 0 auto;
text-align: center;
position: relative;
h1, h3 {
text-transform: uppercase;
color: @yellow;
margin: 10px 0 0 0;
}
h3 {
margin-bottom: 10px
}
#reset, #music-control {
position: absolute;
top: 0;
.tooltip{
position: absolute;
top: 35px;
left: 5px;
border-radius: 5px;
background: @light-gray;
color: @black;
font-size: .75em;
font-weight: bold;
padding: 5px;
opacity: 0;
transition: all .25s ease-in-out;
&:after {
content: " ";
position: absolute;
left: 50%;
bottom: 100%;
border-style: solid;
border-width: 5px;
margin-left: -5px;
border-color: transparent transparent @light-gray transparent;
}
}
&:hover .tooltip{
opacity: 1;
}
}
#reset {
right: 25%;
}
#music-control {
left: 25%;
}
}
// GAME BOARD
#board {
background: url(@bg-image) no-repeat center center;
background-size: cover;
width: 500px;
height: 500px;
margin: 0 auto;
.box {
background-color: @box-bg-color;
margin: 5px;
height: 155px;
width: 155px;
float: left;
vertical-align: top;
text-align: center;
transition: all .25s ease-in-out;
&:hover {
background: @box-hover-color;
transform: scale(1.1);
}
img {
width: 100%;
}
}
#luke-img img,
#vader-img img {
width: 25%;
}
}
// MODAL MESSAGE
#overlay {
width: 100%;
height: 100%;
position: fixed;
top: 0;
background: @overlay-color;
display: none;
}
#modal {
width: 25%;
position: absolute;
top: 15%;
left: 0;
right: 0;
margin: 0 auto;
background: @orange;
box-shadow: 5px 5px 5px @black;
text-align: center;
display: none;
padding: 50px;
#close-modal {
position: absolute;
top: 10px;
right: 15px;
font-size: 1.25em;
border: 2px solid white;
border-radius: 100%;
height: 25px;
width: 25px;
transition: all .25s ease-in-out;
&:hover{
transform: scale(1.2);
}
}
}
// VARIABLES
// =========================
var playerTurn = 0;
var luke = '<img src="http://icons.iconarchive.com/icons/creativeflip/starwars-longshadow-flat/128/Luke-Skywalker-icon.png">';
var vader = '<img src="http://icons.iconarchive.com/icons/creativeflip/starwars-longshadow-flat/128/Darth-Vader-icon.png">';
var boxes = document.getElementsByClassName('box');
var resetBtn = document.getElementById('reset');
var musicBtn = document.getElementById('music-control');
var musicIcon = document.getElementById('music-icon');
var closeModalBtn = document.getElementById('close-modal');
var overlay = document.getElementById("overlay");
var modal = document.getElementById("modal");
var themeSong = document.getElementById('theme-song');
var darkSideClip = document.getElementById('darkside-audio');
var lightSideClip = document.getElementById('lightside-audio');
var soundOn = true;
// modal messages
var modalMessage = {
box_occupied: "Sorry, Pick another Square",
vader_wins: "Now you know the power of the dark side<br> - Vader wins",
luke_wins: "The force is strong with this one<br> - Luke wins"
}
// EVENT LISTENERS
// =========================
//Attach an Event Listener to each box
for(var i = 0; i < boxes.length; i++ ){
boxes[i].addEventListener('click', setPlayerTurn);
}
// Event Listener to reset the board
resetBtn.addEventListener('click', resetBoard);
//Event Listener to close the modal
closeModalBtn.addEventListener('click', closeModal);
// Event Listener to play music
musicBtn.addEventListener('click', function (){
if(soundOn === true) {
soundOn = false;
musicIcon.className = "fa fa-volume-off"
}else if (soundOn === false) {
soundOn = true;
musicIcon.className = "fa fa-volume-up"
}
})
// GAME FUNCTIONS
// =========================
// Setting player turn on each click
function setPlayerTurn(){
// first check if box is unoccupied
if(this.innerHTML !== ''){
openModal(modalMessage.box_occupied);
// if turn is even number set the box as luke
}else if(playerTurn % 2 == 0){
playerTurn += 1;
this.innerHTML = luke;
getWinner();
// if it's odd, then set the box as vader
}else {
playerTurn += 1;
this.innerHTML = vader;
getWinner();
}
}
function getWinner(){
//Luke Wins Across
if(boxes[0].innerHTML === luke && boxes[1].innerHTML === luke && boxes[2].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}else if(boxes[3].innerHTML === luke && boxes[4].innerHTML === luke && boxes[5].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}else if(boxes[6].innerHTML === luke && boxes[7].innerHTML === luke && boxes[8].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}
//Luke Wins Down
else if(boxes[0].innerHTML === luke && boxes[3].innerHTML === luke && boxes[6].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}else if(boxes[1].innerHTML === luke && boxes[4].innerHTML === luke && boxes[7].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}else if(boxes[2].innerHTML === luke && boxes[5].innerHTML === luke && boxes[8].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}
//Luke Wins Diagonal
else if(boxes[0].innerHTML === luke && boxes[4].innerHTML === luke && boxes[8].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}else if(boxes[6].innerHTML === luke && boxes[4].innerHTML === luke && boxes[2].innerHTML === luke){
openMessageAndPlayMusic(modalMessage.luke_wins, lightSideClip);
}
//Vader Wins Across
else if(boxes[0].innerHTML === vader && boxes[1].innerHTML === vader && boxes[2].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}else if(boxes[3].innerHTML === vader && boxes[4].innerHTML === vader && boxes[5].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}else if(boxes[6].innerHTML === vader && boxes[7].innerHTML === vader && boxes[8].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}
//Vader Wins Down
else if(boxes[0].innerHTML === vader && boxes[3].innerHTML === vader && boxes[6].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}else if(boxes[1].innerHTML === vader && boxes[4].innerHTML === vader && boxes[7].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}else if(boxes[2].innerHTML === vader && boxes[5].innerHTML === vader && boxes[8].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}
//Vader Wins Diagonal
else if(boxes[0].innerHTML === vader && boxes[4].innerHTML === vader && boxes[8].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}else if(boxes[6].innerHTML === vader && boxes[4].innerHTML === vader && boxes[2].innerHTML === vader){
openMessageAndPlayMusic(modalMessage.vader_wins, darkSideClip);
}
}
// function to reset board
function resetBoard(){
for(var i = 0; i < boxes.length; i++){
boxes[i].innerHTML = '';
}
playerTurn = 0;
}
// function when either luke or vader wins
function openMessageAndPlayMusic(player,song){
// open messsage
openModal(player);
// play song if sound is on
function playSong(){
if(soundOn === true){
song.play();
}
}
// invoke song function
return playSong(song);
}
// Open modal and set message
function openModal(message){
var h2 = document.createElement("h2");
overlay.style.display = 'block';
modal.style.display = 'block';
h2.innerHTML = message;
h2.id = "message";
modal.appendChild(h2)
}
// Close the modal
function closeModal(){
overlay.style.display = "none";
modal.style.display = "none";
// remove the previous message
if(modal.children[1]){
modal.removeChild(modal.children[1]);
}
}
Also see: Tab Triggers