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.
<!-- app -->
<main class="app">
<!-- header -->
<header class="header">
<!-- left -->
<div class="left">
<a href="#" id="run">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-play" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M10.804 8L5 4.633v6.734L10.804 8zm.792-.696a.802.802 0 0 1 0 1.392l-6.363 3.692C4.713 12.69 4 12.345 4 11.692V4.308c0-.653.713-.998 1.233-.696l6.363 3.692z" />
</svg>
</a>
<a href="#" id="clear">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-trash2-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M2.037 3.225l1.684 10.104A2 2 0 0 0 5.694 15h4.612a2 2 0 0 0 1.973-1.671l1.684-10.104C13.627 4.224 11.085 5 8 5c-3.086 0-5.627-.776-5.963-1.775z" />
<path fill-rule="evenodd" d="M12.9 3c-.18-.14-.497-.307-.974-.466C10.967 2.214 9.58 2 8 2s-2.968.215-3.926.534c-.477.16-.795.327-.975.466.18.14.498.307.975.466C5.032 3.786 6.42 4 8 4s2.967-.215 3.926-.534c.477-.16.795-.327.975-.466zM8 5c3.314 0 6-.895 6-2s-2.686-2-6-2-6 .895-6 2 2.686 2 6 2z" />
</svg>
</a>
<a href="#" id="top">
<svg width="1em" height="1em" viewBox="0 0 16 16" class="bi bi-check-square" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" d="M14 1H2a1 1 0 0 0-1 1v12a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V2a1 1 0 0 0-1-1zM2 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H2z" />
<path fill-rule="evenodd" d="M10.97 4.97a.75.75 0 0 1 1.071 1.05l-3.992 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.236.236 0 0 1 .02-.022z" />
</svg>
</a>
</div>
<!-- / left -->
<!-- center -->
<div id="info" class="center"></div>
<!-- / center -->
<!-- right -->
<div class="right"></div>
<!-- / right -->
</header>
<!-- / header -->
<!-- content -->
<section class="content">
<!-- editor -->
<div id="editor" spellcheck="false"></div>
<!-- / editor -->
<!-- log -->
<div id="log"></div>
<!-- / log -->
</section>
<!-- / content -->
</main>
$background: #3f51b5;
$color: #e8ebff;
$string: #00c4ff;
$number: #f44336;
$boolean: #8bc34a;
$object: #ffc107;
$undefined: #e91e63;
%equal-heights {
flex-wrap: wrap;
float: left;
height: 100%;
width: calc(100% / 2);
}
%fonts {
font-size: 12px;
font-weight: normal;
font-family: "Consolas", monospace;
line-height: 1.3;
letter-spacing: 1.4px;
}
%scrollbar {
overflow-y: auto;
&::-webkit-scrollbar {
width: 10px;
background: darken($background, 20%);
}
&::-webkit-scrollbar-corner {
background: darken($background, 20%);
}
&::-webkit-scrollbar-thumb {
background: darken($background, 10%);
}
}
/** Base typography
-------------------- */
*,
*:before,
*:after {
box-sizing: inherit;
}
html {
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}
* {
box-sizing: border-box;
}
html {
box-sizing: border-box;
font-size: 85%;
font-family: serif;
}
html,
body {
height: 100%;
position: relative;
}
/** body
-------------------- */
body {
margin: 0;
padding: 0;
font-size: 100%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 14px;
font-style: normal;
font-weight: 400;
line-height: 14px;
}
/** App
--------------------------*/
body {
overflow: hidden;
background: darken($background, 30%);
}
.app {
padding: 0;
height: 100%;
position: relative;
overflow: hidden;
background: darken($background, 40%);
box-shadow: 0px 3px 3px darken($background, 40%);
border-radius: 4px;
border: 2px solid darken($background, 20%);
width: 960px;
max-width: calc(100% - 3rem);
max-height: 480px;
margin: 3rem auto;
@media (max-width: 760px) {
box-shadow: none;
border-radius: 0;
border: none;
max-width: 100%;
max-height: 100%;
margin: 0;
}
}
.header {
height: 2.6rem;
background: darken($background, 20%);
overflow: hidden;
font-size: 14px;
font-weight: 700;
user-select: none;
.left {
float: left;
width: 7rem;
}
.right {
float: right;
width: 7em;
text-align: right;
}
.center {
width: calc(100% - 20rem);
height: 2.6rem;
display: block;
padding: 0.2em;
float: left;
span {
display: block;
text-align: center;
margin: 0.4rem;
text-transform: capitalize;
color: $boolean;
animation: fade 1s 1 ease;
@keyframes fade {
from {
opacity: 0;
}
}
}
}
a {
display: inline-block;
text-decoration: none;
transition: background 500ms ease;
margin: 0.2rem 0;
padding: 0.5rem 1rem;
color: $color;
&:first-child {
margin-left: 0.2rem;
}
svg {
stroke: darken($color, 10%);
stroke-width: 1px;
}
&:hover,
&:focus {
transition: background 500ms ease;
background: darken($background, 12%);
}
}
}
.content {
display: flex;
overflow: hidden;
height: calc(100% - 2.6rem);
#log {
margin: 0;
@extend %equal-heights;
word-break: break-word;
white-space: pre-wrap;
@extend %fonts;
@extend %scrollbar;
padding: 0.5rem;
background: darken($background, 35%);
color: $color;
a{
color: darken($color,10%);
}
.log-string {
color: $string;
}
.log-number {
color: $number;
}
.log-boolean {
color: $boolean;
}
.log-object {
color: $object;
}
.log-undefined {
color: $undefined;
font-weight: bold;
}
}
#editor {
@extend %equal-heights;
resize: none;
border: none;
outline: none;
white-space: pre-wrap;
padding: 0;
transition: background 500ms ease;
background: darken($background, 30%);
color: $color;
.CodeMirror {
line-height: 1.5;
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif,
"Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-weight: bolder;
font-size: 13px;
letter-spacing: 1.5px;
transition: background 500ms ease;
background: darken($background, 30%);
color: $color;
.CodeMirror-linenumber {
color: white;
}
.CodeMirror-cursor {
border-left: 1px solid white;
}
.CodeMirror-scroll,
.CodeMirror-vscrollbar,
.CodeMirror-hscrollbar {
@extend %scrollbar;
}
}
.cm-s-material-palenight {
background: darken($background, 30%);
color: $color;
height: 100%;
.CodeMirror-gutter {
background: #171e44;
border-right: 1px solid #111631;
user-select: none;
}
&:hover,
&:focus {
transition: background 500ms ease;
outline: none;
background: darken($background, 32%);
}
}
}
}
.clearing {
animation: clear 800ms ease;
}
@keyframes clear {
from {
background: white;
}
}
.gutter {
background-color: darken($background, 36%);
background-repeat: no-repeat;
background-position: 50%;
cursor: e-resize;
.gutter-vertical {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAFAQMAAABo7865AAAABlBMVEVHcEzMzMzyAv2sAAAAAXRSTlMAQObYZgAAABBJREFUeF5jOAMEEAIEEFwAn3kMwcB6I2AAAAAASUVORK5CYII=");
}
.gutter-horizontal {
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAeCAYAAADkftS9AAAAIklEQVQoU2M4c+bMfxAGAgYYmwGrIIiDjrELjpo5aiZeMwF+yNnOs5KSvgAAAABJRU5ErkJggg==");
}
}
const _ = (el) => document.querySelector(el);
/**
* Storage theme settings
*
* set Storage.val = {color: 'blue'}
* get Storage.val
*/
const Storage = {
get val() {
this.data = window.localStorage.getItem("last-saved");
return this.data;
},
set val(value) {
this.data = value;
window.localStorage.setItem("last-saved", this.data);
}
};
/**
* Replace Console log
*/
const log = console.log;
console.log = function () {
let val = [].slice.call(arguments).reduce(function(prev, arg) {
let output = (typeof arg === "object" ? JSON.stringify(arg) : arg);
if(typeof arg === "object" && typeof JSON === "object") output = `<span class="log-object">${JSON.stringify(arg)}</span>\n`;
else if(typeof arg === "function" && typeof JSON.stringify === "function") output = `<span class="log-function">${JSON.stringify(arg)}</span>\n`;
else output = `<span class="log-${(typeof arg)}">${arg}</span>\n`;
return (prev+' '+output);
}, '');
logger.innerHTML += val;
log(...arguments)
}
// print info cose
const info = `--------------------------------
- Console test [Version 0.0.2] -
- Made with ♥ by @Nakome. -
--------------------------------`;
const editor = document.getElementById("editor");
const logger = document.getElementById("log");
const clear = document.getElementById("clear");
const run = document.getElementById("run");
const infoDiv = document.getElementById("info");
let codeEditor = CodeMirror(editor, {
mode: {
name: "javascript",
json: true
},
lineNumbers: true,
theme: "material-palenight"
});
// first code
let initValue = (Storage.val) ? Storage.val : "let ready = 'Ready'\nready";
codeEditor.setValue(initValue);
// divide
Split(["#editor", "#log"], {
elementStyle: (dimension, size, gutterSize) => ({
"flex-basis": `calc(${size}% - ${gutterSize}px)`
}),
gutterStyle: (dimension, gutterSize) => ({
"flex-basis": `${gutterSize}px`
}),
minSize: [200, 200]
});
window.addEventListener("error", errorInfo);
logger.textContent = info + "\n";
clear.addEventListener("click", clearLog);
run.addEventListener("click", runEval);
notification("Ready");
/**
* Error info
* @param {string} e
*/
function errorInfo(e) {
let msg = "EXCEPTION: ";
msg += e.message + "\n";
msg += e.filename;
msg += e.lineno + ":" + e.colno;
logger.innerHTML += `<div style="margin: 10px 0; display:block; background: #f55; color: white; padding: 4px 10px; border-radius: 4px; font-family: 'Lucida Console', Monaco, monospace, sans-serif; font-size: 80%"><b style="color:#FFEB3B;">Error</b>: ${msg}</div>`;
}
/**
* Clear log
* @param {object} evt
*/
function clearLog(evt) {
evt.preventDefault();
notification("Clear log");
logger.textContent = info + "\n";
logger.className = "clearing";
let w = setTimeout(() => {
logger.className = "";
clearTimeout(w);
}, 800);
}
/**
* Parse code and check type
* @param {string} str
*/
function parseCode(str) {
let code = eval(str);
let output = typeof code === "object" ? JSON.stringify(code) : code;
if (typeof code === "object" && typeof JSON === "object")
output = `<span class="log-object">${JSON.stringify(code)}</span>\n`;
else if (typeof code === "function" && typeof JSON.stringify === "function")
output = `<span class="log-function">${JSON.stringify(code)}</span>\n`;
else output = `<span class="log-${typeof code}">${code}</span>\n`;
return output;
}
/**
* Run code
* @param {object} evt
*/
function runEval(evt) {
if (evt) evt.preventDefault();
notification("Run code");
return render();
}
/**
* Render code
*/
function render() {
let code = editor.querySelector(".CodeMirror-scroll");
code.classList.add("clearing");
let w = setTimeout(() => {
code.classList.remove("clearing");
Storage.val = codeEditor.getValue();
let output = codeEditor.getValue();
logger.innerHTML += parseCode(output);
clearTimeout(w);
}, 800);
}
/**
* Show notifications
* @param {string} txt
*/
function notification(txt) {
infoDiv.innerHTML = `<span>${txt}</span>`;
let w = setTimeout(() => {
infoDiv.innerHTML = `<span>Console test</span>`;
clearTimeout(w);
}, 1000);
}
Also see: Tab Triggers