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="infobuttcontainer">
<button class="infobutt">?</button>
</div>
<div class="disclaimer hide1">
<p>Click a color button, paint away. Make sure to change the grid size and unit size before painting, <em>changing these values mid-painting will re-create the artboard and your progress will be lost.</em> If you want to use an image as a template, drag and drop the image onto your artboard, you should then be able to toggle it on and off. When you click 'generate div', a single div is created with a css block applied to it to render your artwork. It's also copied to your clipboard just in case you want to use it elsewhere.</p>
</div>
<div class="gridsize">
<div>
<label>Columns:</label>
<input id="columninput" type="text" value="40">
</div>
<div>
<label>Rows:</label>
<input id="rowinput" type="text" value="30">
</div>
<div>
<label>Grid Unit Size (in pixels):</label>
<input id="gridunitsize" type="text" value="12">
</div>
<div>
Show Background-Image:
<label class="switch">
<input id="imgbgtoggle" type="checkbox" disabled>
<span class="slider round"></span>
</label>
</div>
</div>
<div class="pcard">
<div class="pixelboard">
<input class="file-upload-input" type='file' onchange="readURL(this);" accept="image/*" style="display:none;" />
</div>
<div id="space-invader"></div>
<div class="knob left"></div>
<div class="knob right"></div>
</div>
<div class="button-container">
<div class="colorbutton">
<button id="button1" class="spaceButt" data-color="#000" style="background-color: #000; box-shadow: -5px 4px 0px #000, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput1" value="#000000" /><input id="colorinput1" type="color" value="#000000" /></div>
</div>
<div class="colorbutton">
<button id="button2" class="spaceButt" data-color="#FFF" style="background-color: #FFF; box-shadow: -5px 4px 0px #FFF, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput2" value="#FFFFFF" /><input id="colorinput2" type="color" value="#FFFFFF" /></div>
</div>
<div class="colorbutton">
<button id="button3" class="spaceButt" data-color="#F44336" style="background-color: #F44336; box-shadow: -5px 4px 0px #F44336, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput3" value="#F44336" /><input id="colorinput3" type="color" value="#F44336" /></div>
</div>
<div class="colorbutton">
<button id="button4" class="spaceButt" data-color="#FDD835" style="background-color: #FDD835; box-shadow: -5px 4px 0px #FDD835, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput4" value="#FDD835" /><input id="colorinput4" type="color" value="#FDD835" /></div>
</div>
<div class="colorbutton">
<button id="button5" class="spaceButt" data-color="#1E88E5" style="background-color: #1E88E5; box-shadow: -5px 4px 0px #1E88E5, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput5" value="#1E88E5" /><input id="colorinput5" type="color" value="#1E88E5" /></div>
</div>
<div class="colorbutton">
<button id="button6" class="spaceButt" data-color="#43b87d" style="background-color: #43b87d; box-shadow: -5px 4px 0px #43b87d, -6px 5px 0px #000;"></button>
<div><input type="text" id="textinput6" value="#43b87d" /><input id="colorinput6" type="color" value="#43b87d" /></div>
</div>
</div>
<button id='gencode'>
GENERATE DIV.
</button>
@import url('https://fonts.googleapis.com/css?family=Oswald');
html,
body {
padding: 0;
margin: 0;
font-family: 'Oswald',
sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
min-height: 100vh;
background: #C9D6FF;
/* fallback for old browsers */
background: -webkit-linear-gradient(to right, #E2E2E2, #C9D6FF);
/* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #E2E2E2, #C9D6FF);
/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
}
.disclaimer {
position: absolute;
font-size: 19px;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
width: 100%;
background: #C9D6FF;
/* fallback for old browsers */
background: -webkit-linear-gradient(to right, #E2E2E2, #C9D6FF);
/* Chrome 10-25, Safari 5.1-6 */
background: linear-gradient(to right, #E2E2E2, #C9D6FF);
/* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
z-index: 100;
}
.disclaimer p {
width: 600px
}
.disclaimer p em{
color: red;
}
.spaceButt {
border: 1px solid #000;
border-radius: 50%;
height: 50px;
width: 50px;
margin: 40px 10px;
transition: all .05s ease-in-out;
cursor: pointer;
}
.button-container {
display: flex;
justify-content: center;
}
.button-container input[type="text"] {
padding: 10px;
width: 100px;
border-radius: 3px;
border: none;
}
.colorbutton {
display: flex;
flex-direction: column;
margin: 0 5px;
align-items: center;
}
.spaceButt:focus {
outline: none;
}
.spaceButt:active,
.spaceButt.pressed {
transform: translate(-7px, 6px);
box-shadow: none !important;
transition: all .05s ease-in-out;
}
input[type="color"] {
padding: 0;
margin: 0;
border: 0;
background-color: transparent;
box-shadow: none;
cursor: pointer;
height: 30px;
width: 30px;
vertical-align: middle;
margin-left: -35px;
}
.pixelboard {
display: grid;
border: .5px solid #d6d6d6;
position: relative;
background-size: cover;
background-position: center;
background-color: #f0f0f0;
}
.pixelboard div {
border: .5px solid #d6d6d6;
}
.file-upload-input {
position: absolute;
margin: 0;
padding: 0;
width: 100%;
height: 100%;
outline: none;
opacity: 0;
cursor: pointer;
}
.gridsize {
display: flex;
}
.gridsize div {
padding: 15px;
display: flex;
align-items: center;
justify-content: center;
}
.gridsize input {
padding: 8px;
margin: 6px;
width: 30px;
font-size: 18px;
text-align: center;
border-radius: 3px;
border: none;
}
.pcard {
background-color: #ffffffa6;
box-shadow: 20px 50px 0px 0px #F44336,
20px 0px 0px 0px #F44336,
0px -20px 0px 0px #F44336,
-20px -20px 0px 0px #F44336,
0px 50px 0px 0px #F44336,
-20px 50px 0px 0px #F44336,
20px -20px 0px 0px #F44336;
transform-style: preserve-3d;
transition: -webkit-transform 0.6s;
-webkit-transition: -webkit-transform 0.6s;
transition: transform 0.6s;
transition: transform 0.6s,
-webkit-transform 0.6s;
border-radius: 27px;
margin: 15px 15px 40px 15px;
}
.knob {
height: 28px;
width: 28px;
background-color: #fff;
position: absolute;
border-radius: 50%;
bottom: -37px;
}
.knob.left {
left: 30px;
}
.knob.right {
right: 30px;
}
/* sliders */
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked+.slider {
background-color: #41d47e;
}
input:focus+.slider {
box-shadow: 0 0 1px #41d47e;
}
input:checked+.slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
.hide1 {
display: none;
}
.hide0 {
background-image: none !important;
}
.infobuttcontainer {
position: fixed;
top: 20px;
right: 20px;
z-index: 110;
}
.infobutt {
outline: none;
background-color: #1e88e5;
color: #fff;
border: none;
height: 50px;
width: 50px;
border-radius: 50%;
font-size: 30px;
cursor: pointer;
position: relative;
}
.infobutt:before {
content: '';
height: 54px;
width: 54px;
border-radius: 50%;
border: 3px solid #1e88e5;
display: block;
position: absolute;
top: -5px;
left: -5px;
}
#gencode {
margin-top: 60px;
width: 300px;
height: 50px;
border-radius: 4px;
border: 2px solid rgb(82, 82, 82);
background-color: #fff;
color: #000;
cursor: pointer;
box-shadow: -5px 6px 0 0 #919191;
transition: all 80ms;
}
#gencode:active {
transform: translate(-5px, 6px);
transition: all 80ms;
box-shadow: 0 0 0 0 #919191;
}
// variable definitions
let colorButtons = document.querySelectorAll(".spaceButt");
let currentColor = "transparent";
let pixelboard = document.querySelector(".pixelboard");
let columninput = document.getElementById("columninput");
let rowinput = document.getElementById("rowinput");
let gridunitsize = document.getElementById("gridunitsize");
let imgbgtoggle = document.getElementById("imgbgtoggle");
let mouseDown = false;
let buttons = document.querySelectorAll(".colorbutton button");
let textinputs = document.querySelectorAll('.colorbutton input[type="text"]');
let colorinputs = document.querySelectorAll('.colorbutton input[type="color"]');
let infobutt = document.querySelector(".infobutt");
let disclaimer = document.querySelector(".disclaimer");
//copy helper function
let createCopy = function(textToCopy, callback = null) {
//create the readonly textarea with the text in it and hide it
let tarea = document.createElement("textarea");
tarea.setAttribute("id", "copyarea");
tarea.setAttribute("readonly", "readonly");
tarea.setAttribute(
"style",
"opacity: 0; position: absolute; z-index: -1; top: 0; left: -9999px;"
);
tarea.appendChild(document.createTextNode(textToCopy));
document.body.appendChild(tarea);
//select and copy the text in the readonly text area
tarea.select();
document.execCommand("copy");
//remove the element from the DOM
document.body.removeChild(tarea);
//fire callback function if provided
if (typeof callback === "function" && callback()) {
callback();
}
};
//recognize when mouse is pressed
document.body.onmousedown = function() {
mouseDown = true;
};
document.body.onmouseup = function() {
mouseDown = false;
};
// button pressing and paintbrush loading
for (let index = 0; index < colorButtons.length; index++) {
const element = colorButtons[index];
element.addEventListener("click", function() {
let pressedButt = document.querySelector(".pressed");
if (pressedButt) {
pressedButt.classList.remove("pressed");
}
if (this == pressedButt) {
currentColor = "transparent";
} else {
this.classList.add("pressed");
currentColor = this.dataset.color;
}
});
}
//event listeners on grid to allow paintbrush to paint
let wireUpSquares = function() {
let gridSquares = document.querySelectorAll(".pixelboard div");
for (let index = 0; index < gridSquares.length; index++) {
const element = gridSquares[index];
element.addEventListener("mousedown", function(e) {
this.style.backgroundColor = currentColor;
e.preventDefault();
});
element.addEventListener("mouseenter", function(e) {
if (mouseDown) {
this.style.backgroundColor = currentColor;
}
e.preventDefault();
});
}
};
function isEven(n) {
return n % 2 == 0;
}
//generate the one div style block
let generateCode = function() {
let widthSq = parseInt(columninput.value);
let heightSq = parseInt(rowinput.value);
let sqMeasW = isEven(widthSq) ? widthSq / 2 : (widthSq - 1) / 2;
let sqMeasH = isEven(heightSq) ? heightSq / 2 : (heightSq - 1) / 2;
let sqPadW = isEven(widthSq) ? sqMeasW - 1 : sqMeasW;
let sqPadH = isEven(heightSq) ? sqMeasH - 1 : sqMeasH;
let backString = "";
let hOff = -(sqPadW + 1);
let vOff = -(sqMeasH + 1);
let styleString = `content: '';
display: block;
font-size: ${gridunitsize.value}px;
width: 1em;
height: 1em;
margin: ${sqMeasH}em ${sqMeasW}em ${sqPadH}em ${sqPadW}em;
box-shadow:`;
for (let index = 0; index < widthSq * heightSq; index++) {
let dBG = document.querySelector(
".pixelboard div:nth-child(" + (index + 2) + ")"
).style.backgroundColor;
sqColor = dBG ? dBG : "transparent";
let row = Math.ceil((index + 1) / widthSq);
let col = ((index + 1) / widthSq - (row - 1)) / (1 / widthSq);
let vEm = Math.round(row + vOff);
let hEm = Math.round(col + hOff);
if (vEm === 0 && hEm === 0) {
backString = `background-color: ${sqColor};`;
} else if (sqColor !== "transparent") {
styleString += `${hEm}em ${vEm}em 0 0 ${sqColor},`;
}
}
var finalStyle = backString + styleString.replace(/.$/, ";");
document.getElementById("space-invader").setAttribute("style", finalStyle);
pixelboard.style.display = "none";
document.querySelector(".pcard").style.boxShadow = "none";
document.querySelector(".pcard").style.borderRadius = "0px";
let knobs = document.querySelectorAll(".knob");
for (let index = 0; index < knobs.length; index++) {
const element = knobs[index];
element.style.display = "none";
}
createCopy(finalStyle, function() {
alert(
"css has been copied to your clipboard... go apply it to an empty div :)"
);
});
};
//file input wiring
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
pixelboard.style.backgroundImage = "url('" + e.target.result + "')";
imgbgtoggle.disabled = false;
imgbgtoggle.checked = true;
pixelboard.classList.remove("hide0");
};
reader.readAsDataURL(input.files[0]);
}
}
//mouse events for drag and drop image BG for grid
let turnDropOff = function() {
document.querySelector(".file-upload-input").style.display = "none";
};
pixelboard.addEventListener("dragenter", function(e) {
document.querySelector(".file-upload-input").style.display = "inherit";
});
pixelboard.addEventListener("drop", function(e) {
turnDropOff();
});
document.body.addEventListener("mouseleave", function(e) {
turnDropOff();
});
//grid creation function
let createGrid = function(row, col, unitsize) {
console.log("firing");
let currentG = document.querySelectorAll(".pixelboard div");
for (let index = 0; index < currentG.length; index++) {
const element = currentG[index];
element.parentNode.removeChild(element);
}
for (let index = 0; index < row * col; index++) {
var emptyDiv = document.createElement("div");
pixelboard.appendChild(emptyDiv);
}
pixelboard.style.gridTemplateColumns = `repeat(${col}, ${unitsize}px)`;
pixelboard.style.gridTemplateRows = `repeat(${row}, ${unitsize}px)`;
wireUpSquares();
};
//listners on the grid adjustment inputs to recreate the grid
columninput.addEventListener("input", function() {
createGrid(rowinput.value, columninput.value, gridunitsize.value);
});
rowinput.addEventListener("input", function() {
createGrid(rowinput.value, columninput.value, gridunitsize.value);
});
gridunitsize.addEventListener("input", function() {
createGrid(rowinput.value, columninput.value, gridunitsize.value);
});
//create initial grid on document load
document.onload = createGrid(
rowinput.value,
columninput.value,
gridunitsize.value
);
//helper function to change color of button
let changeColorButton = function(number, color) {
let butt = document.getElementById("button" + number);
butt.setAttribute("data-color", color);
butt.setAttribute(
"style",
`background-color: ${color}; box-shadow: -5px 4px 0px ${color}, -6px 5px 0px #000;`
);
};
//helper function to change color of text input
let changeColorText = function(number, color) {
let text = document.getElementById("textinput" + number);
text.value = color;
};
//helper function to change color of color input
let changeColorPicker = function(number, color) {
let picker = document.getElementById("colorinput" + number);
picker.value = color;
};
//event listeners for color inputs to change color of corresponding text input and button
for (let index = 0; index < colorinputs.length; index++) {
const element = colorinputs[index];
element.addEventListener("change", function() {
changeColorButton(index + 1, this.value);
changeColorText(index + 1, this.value);
currentColor = document.querySelector(".pressed").dataset.color;
});
}
//event listeners for text inputs to change color of corresponding color input and button
for (let index = 0; index < textinputs.length; index++) {
const element = textinputs[index];
element.addEventListener("change", function() {
changeColorButton(index + 1, this.value);
changeColorPicker(index + 1, this.value);
currentColor = document.querySelector(".pressed").dataset.color;
});
}
//toggle the pixelboard background based on the imgbg checkbox input
imgbgtoggle.addEventListener("change", function() {
pixelboard.classList.toggle("hide0");
});
//add event listener to #gencode div to generate the css and to show the user the final single-div art!
document.getElementById("gencode").addEventListener("click", function() {
generateCode();
});
//show info about pen on infobutt click
infobutt.addEventListener("click", function() {
disclaimer.classList.toggle("hide1");
});
disclaimer.addEventListener("click", function() {
disclaimer.classList.toggle("hide1");
});
Also see: Tab Triggers