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.
<main>
<section id="panel">
<header>Percentages</header>
<form>
<label>
Snap X
<input id="snap-x" type="number" value="10" min="5" max="50" step="0.1" autofocus />
</label>
<label>
Snap Y
<input id="snap-y" type="number" value="10" min="5" max="50" step="0.1" />
</label>
<label>
Marker Width
<input id="marker-width" type="number" value="20" min="1" max="100" step="0.1" />
</label>
<label>
Marker Height
<input id="marker-height" type="number" value="20" min="1" max="100" step="0.1" />
</label>
<label>
Board Size
<input id="board-size" type="number" value="80" min="1" max="100" step="0.1" />
</label>
<input type="submit" value="Apply" />
</form>
</section>
<section id="board">
<div id="marker"></div>
</section>
</main>
html, body {
height: 100%;
margin: 0;
}
body {
background-color: #ddd;
overflow: hidden;
}
main {
background: #ddd;
position: relative;
height: 100vh;
}
#board {
position: relative;
top: 50%;
left: 50%;
z-index: 10;
background: #d7d7d7;
}
#marker {
position: absolute;
background: rgba(128, 0, 128, 0.7);
outline: 1px solid transparent;
top: 0;
left: 0;
z-index: 1000;
}
.cell {
background: #aeaeae;
position: absolute;
top: 0;
left: 0;
}
#panel {
position: absolute;
padding: 10px;
z-index: 20;
background: #ddd;
border: 1px solid #ccc;
header {
font-weight: bold;
font-size: 14px;
}
}
input {
display: block;
}
label {
display: block;
font-size: 12px;
margin: 6px 0;
}
[type="number"] {
width: 100px;
margin-top: 4px;
}
[type="submit"] {
background: #ddd;
box-shadow: none;
outline: none;
font-size: 12px;
padding: 4px 8px;
border: 1px solid #aaa;
margin-top: 8px;
&:focus {
outline: none;
}
}
var board, marker;
var input = {
width : $("#marker-width"),
height : $("#marker-height"),
snapX : $("#snap-x"),
snapY : $("#snap-y"),
size : $("#board-size")
};
$("form").submit(init);
// BOARD ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
board = {
_bounds : null,
size : null,
target : $("#board"),
get bounds() { return this._bounds || this.target[0].getBoundingClientRect(); },
set bounds(bounds) { this._bounds = bounds; },
get cols() { return Math.floor(100 / marker._snapX); },
get rows() { return Math.floor(100 / marker._snapY); },
get width() { return this.bounds.width; },
get height() { return this.bounds.height; }
}
// MARKER :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
marker = {
_snapX : null,
_snapY : null,
target : $("#marker"),
dragger : createDraggable("#marker"),
width : null,
height : null,
xPercent : 0,
yPercent : 0,
get snapX() { return this._snapX + 100 % this._snapX / board.cols; },
set snapX(snapX) { this._snapX = snapX; },
get snapY() { return this._snapY + 100 % this._snapY / board.rows; },
set snapY(snapY) { this._snapY = snapY; },
get x() { return this.dragger.x; },
get y() { return this.dragger.y; }
};
init();
// DRAGGABLE ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createDraggable(target) {
return new Draggable(target, {
type : "top,left",
onPress : onPress,
onDrag : onDrag,
onRelease : onRelease
});
}
function onPress() {
// this will cause it to recalculate
board.bounds = null;
setAbsolute();
this.update();
}
function onRelease() {
board.bounds = null;
this.update();
setRelative();
}
function onDrag() {
// snap the marker when it's on the board
if (this.hitTest(board.target)) {
// calculate position as a percent
var xPercent = this.x / board.width * 100;
var yPercent = this.y / board.height * 100;
// calculate snap as a percent and store the value
marker.xPercent = Math.round(xPercent / marker.snapX) * marker.snapX;
marker.yPercent = Math.round(yPercent / marker.snapY) * marker.snapY;
// set snap value in px
TweenLite.set(this.target, {
left : board.width * marker.xPercent / 100,
top : board.height * marker.yPercent / 100
});
} else {
marker.xPercent = 0;
marker.yPercent = 0;
}
}
// POSITIONING ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function setAbsolute() {
// calculate px value
var left = marker.xPercent * board.width / 100;
var top = marker.yPercent * board.height / 100;
TweenLite.set(board.target, { zIndex: 30 });
TweenLite.set(marker.target, { left: left, top: top });
}
function setRelative() {
// if the percent isn't set, calculate it
marker.xPercent = marker.xPercent || marker.x / board.width * 100;
marker.yPercent = marker.yPercent || marker.y / board.height * 100;
TweenLite.set(board.target, { zIndex: 10 });
TweenLite.set(marker.target, {
left : marker.xPercent + "%",
top : marker.yPercent + "%"
});
}
// SIZING :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function updateSize() {
var winWidth = window.innerWidth;
var winHeight = window.innerHeight;
var landscape = (winWidth > winHeight);
var min = Math.min(winWidth, winHeight);
var max = Math.max(winWidth, winHeight);
var size = board.size * min / 100;
TweenLite.set(board.target, {
xPercent : -50,
yPercent : -50,
width : size / (landscape ? max : min) * 100 + "%",
paddingTop : size / (landscape ? max : min) * 100 + "%"
});
TweenLite.set(marker.target, {
width : marker.width + "%",
height : marker.height + "%"
});
}
// GRID :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function createGrid() {
// remove previous cells
$(".cell").remove();
for (var row = 0; row < board.rows; row++) {
for (var col = 0; col < board.cols; col++) {
// skip every other one to create a checkerboard pattern
if (((row % 2) && !(col % 2)) || (!(row % 2) && (col % 2))) continue;
var target = $("<div class='cell' />").appendTo(board.target);
TweenLite.set(target, {
width : marker.snapX + "%",
height : marker.snapY + "%",
left : col * marker.snapX + "%",
top : row * marker.snapY + "%"
});
}
}
}
// INIT :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
function init(event) {
// prevent when called by submit
event && event.preventDefault();
// cast input values to numbers
board.size = +input.size.val();
marker.snapX = +input.snapX.val();
marker.snapY = +input.snapY.val();
marker.width = +input.width.val();
marker.height = +input.height.val();
createGrid();
updateSize();
// snap the marker
if (Draggable.hitTest(marker.target, board.target)) {
onDrag.call(marker.dragger);
onRelease.call(marker.dragger);
}
}
Also see: Tab Triggers