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 Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
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.
<!-- include a single button, to be absolute positioned in the center of the viewport
the columns of different colors are added through the script
-->
<button>
🌈 Sort <span>🌈</span>
</button>
@import url("https://fonts.googleapis.com/css?family=Luckiest+Guy");
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
/* specify the width and height of the body to encompass the viewport */
body {
width: 100vw;
height: 100vh;
/* include a grid whose items are automatically added as column and occupy a fraction of the width */
display: grid;
grid-auto-columns: 1fr;
grid-auto-flow: column;
}
/* absolute position the button in the center of teh viewport */
button {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 0.6rem 1.25rem;
background: hsl(0, 0%, 100%);
border: none;
border-radius: 10px;
box-shadow: 0 2px 5px hsla(0, 0%, 0%, 0.3);
color: #1c2d4a;
text-transform: uppercase;
font-family: "Luckiest Guy", cursive;
font-size: 2rem;
letter-spacing: 0.3rem;
/* transition for the transform and box-shadow properties */
transition: all 0.3s ease-out;
}
/* on hover slightly elevate the button */
button:hover {
transform: translate(-50%, calc(-50% - 2px));
box-shadow: 0 2px 7px hsla(0, 0%, 0%, 0.4);
}
/* when a class of .clicked is added (through the script) animate the button out of sight */
button.clicked {
animation: clicked 0.8s cubic-bezier(0.13, 1.03, 0.72, 1.32) forwards;
}
/* animation to have the button pushed downward and then disappear as it returns to its original location */
@keyframes clicked {
10%,
35% {
transform: translate(-50%, calc(-50% + 8px)) scaleY(0.95);
box-shadow: 0 1px 5px hsla(0, 0%, 0%, 0.2);
opacity: 1;
visibility: visible;
}
80%,
100% {
opacity: 0;
visibility: hidden;
}
}
button span {
display: inline-block;
transform: rotateY(180deg);
}
/*
variables tweaking the design
- numberOfColumns: the number of columns to be included in the viewport
! the value is capped to 360 to have at most one color for each hue in the [0-360] color wheel
- duration: the number of seconds it takes to complete the bubble sort
! this does not account for the animation of the button, but the animation of the columns only
- lightness and saturation: default values for the hsl color
*/
const numberOfColumns = 359;
const duration = 7.5;
const saturation = 70;
const lightness = 70;
// array describing the hues of the different columns, to avoid doubles
const hues = [];
// function returning a random hue, which is not already in the hues array
const uniqueHue = () => {
const hue = Math.floor(Math.random() * 360);
// if the hue is already present re-run the function
if (hues.includes(hue)) {
return uniqueHue();
}
// else add the hue to the array and return its value
hues.push(hue);
return hue;
};
// select the node in which to add the columns
const body = document.querySelector('body');
// in the body include as many div elements as specified by the columns variable
for (let i = 0; i < Math.min(numberOfColumns, 359); i += 1) {
const columns = document.createElement('div');
// with a unique hue
const hue = uniqueHue();
columns.style.background = `hsl(${hue}, ${saturation}%, ${lightness}%)`;
// with a transition incrementally delayed for each successive column
// ! ultimately this has the effect of swapping the columns one after the other
columns.style.transition = `all 0.2s ${duration / numberOfColumns * i}s ease-out`;
// add a data attribute to rapidly order the columns according to the hue value
columns.setAttribute('data-hue', hue);
body.appendChild(columns);
}
// target the button
const button = document.querySelector('button');
// function called to sort the items of an array according to data-hue attribute
function bubbleSort(arr) {
// bubble sort algorithm
// instead of swapping the items based on their values, swap them on the basis of the data-hue attribute
for (let n = arr.length; n >= 0; n -= 1) {
for (let i = 0; i < n - 1; i += 1) {
const currentPosition = i;
const nextPosition = i + 1;
const current = arr[currentPosition];
const next = arr[nextPosition];
const currentHue = Number.parseInt(current.getAttribute('data-hue'), 10);
const nextHue = Number.parseInt(next.getAttribute('data-hue'), 10);
if (currentHue > nextHue) {
[arr[currentPosition], arr[nextPosition]] = [arr[nextPosition], arr[currentPosition]];
}
}
}
return arr;
}
// function called when the button is pressed
function handleClick() {
// add the class prompting the transition
button.classList.add('clicked');
// once the button's animation is complete
button.addEventListener('animationend', () => {
// target all the columns
const columns = body.querySelectorAll('div');
// sort an copy of the node list, converting it first to an array
const sortedColumns = bubbleSort([...columns]);
// loop through the sortedColumns array
for (let i = 0; i < sortedColumns.length; i += 1) {
// find the column in the unsorted data structure with the data-hue attribute matching the sorted value
const sortedColumn = sortedColumns[i];
const matchingColumn = [...columns].find(column => column.getAttribute('data-hue') === sortedColumn.getAttribute('data-hue'));
// apply a property of order beginning with the smallest values, pushing the smaller hues to the beginning of the grid
matchingColumn.style.order = i - sortedColumns.length;
}
});
}
// on click call the function prompting the sorting and the application of the order property according to the hue
button.addEventListener('click', handleClick);
Also see: Tab Triggers