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="post">
<img src="https://unsplash.it/500/300/?random">
<p>Pellentesque finibus fringilla velit, quis blandit dui blandit feugiat. Nam viverra arcu vitae suscipit commodo. Sed id tincidunt nibh, sit amet efficitur lorem. In in enim nec turpis sollicitudin vulputate id viverra augue. Praesent auctor, eros a elementum vestibulum, nunc sapien euismod quam, et pulvinar tellus orci ut odio. Ut tristique tortor vitae vehicula scelerisque. Sed pulvinar eu turpis vitae dapibus. Quisque eget nisl pulvinar, tincidunt ante ut, sollicitudin arcu. Cras id semper lacus, vitae ornare ex. Morbi blandit molestie justo, vel accumsan felis auctor nec. Etiam ut iaculis nibh, quis ullamcorper elit. Mauris in commodo velit. Suspendisse hendrerit suscipit sapien, non sodales urna placerat eget. Nullam elit nibh, tempor et elit et, tincidunt rhoncus arcu. Phasellus vitae varius leo. Aliquam gravida velit tellus, non lacinia nulla porttitor non.</p>
<img src="https://unsplash.it/1200/600/?random">
<p>Proin laoreet tristique elit, nec euismod purus imperdiet ut. Nunc venenatis, leo ut tincidunt sodales, lorem sapien lobortis diam, ac scelerisque metus ex in augue. Sed varius posuere diam, nec blandit augue ullamcorper sed. Donec vehicula diam sapien, vitae maximus mi placerat ut. Vivamus nunc nibh, faucibus at odio eu, efficitur euismod eros. In at tempus lacus, vel ultricies enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla posuere sapien eget vestibulum fermentum.</p>
<img src="https://unsplash.it/1200/800/?random">
<p>Donec mollis sapien eu dignissim sagittis. Sed eu diam eu ex pellentesque euismod. Vestibulum pellentesque pretium nulla, id elementum nunc placerat at. Vestibulum faucibus tristique nibh vel fermentum. In iaculis laoreet eros. Nam fermentum dignissim aliquet. Quisque finibus tristique ex id aliquet. Etiam lorem dolor, mattis vitae iaculis sit amet, eleifend suscipit lectus. Fusce congue ultricies dui tempor ultricies.</p>
<img src="https://unsplash.it/1200/700/?random">
<p>O, jak raz zawitała moda francuszczyzny! gdy ów Wespazyjanus nie poruczy, bo tak na oknach donice z cudzych krajó wtargnęli do nas Proin laoreet tristique elit, nec euismod purus imperdiet ut. Nunc venenatis, leo ut tincidunt sodales, lorem sapien lobortis diam, ac scelerisque metus ex in augue. Sed varius posuere diam, nec blandit augue ullamcorper sed. Donec vehicula diam sapien, vitae maximus mi placerat ut. Vivamus nunc nibh, faucibus at odio eu, efficitur euismod eros. In at tempus lacus, vel ultricies enim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla posuere sapien eget vestibulum fermentum.</p>
<p>Pellentesque finibus fringilla velit, quis blandit dui blandit feugiat. Nam viverra arcu vitae suscipit commodo. Sed id tincidunt nibh, sit amet efficitur lorem. In in enim nec turpis sollicitudin vulputate id viverra augue. Praesent auctor, eros a elementum vestibulum, nunc sapien euismod quam, et pulvinar tellus orci ut odio. Ut tristique tortor vitae vehicula scelerisque. Sed pulvinar eu turpis vitae dapibus. Quisque eget nisl pulvinar, tincidunt ante ut, sollicitudin arcu. Cras id semper lacus, vitae ornare ex. Morbi blandit molestie justo, vel accumsan felis auctor nec. Etiam ut iaculis nibh, quis ullamcorper elit. Mauris in commodo velit. Suspendisse hendrerit suscipit sapien, non sodales urna placerat eget. Nullam elit nibh, tempor et elit et, tincidunt rhoncus arcu. Phasellus vitae varius leo. Aliquam gravida velit tellus, non lacinia nulla porttitor non.</p>
<img src="https://unsplash.it/1200/900/?random">
</div>
.post {
font-family: 'Lato', sans-serif;
font-size: 32px;
min-height: 100%;
width: 100%;
padding: 20px 100px;
box-sizing: border-box;
display: block;
margin: 0 auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.post h1 {
text-align: center;
}
.post p {
font-size: 18px;
font-weight: 400;
width: 80%;
margin: 40px auto;
line-height: 44px;
}
.post img {
max-height: 100%;
max-width: 100%;
display: block;
margin: 0 auto;
cursor: zoom-in;
}
[data-icon]:before {
font-family: "icons" !important;
content: attr(data-icon);
font-style: normal !important;
font-weight: normal !important;
font-variant: normal !important;
text-transform: none !important;
speak: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
[class^="icon-"]:before,
[class*=" icon-"]:before {
font-family: "icons" !important;
font-style: normal !important;
font-weight: normal !important;
font-variant: normal !important;
text-transform: none !important;
speak: none;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.icon-arrow-right:before {
content: ">";
}
.icon-arrow-left:before {
content: "<";
}
.icon-close:before {
content: "X";
}
.flatbox-container {
width: 100%;
height: 100%;
display: table;
position: fixed;
top: 0;
left: 0;
}
.flatbox-container.hidden {
display: none;
}
.flatbox-overlay {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
cursor: zoom-out;
background-color: rgba(0, 0, 0, 0.9);
}
.flatbox-slide {
max-height: 85%;
max-width: 80%;
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
opacity: 0;
visibility: hidden;
cursor: default;
transition: visibility 0s linear .3s, opacity .3s, transform .3s;
}
.flatbox-prev-slide {
z-index: 10;
transform: translateX(-10%);
}
.flatbox-current-slide {
z-index: 20;
opacity: 1;
visibility: visible;
transition: visibility 0s linear 0s, opacity .3s, transform .3s;
}
.flatbox-next-slide {
z-index: 30;
transform: translateX(10%);
}
.flatbox-arrow {
font-size: 24px;
width: 30px;
height: 30px;
display: table-cell;
vertical-align: middle;
position: fixed;
top: calc(50% - 15px);
line-height: 35px;
color: #afafaf;
cursor: pointer;
z-index: 10;
transition: color .3s;
}
.flatbox-arrow:hover {
color: #ffffff;
}
.flatbox-prev {
left: 0;
}
.flatbox-next {
right: 0;
}
.flatbox-close {
width: 12px;
height: 30px;
font-size: 18px;
display: inline-block;
position: fixed;
line-height: 30px;
color: #afafaf;
z-index: 10;
cursor: pointer;
text-align: center;
transition: color .3s;
}
.flatbox-close:hover {
color: #ffffff;
}
@media screen and (max-width: 600px) {
.flatbox-arrow {
font-size: 18px;
}
}
var FlatBox = (function() {
'use strict';
var FlatBox = function(selector, options) {
this.selector = selector;
this.changeSlideSpeed = options.changeSlideSpeed;
this.init();
}
FlatBox.prototype.init = function() {
var rootElement = this.getRootElement(this.selector);
this.images = this.getImages(rootElement);
this.currentSlideIndex = 0;
this.checkKey = this.checkKey.bind(this);
this.createLightboxElements();
this.prepareLightboxElements();
this.appendLightbox();
this.hide();
this.prepareImages();
window.onresize = this.windowResize.bind(this);
}
FlatBox.prototype.setNextSlide = function() {
var tempPrevSlideElement = this.prevSlideElement,
tempCurrentSlideElement = this.currentSlideElement,
tempNextSlideElement = this.nextSlideElement;
this.currentSlideIndex = this.getNextSlideIndex();
this.setSlidesIndexes();
var nextSlideIndex = this.getNextSlideIndex();
tempPrevSlideElement.src = this.getImage(nextSlideIndex).src;
this.addClass(tempPrevSlideElement, 'flatbox-next-slide flatbox-slide');
this.addClass(tempCurrentSlideElement, 'flatbox-prev-slide flatbox-slide');
this.addClass(tempNextSlideElement, 'flatbox-current-slide flatbox-slide');
this.prevSlideElement = tempCurrentSlideElement;
this.currentSlideElement = tempNextSlideElement;
this.nextSlideElement = tempPrevSlideElement;
this.setClosePosition();
this.setArrowsPosition();
}
FlatBox.prototype.setPrevSlide = function() {
var tempPrevSlideElement = this.prevSlideElement,
tempCurrentSlideElement = this.currentSlideElement,
tempNextSlideElement = this.nextSlideElement;
this.currentSlideIndex = this.getPrevSlideIndex();
this.setSlidesIndexes();
var prevSlideIndex = this.getPrevSlideIndex();
tempNextSlideElement.src = this.getImage(prevSlideIndex).src;
this.addClass(tempPrevSlideElement, 'flatbox-current-slide flatbox-slide');
this.addClass(tempCurrentSlideElement, 'flatbox-next-slide flatbox-slide');
this.addClass(tempNextSlideElement, 'flatbox-prev-slide flatbox-slide');
this.prevSlideElement = tempNextSlideElement;
this.currentSlideElement = tempPrevSlideElement;
this.nextSlideElement = tempCurrentSlideElement;
this.setClosePosition();
this.setArrowsPosition();
}
FlatBox.prototype.getImage = function(index) {
return this.images[index];
}
FlatBox.prototype.createLightboxElements = function() {
this.lightboxContainerElement = this.createElement('div');
this.currentSlideElement = this.createElement('img');
this.nextSlideElement = this.createElement('img');
this.prevSlideElement = this.createElement('img');
this.backgroundOverlayElement = this.createElement('div');
this.closeButtonElement = this.createElement('i');
this.nextArrowElement = this.createElement('i');
this.prevArrowElement = this.createElement('i');
this.appendChilds(this.lightboxContainerElement,
[
this.prevSlideElement,
this.currentSlideElement,
this.nextSlideElement,
this.closeButtonElement,
this.prevArrowElement,
this.nextArrowElement,
this.backgroundOverlayElement
]);
}
FlatBox.prototype.prepareLightboxElements = function() {
this.addClass(this.lightboxContainerElement, 'flatbox-container');
this.addClass(this.prevSlideElement, 'flatbox-prev-slide flatbox-slide');
this.addClass(this.currentSlideElement, 'flatbox-current-slide flatbox-slide');
this.addClass(this.nextSlideElement, 'flatbox-next-slide flatbox-slide');
this.addClass(this.backgroundOverlayElement, 'flatbox-overlay');
this.addClass(this.closeButtonElement, 'flatbox-close icon-close');
this.addClass(this.nextArrowElement, 'flatbox-next flatbox-arrow icon-arrow-right');
this.addClass(this.prevArrowElement, 'flatbox-prev flatbox-arrow icon-arrow-left');
this.bindClick(this.prevArrowElement, this.setPrevSlide);
this.bindClick(this.nextArrowElement, this.setNextSlide);
this.bindClick(this.backgroundOverlayElement, this.hide);
this.bindClick(this.closeButtonElement, this.hide);
}
FlatBox.prototype.appendLightbox = function() {
this.appendToBody(this.lightboxContainerElement);
}
FlatBox.prototype.prepareImages = function() {
var length = this.images.length;
for(var i = 0; i < length; i++) {
var image = this.images[i];
image.setAttribute('data-flatbox', i);
this.bindClick(image, this.imageClick);
}
}
FlatBox.prototype.imageClick = function(event) {
var image = event.target;
this.currentSlideIndex = parseInt(image.getAttribute('data-flatbox'), 10);
this.setSlidesSources();
this.show();
}
FlatBox.prototype.setSlidesSources = function() {
var prevSlideIndex = this.getPrevSlideIndex();
var nextSlideIndex = this.getNextSlideIndex();
this.addEventListenerOnce(this.currentSlideElement, 'load', function () {
this.prevSlideElement.src = this.getImage(prevSlideIndex).src;
this.nextSlideElement.src = this.getImage(nextSlideIndex).src;
this.setSlidesIndexes();
this.setClosePosition();
this.setArrowsPosition();
}.bind(this));
this.currentSlideElement.src = this.getImage(this.currentSlideIndex).src;
}
FlatBox.prototype.setSlidesIndexes = function() {
var prevSlideIndex = this.getPrevSlideIndex();
var nextSlideIndex = this.getNextSlideIndex();
this.prevSlideElement.setAttribute('data-flatbox', prevSlideIndex);
this.currentSlideElement.setAttribute('data-flatbox', this.currentSlideIndex);
this.nextSlideElement.setAttribute('data-flatbox', nextSlideIndex);
}
FlatBox.prototype.getNextSlideIndex = function() {
if (this.isLastSlide(this.currentSlideIndex)) {
return 0;
}
return this.currentSlideIndex + 1;
}
FlatBox.prototype.getPrevSlideIndex = function() {
if (this.isFirstSlide(this.currentSlideIndex)) {
return this.images.length - 1;
}
return this.currentSlideIndex - 1;
}
FlatBox.prototype.isLastSlide = function(slide) {
return slide === this.images.length - 1;
}
FlatBox.prototype.isFirstSlide = function(slide) {
return slide === 0;
}
FlatBox.prototype.getImages = function(rootElement) {
return rootElement.getElementsByTagName('img');
}
FlatBox.prototype.getRootElement = function(selector) {
var element = document.querySelectorAll(selector);
if (element.length > 1) throw 'Use an unique selector.';
return element[0];
}
FlatBox.prototype.createElement = function(tagName) {
return document.createElement(tagName);
}
FlatBox.prototype.appendChilds = function(parent, childs) {
var length = childs.length;
for(var i = 0; i < length; i++) {
parent.appendChild(childs[i]);
}
}
FlatBox.prototype.appendToBody = function(element) {
document.body.appendChild(element);
}
FlatBox.prototype.bindKeydownOnce = function() {
document.addEventListener('keydown', this.checkKey);
}
FlatBox.prototype.unbindKeydown = function() {
document.removeEventListener('keydown', this.checkKey);
}
FlatBox.prototype.windowResize = function() {
this.setClosePosition();
this.setArrowsPosition();
}
FlatBox.prototype.checkKey = function(e) {
document.removeEventListener('keydown', this.checkKey);
var e = e || window.event;
switch(e.keyCode) {
case 27:
this.hide();
break;
case 37:
this.setPrevSlide();
break;
case 39:
this.setNextSlide();
break;
}
setTimeout(function() {
this.bindKeydownOnce();
}.bind(this), this.changeSlideSpeed);
}
FlatBox.prototype.setClosePosition = function() {
var currentSlide = this.getCurrentSlide();
var slideProperties = this.getSlideProperties(currentSlide);
var windowProperties = this.getWindowProperties();
this.closeButtonElement.style.bottom = ((windowProperties.height + slideProperties.height) / 2) + 'px'
this.closeButtonElement.style.right = ((windowProperties.width - slideProperties.width) / 2) + 'px'
}
FlatBox.prototype.setArrowsPosition = function() {
var currentSlide = this.getCurrentSlide();
var slideProperties = this.getSlideProperties(currentSlide);
var windowProperties = this.getWindowProperties();
var leftArrowWidth = this.prevArrowElement.getBoundingClientRect().width;
var rightArrowWidth = this.nextArrowElement.getBoundingClientRect().width;
this.prevArrowElement.style.left = ((((windowProperties.width - slideProperties.width) / 2) - leftArrowWidth) / 2) + 'px'
this.nextArrowElement.style.right = ((((windowProperties.width - slideProperties.width) / 2) - rightArrowWidth) / 2) + 'px'
}
FlatBox.prototype.getSlideProperties = function(slide) {
return {
width: slide.getBoundingClientRect().width,
height: slide.getBoundingClientRect().height,
src: slide.src
}
}
FlatBox.prototype.getWindowProperties = function() {
return {
width: window.innerWidth,
height: window.innerHeight
}
}
FlatBox.prototype.getCurrentSlide = function() {
return document.querySelector('.flatbox-current-slide');
}
FlatBox.prototype.bindClick = function(target, listener) {
target.addEventListener('click', listener.bind(this));
}
FlatBox.prototype.addClass = function(element, className) {
element.className = className;
}
FlatBox.prototype.insertHTML = function(element, text) {
element.innerHTML = text;
}
FlatBox.prototype.addEventListenerOnce = function(target, type, listener) {
target.addEventListener(type, function fn(event) {
target.removeEventListener(type, fn);
listener(event);
});
}
FlatBox.prototype.hide = function() {
this.addClass(this.lightboxContainerElement, 'flatbox-container hidden');
this.unbindKeydown();
}
FlatBox.prototype.show = function() {
this.addClass(this.lightboxContainerElement, 'flatbox-container');
this.bindKeydownOnce();
}
return FlatBox;
}());
/*init*/
var FlatBox = new FlatBox('.post', {
changeSlideSpeed: 200
});
Also see: Tab Triggers