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="container">
<div>
<div id="paginate"></div>
</div>
<div>
<div id="searchResults"></div>
</div>
</div>
.container {
width: 90%;
margin: auto;
text-align: center;
background-color: #ddd;
border: 1px solid #999;
}
#paginate {
display: inline-block;
}
.page-link {
margin: 10px;
font-size: 22px;
padding: 4px;
}
.page-link a {
text-decoration: none;
}
.next-link {
display: inline-block;
}
/* Constants */
RESULTS_PER_PAGE = 10;
SEARCH_RESULTS = 'searchResults' // ID of element to display search results
PAGINATE = 'paginate' // ID of element to display pagination results
/**
* pagination()
*
* Returns an array of page numbers between one (1) and a given end number,
* returning a maximum of 10 numbers, with missing ranges replaced with elipses
* (which are included in the maximum return count). A 'current page' is
* supplied, and the current page number is surrounded by the two sequential
* numbers before and after.
*
* The total number of numbers to return is determined by the total number
* of results to expect divided by the number of results to display per page.
* This is further affected by the current page displayed. Possible outcomes of
* the function could look like so:
*
* [1, 2, 3, 4, 5, 6, 7, '...', 44, 45] // 1-7 is the current page
* [1, 2, 3, '...', 7, 8, 9, 10, 11, 12] // 2 is the current page
* [1, 2, '...', 10, 11, 12, '...', 44, 45] // 8-39 is the current page
* [1, 2, '...', 33, 34, 35, 36, 37, 38, 39] // 33-39 is the current page
*
* @param {number} total_results (Required: The total number of results to
* expect, the end number.)
*
* @param {number} start_number (Required: The value/start number of the page
* currently displayed.)
*
* @param {number} results_page (Optional: The number of results to show for
* each page. Defaults to 10.)
*
* @return {array} (An array containing the range numbers.)
*
* @throws {TypeError} (If any parameter is not a finite number.)
* @throws {Error} (If any parameter is not a positive number.)
*
* modified from: https://www.freecodecamp.org/news/https-medium-com-gladchinda-hacks-for-creating-javascript-arrays-a1b80cb372b/
* author: Ammon Shepherd
* date: August 12, 2019
*/
function pagination(total_results, start_number, results_page = 10) {
// Test that the first 3 arguments are finite numbers.
// Using Array.prototype.every() and Number.isFinite().
const allNumbers = [total_results, results_page, start_number].every(Number.isFinite);
// Throw an error if any of the first 3 arguments is not a finite number.
if (!allNumbers) {
throw new TypeError('pagination() expects only finite numbers as arguments.');
}
// Ensure the step is always a positive number.
if (total_results <= 0) {
throw new Error('total_results must be a number greater than 0.');
}
if (results_page <= 0) {
throw new Error('results_page must be a number greater than 0.');
}
if (start_number < 0) {
throw new Error('start_number must be a number greater than or equal to 0.');
}
// Determine the length of the array to be returned, which is the total
// results divided by the number of results per page.
const length = Math.ceil(Math.abs(total_results / results_page));
// 10 pages or less, just return the array
if (length <= 10 ) {
// Fill up a new array with the range numbers
// using Array.from() with a mapping function.
let full_array = Array.from(Array(length), (x, index) => index + 1);
return full_array
}
let current_page = (start_number / results_page) + 1;
if (start_number == 1){
current_page = 1;
}
let pages = new Array();
for (i = 1; i <= length; i++) {
// If the current page is 7 or less, show pages 1-7, then ellipses, then
// the last two pages
if ( (current_page < 7 ) && (i < 7) ) {
pages.push(i);
continue;
}
if ( (i == 8) && (current_page < 6) ) {
pages.push('...');
continue;
}
// if the current page is within six places of the last page, show the
// last six pages.
if ( (current_page > length - 5) && (i > length - 5) ) {
pages.push(i);
continue;
}
if ( (i == length - 6) && (current_page > length - 4) ) {
pages.push('...');
continue;
}
// Show the first two pages, the current page surrounded by a page and
// ellispses, then the last two pages
if (i < 3) {
pages.push(i);
} else if (i == current_page - 1) {
pages.push('...');
pages.push(i);
} else if (i == current_page) {
pages.push(i);
continue;
} else if ( (i == current_page + 1) && (current_page < length - 3) ) {
pages.push(i);
pages.push('...');
}
if ( (i == length - 1) || (i == length) ) {
pages.push(i);
}
}
return pages;
}
/**
* buildPagination()
*
* Generates and inserts the pagination links into the HTML document. This
* function should be called when the page is loaded. The function takes the
* total number of results returned from the search query.
*
* @param {number} results_total (Required: The total number of results
* returned from the search query.)
*
* @throws {Error} (If any parameter is not a positive number.)
*
* author: Ammon Shepherd
* date: August 22, 2019
*/
function buildPagination(results_total) {
// Ensure the step is always a positive number.
if (results_total <= 0) {
throw new Error('total_results must be a number greater than 0.');
}
const url = new URL(window.location.origin + window.location.pathname);
let searchParams = new URLSearchParams(window.location.search);
var params = (new URL(document.location)).searchParams;
var current_start = parseInt(params.get('start')); // value of start parameter
// if the URL parameter 'start' is empty, change it to 0, the first page
if (!current_start) {
current_start = 0;
}
// the start param returns the results to start on, to convert to the
// current page number, divide by results per page and add 1.
var cp = (current_start / RESULTS_PER_PAGE) + 1; // current page number
// If there are no results, just return some text saying that.
if (results_total < 1) {
const result = document.getElementById(SEARCH_RESULTS);
result.insertAdjacentHTML('afterbegin', '<h2>No results found.</h2>');
return;
}
// Generate an array of page numbers, using the pagination function
let pages_list = pagination(results_total, current_start, RESULTS_PER_PAGE);
const paginate = document.getElementById(PAGINATE);
// loop through the array of pages, and construct a link for each page
// number. The current page number does not become a link. Also create the
// previous and next links.
for (n = 0; n < pages_list.length; n++) {
var page_num = pages_list[n]; // the page number from the array
// if the page number is actually an elipse, just return an elipse
// without a link
if (page_num == "...") {
paginate.insertAdjacentHTML('beforeend', `<span class="page-link">${page_num}</span>`);
} else {
// start = where the search results should start.
// This is the page number (in the array), minus one (1), times the
// number of results per page
var start = (page_num - 1) * RESULTS_PER_PAGE;
// if the page number in the array that we are on is the same as
// the current page (calculated from the start parameter) then this
// is the current page,
if (page_num == cp) {
// Show the previous link if the current page is greater than 5
if (cp > 5) {
// the start value should be the results per page quantity
// less than the current start value
params.set('start', start - RESULTS_PER_PAGE);
paginate.insertAdjacentHTML('afterbegin', `<span class="page-link"><a href='${url.toString()}?${params.toString()}'>☜</a></span>`)
}
// Show the next link if the current page is 5 places less than
// the last one (which you get by getting the value of the last
// element in the pages_list array) then subtract 5
if (cp < pages_list[pages_list.length - 1] - 4 ) {
// if the current page is 1, then start should be 0
if (cp == 1){
start = 0;
}
params.set('start', start + RESULTS_PER_PAGE);
paginate.insertAdjacentHTML('afterend', `<div class="page-link next-link"><a href='${url}?${params.toString()}'>☞</a></div>`)
}
// Show the current page number, without a link, then skip the
// rest of the code in the loop and go to the next element in
// the array
paginate.insertAdjacentHTML('beforeend', `<span class="page-link current-page-link btn-floating btn-large">${page_num}</span>`);
continue;
}
// display the rest of the links
params.set('start', start);
paginate.insertAdjacentHTML('beforeend', `<span class="page-link"><a href='${url}?${params.toString()}'>${page_num}</a></span>`);
}
}
}
// Generate the page navigation links
buildPagination(200);
Also see: Tab Triggers