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.
<h1 class="heading">Paginate anything!</h1>
<p class="heading">Take any set of elements, regardless of their children (or parent). Apply the paginate method to them. <strong>Voila!</strong></p>
<div class="article-loop">
<h3>Article 1</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 2</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum dolor sit amet.</p>
</div>
<div class="article-loop">
<h3>Article 3</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum dolor.</p>
</div>
<div class="article-loop">
<h3>Article 4</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Expedita quas incidunt soluta.</p>
</div>
<div class="article-loop">
<h3>Article 5</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Atque, ad earum minus illo recusandae vitae fuga laboriosam facilis omnis autem.</p>
</div>
<div class="article-loop">
<h3>Article 6</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 7</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum.</p>
</div>
<div class="article-loop">
<h3>Article 8</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 9</h3>
<p>Lorem ipsum set dolor amet. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus et qui id atque.</p>
</div>
<div class="article-loop">
<h3>Article 10</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 11</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 12</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 13</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 14</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
<div class="article-loop">
<h3>Article 15</h3>
<p>Lorem ipsum set dolor amet.</p>
</div>
.pagination {
display: block;
width: 75%;
margin: 1em auto;
text-align: center;
&:after {
content: '';
clear: both;
}
}
.pagination-button {
display: inline-block;
padding: 5px 10px;
border: 1px solid #e0e0e0;
background-color: #eee;
color: #333;
cursor: pointer;
transition: background 0.1s, color 0.1s;
&:hover {
background-color: #ddd;
color: #3366cc;
}
&.active {
background-color: #bbb;
border-color: #bbb;
color: #3366cc;
}
$border-radius: 18px;
&:first-of-type {
border-radius: $border-radius 0 0 $border-radius;
}
&:last-of-type {
border-radius: 0 $border-radius $border-radius 0;
}
}
/* arbitrary styles */
.heading { text-align: center; max-width: 500px; margin: 20px auto; }
.article-loop {
display: block;
width: 75%;
padding: 1em 2em;
margin: 1em auto;
border: 1px solid #ddd;
background-color: #ededed;
}
/*
* jQuery Pagination
* Author: Austin Wulf (@austinwulf)
*
* Call the paginate method on an array
* of elements. Accepts # of items per page
* as an argument. Defaults to 5.
*
* Example:
* $(selector).paginate(3);
*
* Released under the MIT License.
*
* v 1.0
*/
(function($){
var paginate = {
startPos: function(pageNumber, perPage) {
// determine what array position to start from
// based on current page and # per page
return pageNumber * perPage;
},
getPage: function(items, startPos, perPage) {
// declare an empty array to hold our page items
var page = [];
// only get items after the starting position
items = items.slice(startPos, items.length);
// loop remaining items until max per page
for (var i=0; i < perPage; i++) {
page.push(items[i]); }
return page;
},
totalPages: function(items, perPage) {
// determine total number of pages
return Math.ceil(items.length / perPage);
},
createBtns: function(totalPages, currentPage) {
// create buttons to manipulate current page
var pagination = $('<div class="pagination" />');
// add a "first" button
pagination.append('<span class="pagination-button">«</span>');
// add pages inbetween
for (var i=1; i <= totalPages; i++) {
// truncate list when too large
if (totalPages > 5 && currentPage !== i) {
// if on first two pages
if (currentPage === 1 || currentPage === 2) {
// show first 5 pages
if (i > 5) continue;
// if on last two pages
} else if (currentPage === totalPages || currentPage === totalPages - 1) {
// show last 5 pages
if (i < totalPages - 4) continue;
// otherwise show 5 pages w/ current in middle
} else {
if (i < currentPage - 2 || i > currentPage + 2) {
continue; }
}
}
// markup for page button
var pageBtn = $('<span class="pagination-button page-num" />');
// add active class for current page
if (i == currentPage) {
pageBtn.addClass('active'); }
// set text to the page number
pageBtn.text(i);
// add button to the container
pagination.append(pageBtn);
}
// add a "last" button
pagination.append($('<span class="pagination-button">»</span>'));
return pagination;
},
createPage: function(items, currentPage, perPage) {
// remove pagination from the page
$('.pagination').remove();
// set context for the items
var container = items.parent(),
// detach items from the page and cast as array
items = items.detach().toArray(),
// get start position and select items for page
startPos = this.startPos(currentPage - 1, perPage),
page = this.getPage(items, startPos, perPage);
// loop items and readd to page
$.each(page, function(){
// prevent empty items that return as Window
if (this.window === undefined) {
container.append($(this)); }
});
// prep pagination buttons and add to page
var totalPages = this.totalPages(items, perPage),
pageButtons = this.createBtns(totalPages, currentPage);
container.after(pageButtons);
}
};
// stuff it all into a jQuery method!
$.fn.paginate = function(perPage) {
var items = $(this);
// default perPage to 5
if (isNaN(perPage) || perPage === undefined) {
perPage = 5; }
// don't fire if fewer items than perPage
if (items.length <= perPage) {
return true; }
// ensure items stay in the same DOM position
if (items.length !== items.parent()[0].children.length) {
items.wrapAll('<div class="pagination-items" />');
}
// paginate the items starting at page 1
paginate.createPage(items, 1, perPage);
// handle click events on the buttons
$(document).on('click', '.pagination-button', function(e) {
// get current page from active button
var currentPage = parseInt($('.pagination-button.active').text(), 10),
newPage = currentPage,
totalPages = paginate.totalPages(items, perPage),
target = $(e.target);
// get numbered page
newPage = parseInt(target.text(), 10);
if (target.text() == '«') newPage = 1;
if (target.text() == '»') newPage = totalPages;
// ensure newPage is in available range
if (newPage > 0 && newPage <= totalPages) {
paginate.createPage(items, newPage, perPage); }
});
};
})(jQuery);
/* This part is just for the demo,
not actually part of the plugin */
$('.article-loop').paginate(2);
Also see: Tab Triggers