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.
<body id="body">
</body>
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.ComparisonSlider {
position: relative;
user-select: none;
}
.ComparisonSlider * {
box-sizing: border-box;
}
.ComparisonSlider > div {
position: absolute;
height: 100%;
width: 100%;
z-index: 1;
overflow: hidden;
}
.ComparisonSlider > div > img {
position: relative;
display: block;
height: 100%;
width: auto;
}
/* the slider */
.ComparisonSlider > span {
position: absolute;
top: 0;
left: calc(50% - 30px);
height: 100%;
width: 60px;
cursor: ew-resize;
z-index: 3;
}
document.addEventListener('DOMContentLoaded',run);
function run(){
const container = document.getElementById('body');
const images = [
"https://source.unsplash.com/random/700x500",
"https://source.unsplash.com/random/800x600",
];
const dimensions = {width: 1000, height: 600};
new ComparisonSlider(container, images, dimensions);
}
//the container will be built with absolute pixel dimensions
class ComparisonSlider{
constructor(node,images,dims){
//node = DOM parent node
//images = array of image src paths
//dims = object with width and height props
this._node = node;
this._width = validate(dims.width);
this._height = validate(dims.height);
this._container = null;
this._div1 = null;
this._div2 = null;
this._img1 = null;
this._img2 = null;
this._small = null;
this._slider = null;
this._images = images;
this.build(this.node);
//helper
function validate(prop){
if (Number(prop)){
return Number(prop);
} else {
return 500;
}
}
}
get node(){
return this._node;
}
get images(){
return this._images;
}
get container(){
return this._container;
}
get width(){
return this._width;
}
get height(){
return this._height;
}
get div1(){
return this._div1;
}
get div2(){
return this._div2;
}
get img1(){
return this._img1;
}
get img2(){
return this._img2;
}
get small(){
return this._small;
}
get slider(){
return this._slider;
}
setSmall(node){
this._small = node;
this._width = node.offsetWidth;
}
build(node){
let container = document.createElement('div');
let div1 = document.createElement('div');
let div2 = document.createElement('div');
let img1 = document.createElement('img');
let img2 = document.createElement('img');
//container setup
container.className = "ComparisonSlider";
container.style.width = this.width + 'px';
container.style.height = this.height + 'px';
//hide everything until async load complete
container.style.opacity = 0;
//append
div1.append(img1);
div2.append(img2);
container.append(div1);
container.append(div2);
node.append(container);
//attach to object props
this._container = container;
this._div1 = div1;
this._div2 = div1;
this._img1 = img1;
this._img2 = img2;
//get image paths
let path1 = this.images[0];
let path2 = this.images[1];
let setSmall = this.setSmall.bind(this);
//counter to track when both images sized
let loadCounter = 2;
//when setDims are complete, run buildReady
const buildReady = ()=>{
//show when ready
this.container.style.opacity = 1;
//next script
this.insertSlider();
};
//determine image widths via async
setDims(img1,path1);
setDims(img2,path2);
//helper get and set dims of image elements
function setDims(elem,url){
var img = new Image();
img.onload = function(){
elem.width = this.naturalWidth;
elem.height = this.naturalHeight;
elem.src = url;
loadCounter--;
//after both images dims load -- resize container to smaller width
if (loadCounter < 1){
let w1 = img1.offsetWidth;
let w2 = img2.offsetWidth;
if(w1<w2){
resize(w1,w2,img1,img2);
} else {
resize(w2,w1,img2,img1);
}
buildReady();
//helper resize
function resize(small,big,smallNode,bigNode){
container.style.width = small + 'px';
let dif = big - small;
let offset = dif / 2;
bigNode.style.left = (-1 * offset) + 'px';
setSmall(smallNode);
}
}
}
img.src = url;
}
}
insertSlider(){
//split and format images
let smallDiv = this.small.parentNode;
smallDiv.style.zIndex = 2;
let w = this.small.offsetWidth;
smallDiv.style.width = (w / 2) + 'px';
smallDiv.style.borderRight = "1px solid rgba(0,0,0,0.5)";
//insert slider element
let slider = document.createElement('span');
this.container.append(slider);
this._slider = slider;
this.initializeSlider();
}
initializeSlider(){
let slider = this.slider;
let small = this.small.parentNode;
let fullWidth = this.width;
slider.onmousedown = (e)=>{
e = e || window.event;
e.stopPropagation();
e.preventDefault();
//get cursor x position
var cursor = e.clientX;
//set window mouse events
window.onmousemove = drag;
window.onmouseup = close;
function drag(e){
e = e || window.event;
e.stopPropagation();
e.preventDefault();
let offset = e.clientX - cursor;
cursor = e.clientX;
//deltas -- displacement
let sliderD = slider.offsetLeft + offset;
let smallD = small.offsetWidth + offset;
//new positions after limit check
let sliderPos = limit(sliderD,-30) //-30 offset for UI
let smallPos = limit(smallD,0);
//change styles for new position
slider.style.left = sliderPos + 'px';
small.style.width = smallPos + 'px';
//helper to keep position within bounds
function limit(val,off){
if (val < off){
return off;
} else if (val > fullWidth + off){
return fullWidth + off;
} else {
return val;
}
}
}
function close(){
window.onmousemove = null;
window.onmouseup = null;
}
};
}
}
Also see: Tab Triggers