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.
<!-- Begin div .grid -->
<div id="gallery__grid" class="container gallery__grid" data-element="gallery-item">
<!-- Begin div .row 1 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/nasa/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/erondu/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/people/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 1 -->
<!-- Begin div .row 2 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/lukasbudimaier/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/objects/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/fableandfolk/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 2 -->
<!-- Begin div .row 3 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/mickeyoneil/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/nature/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/joannakosinska/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 3 -->
<!-- Begin div .row 4 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/leadbt/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/technology/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/davideragusa/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 4 -->
<!-- Begin div .row 5 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/erichuang78910/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/buildings/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/elliottengelmann/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 5 -->
<!-- Begin div .row 6 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/nature/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/fritzbielmeier/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/thata7guy/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 6 -->
<!-- Begin div .row 7 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/pjrvs/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/olliepb/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/food/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 7 -->
<!-- Begin div .row 8 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/jonottosson/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/hideobara/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/sylvain_guiheneuc/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 8 -->
<!-- Begin div .row 9 -->
<div class="row">
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/worthyofelegance/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/category/buildings/400x400" alt="gallery thumbnail">
</div>
<div class="col-md-4 gallery__column">
<img class="gallery-item" src="https://source.unsplash.com/user/_vickyreyes/400x400" alt="gallery thumbnail">
</div>
</div><!-- end div .row 9 -->
</div><!-- end div .grid -->
/* Resetting font size */
html {
font-size: 1rem;
}
/**
* Grid
*/
.gallery__grid .row:nth-child(n+2) {
/* Adding some margin to create a bit of space under rows */
margin-top: 1rem;
}
.gallery__column {
/* In case the image is bigger than the column */
overflow: hidden;
}
// Creating self-invoking anonymous function
(function() {
'use strict';
var app = {
// Setting couple variables
settings: {
grid: $('#gallery__grid'),
modalDialog: $('#modal-dialog'),
elementClass: $('#gallery__grid').data('element')
},
controllers: function() {
// Run the code only if gallery exists
if (this.settings.grid.length > 0) {
// Modal Constructor
var modalBuilder = '<div id="modal-dialog" class="js-modal-overlay modal__overlay"><section class="js-modal-dialog modal-dialog"><button class="js-modal-close modal__close"><span class="sr-only">Close Modal</span>⨯</button><img class="js-modal-dialog__img modal-dialog__img" src="" alt="Gallery thumbnail"><div class="modal__navigation"><a href="#" class="js-modal-prev modal__prev theme-background">‹</a><a href="#" class="js-modal-next modal__next theme-background">›</a></div></section></div>';
// Putting the modal contructor right after the grid container
$(app.settings.grid).after(modalBuilder);
// Creating variables we are going to use through the code
var modalOverlay = $('.js-modal-overlay'),
modalImage = $('.js-modal-dialog__img'),
modalCloseBtn = $('.js-modal-close'),
nextImageAnchor = $('.js-modal-next'),
prevImageAnchor = $('.js-modal-prev'),
grid = $('.gallery__grid'),
imagesArray = grid.find(this.settings.elementClass),
imagesArrayLength = imagesArray.length;
// Styling the modal dialog
$('.js-modal-dialog').css({
'position': 'relative',
'top': '50%',
'right': '0',
'left': '0',
'transform': 'translateY(-50%)',
'margin-right': 'auto',
'margin-left': 'auto',
'max-width': '25rem',
'text-align': 'center'
});
// Styling the modal overlay
modalOverlay.css({
'position': 'fixed',
'top': '0',
'left': '0',
'z-index': '-1',
'width': '100%',
'height': '100%',
'background': 'rgba(0,0,0,.75)',
'opacity': '0',
'transition': '.25s opacity ease-in-out'
});
// Styling close button
modalCloseBtn.css({
'position': 'absolute',
'top': '-25px',
'right': '-26px',
'padding-top': '.75rem',
'padding-bottom': '.75rem',
'width': '3rem',
'font-size': '1rem',
'color': '#fff',
'border': '0',
'border-radius': '50%',
'opacity': '0',
'cursor': 'pointer',
'background': '#111',
'transition': '.25s opacity ease-in-out'
});
// Styling close button - hover over overlay
modalOverlay.on('mouseenter', function() {
modalCloseBtn.css({'opacity': '.5'});
});
// Styling close button - mouse leaves the overlay
modalOverlay.on('mouseleave', function() {
modalCloseBtn.css({'opacity': '0'});
});
// Styling close button - hover over button itself
modalOverlay.on('mouseenter', function() {
modalCloseBtn.on('mouseenter', function() {
$(this).css({'opacity': '1'});
});
});
// Styling close button - mouse leaves the button
modalOverlay.on('mouseleave', function() {
modalCloseBtn.on('mouseleave', function() {
$(this).css({'opacity': '.5'});
});
});
// Styling the arrows
$('.js-modal-next, .js-modal-prev').css({
'position': 'absolute',
'top': '50%',
'z-index': '91',
'display': 'block',
'width': '3rem',
'height': '3rem',
'font-size': '32px',
'font-weight': '400',
'text-decoration': 'none',
'color': '#fff',
'background': '#111',
'box-shadow': '0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.24)',
'opacity': '0',
'transition': '.25s all ease-in-out'
});
$('.js-modal-prev').css({
'left': '0'
});
$('.js-modal-next').css({
'right': '0'
});
// Styling arrows - hover over overlay
modalOverlay.on('mouseenter', function() {
$('.js-modal-next, .js-modal-prev').css({'opacity': '.5'});
});
// Styling arrows - mouse leaves the overlay
modalOverlay.on('mouseleave', function() {
$('.js-modal-next, .js-modal-prev').css({'opacity': '0'});
});
// Styling arrows - hover over the buttons
$('.js-modal-next, .js-modal-prev').on('mouseenter', function() {
$(this).css({
'color': '#fff',
'box-shadow': '0 3px 6px rgba(0,0,0,.16), 0 3px 6px rgba(0,0,0,.23)',
'opacity': '1'
});
});
// Styling arrows - mouse leaves the buttons
$('.js-modal-next, .js-modal-prev').on('mouseleave', function() {
$(this).css({
'color': '#fff',
'box-shadow': '0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.24)',
'opacity': '.5'
});
});
// Grid-Based Gallery
$('.' + app.settings.elementClass).on('click', function() {
// Itterating over all images in the gallery
$('.' + app.settings.elementClass).each(function(index) {
// Assigning data-index attribute with unique number
// to every image in the gallery
$(this).attr('data-index', index);
$(this).css({'max-width': '100%'});
});
var currentImage = $(this), // Currently clicked image
imgSrc = currentImage.attr('src'), // Src attribute of clicked image
imgAlt = currentImage.attr('alt'), // Alt attribute of clicked image
currentImageIndex = currentImage.data('index'), // Number from data-index attribute of clicked image
imgArray = $('img[data-index]'), // array of all images
numOfImages = imgArray.length - 1, // switch to 0-based index
nextIndex, // prepare nextIndex variable
prevIndex; // prepare prevIndex variable
if (currentImageIndex > 0) {
// If you are not on the first image, decrease the index by 1
prevIndex = currentImageIndex - 1;
} else {
// If you are on the first image, go on the last one
prevIndex = numOfImages;
}
if ((currentImageIndex + 1) < numOfImages) {
// If you are not on the last image, increase the index by 1
nextIndex = currentImageIndex + 1;
} else {
// If you are on the last image, go on the first one
nextIndex = 0;
}
// Attach image to modal window
function imageAttach() {
modalImage.attr({
src: imgSrc,
alt: imgAlt,
"data-index": currentImageIndex
});
}
// Open Modal Window
function modalOpen() {
modalOverlay.css({
'z-index': '90',
'opacity': '1'
});
// Show previous image on "left arrow" key press
$(document).on('keydown', function(e) {
if (e.keyCode == 37 || e.charCode == 37 || e.which == 37) {
prevImage(e);
}
});
// Show next image on "right arrow" key press
$(document).on('keydown', function(e) {
if (e.keyCode == 39 || e.charCode == 39 || e.which == 39) {
nextImage(e);
}
});
// Close modal on "Esc" key press
$(document).on('keydown', function(e) {
if (e.keyCode == 27 || e.charCode == 27 || e.which == 27) {
closeModal(e);
}
});
}
// Show previous image
function prevImage(e) {
e.preventDefault();
modalImage.attr({
src: $('[data-index=' + prevIndex + ']').attr('src'),
alt: $('[data-index=' + prevIndex + ']').attr('alt'),
'data-index': $('[data-index=' + prevIndex + ']').attr('data-index')
});
currentImageIndex = prevIndex;
if (currentImageIndex > 0) {
// If you are not on the first image, decrease the index by 1
prevIndex = currentImageIndex - 1;
} else {
// If you are on the first image, go on the last one
prevIndex = numOfImages;
}
if ((currentImageIndex + 1) < numOfImages) {
// If you are not on the last image, increase the index by 1
nextIndex = currentImageIndex + 1;
} else {
// If you are on the last image, go on the first one
nextIndex = 0;
}
}
// Show next image
function nextImage(e) {
e.preventDefault();
modalImage.attr({
src: $('[data-index=' + nextIndex + ']').attr('src'),
alt: $('[data-index=' + nextIndex + ']').attr('alt'),
'data-index': $('[data-index=' + nextIndex + ']').attr('data-index')
});
currentImageIndex = nextIndex;
if (currentImageIndex > 0) {
// If you are not on the first image, decrease the index by 1
prevIndex = currentImageIndex - 1;
} else {
// If you are on the first image, go on the last one
prevIndex = numOfImages;
}
if ((currentImageIndex + 1) <= numOfImages) {
// If you are not on the last image, increase the index by 1
nextIndex = currentImageIndex + 1;
} else {
// If you are on the last image, go on the first one
nextIndex = 0;
}
}
// Closing Modal Window
function closeModal(e) {
e.preventDefault();
modalOverlay.css({
'z-index': '-1',
'opacity': '0'
});
}
// Attach clicked image to modal dialog
imageAttach();
// Open modal dialog
modalOpen();
// Handle click on right arrow
nextImageAnchor.on('click', function(e) {
nextImage(e);
});
// Handle click on left arrow
prevImageAnchor.on('click', function(e) {
prevImage(e);
});
// Handle click on close button
modalCloseBtn.on('click', function(e) {
closeModal(e);
});
});
}
},
// Run the controllers
init: function() {
app.controllers();
}
};
// Initialize the app
app.init();
})($);
Also see: Tab Triggers