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 URL's 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 it's URL and the proper URL extention.
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.
<h1>Demo: Vanilla JavaScript Smooth Scroll <a href="https://perishablepress.com/wp/wp-content/online/pages/javascript-scroll-anchors.html">(originally by Jeff Starr)</a></h1>
<p>Click any link to scroll to the target. <a href="https://perishablepress.com/vanilla-javascript-scroll-anchor/">Visit the tutorial »</a></p>
<ul>
<li><a class="scroll" href="#example-1" data-tab-index="1">Example 1</a></li>
<li><a class="scroll" href="#example-2" data-tab-index="2">Example 2</a></li>
<li><a class="scroll" href="#example-3" data-tab-index="3">Example 3</a></li>
</ul>
<h2 id="example-1" tabindex="-1">Example 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque commodo id est ut tincidunt. Sed vel ipsum sit amet leo fringilla ornare. Nam sed metus dapibus, efficitur eros id, varius nulla. Cras tincidunt cursus ligula, vel aliquam ante ornare ut. Aliquam id consectetur neque. Nam venenatis, turpis sed dictum aliquet, augue diam varius leo, a consectetur eros ex sit amet elit. Etiam non mollis felis. Curabitur fermentum nunc ac pharetra iaculis. Aenean hendrerit nisl a felis accumsan mattis ac a quam.</p>
<p>Vivamus vitae ultrices orci. Donec molestie, ipsum sit amet ultricies cursus, tellus nisi congue elit, vel consectetur risus erat a magna. In gravida, tellus et molestie mattis, nisi justo tristique dui, sed feugiat urna ligula et erat. Phasellus tempus, quam at sollicitudin semper, nisi metus fringilla tellus, vel accumsan nulla lorem a tortor. Vivamus porta leo id magna accumsan elementum. Integer in finibus arcu, ut molestie sem. Cras semper scelerisque leo at vestibulum.</p>
<p>Vivamus varius feugiat justo eget sollicitudin. Cras feugiat consectetur nunc, et laoreet ligula suscipit a. Fusce rutrum, turpis at convallis tempus, ligula urna consectetur orci, ac hendrerit lorem elit ut tortor. Morbi non mauris rutrum, ullamcorper metus quis, auctor nibh. Curabitur quis libero non enim dapibus fringilla at eget tellus. Proin lobortis tellus quis fringilla sollicitudin. Mauris vitae velit pulvinar, dignissim mi dapibus, eleifend augue. Cras bibendum eleifend aliquam. Nam urna massa, tristique at convallis id, scelerisque eget purus. In id elit sit amet lectus feugiat pharetra. Suspendisse viverra nisi leo, ut auctor neque gravida sed. Quisque eleifend porta elit, ut sagittis quam dictum luctus. Mauris bibendum rutrum pellentesque. Nam ac libero eu purus luctus luctus. Pellentesque a auctor turpis. Sed blandit, metus sit amet finibus viverra, quam ex posuere risus, eu scelerisque nulla leo nec quam.</p>
<p>In hac habitasse platea dictumst. Morbi non venenatis enim. Sed eleifend mattis odio, dignissim malesuada diam placerat in. Nunc sed gravida neque. Proin pretium faucibus nunc et ultrices. Cras erat nisl, venenatis id maximus pretium, dictum et augue. Nunc semper venenatis est et venenatis.</p>
<p>Quisque laoreet semper nisi, eleifend interdum nisi maximus eget. Aliquam erat volutpat. Donec vel metus metus. Vestibulum pharetra pulvinar ligula, scelerisque dictum ligula hendrerit eu. Suspendisse fermentum nunc et mauris semper, sit amet ullamcorper augue sollicitudin. Mauris non nisl condimentum, fermentum arcu nec, consequat mauris. Suspendisse ligula nibh, fermentum eu tristique ac, bibendum et ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Morbi quam arcu, consectetur eu est ut, finibus cursus magna. Fusce ac nulla tristique, fermentum sem non, pharetra libero. Sed eleifend efficitur ante, vel ultrices nisi rhoncus at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel neque ex.</p>
<h2 id="example-2" tabindex="-1">Example 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque commodo id est ut tincidunt. Sed vel ipsum sit amet leo fringilla ornare. Nam sed metus dapibus, efficitur eros id, varius nulla. Cras tincidunt cursus ligula, vel aliquam ante ornare ut. Aliquam id consectetur neque. Nam venenatis, turpis sed dictum aliquet, augue diam varius leo, a consectetur eros ex sit amet elit. Etiam non mollis felis. Curabitur fermentum nunc ac pharetra iaculis. Aenean hendrerit nisl a felis accumsan mattis ac a quam.</p>
<p>Vivamus vitae ultrices orci. Donec molestie, ipsum sit amet ultricies cursus, tellus nisi congue elit, vel consectetur risus erat a magna. In gravida, tellus et molestie mattis, nisi justo tristique dui, sed feugiat urna ligula et erat. Phasellus tempus, quam at sollicitudin semper, nisi metus fringilla tellus, vel accumsan nulla lorem a tortor. Vivamus porta leo id magna accumsan elementum. Integer in finibus arcu, ut molestie sem. Cras semper scelerisque leo at vestibulum.</p>
<p>Vivamus varius feugiat justo eget sollicitudin. Cras feugiat consectetur nunc, et laoreet ligula suscipit a. Fusce rutrum, turpis at convallis tempus, ligula urna consectetur orci, ac hendrerit lorem elit ut tortor. Morbi non mauris rutrum, ullamcorper metus quis, auctor nibh. Curabitur quis libero non enim dapibus fringilla at eget tellus. Proin lobortis tellus quis fringilla sollicitudin. Mauris vitae velit pulvinar, dignissim mi dapibus, eleifend augue. Cras bibendum eleifend aliquam. Nam urna massa, tristique at convallis id, scelerisque eget purus. In id elit sit amet lectus feugiat pharetra. Suspendisse viverra nisi leo, ut auctor neque gravida sed. Quisque eleifend porta elit, ut sagittis quam dictum luctus. Mauris bibendum rutrum pellentesque. Nam ac libero eu purus luctus luctus. Pellentesque a auctor turpis. Sed blandit, metus sit amet finibus viverra, quam ex posuere risus, eu scelerisque nulla leo nec quam.</p>
<p>In hac habitasse platea dictumst. Morbi non venenatis enim. Sed eleifend mattis odio, dignissim malesuada diam placerat in. Nunc sed gravida neque. Proin pretium faucibus nunc et ultrices. Cras erat nisl, venenatis id maximus pretium, dictum et augue. Nunc semper venenatis est et venenatis.</p>
<p>Quisque laoreet semper nisi, eleifend interdum nisi maximus eget. Aliquam erat volutpat. Donec vel metus metus. Vestibulum pharetra pulvinar ligula, scelerisque dictum ligula hendrerit eu. Suspendisse fermentum nunc et mauris semper, sit amet ullamcorper augue sollicitudin. Mauris non nisl condimentum, fermentum arcu nec, consequat mauris. Suspendisse ligula nibh, fermentum eu tristique ac, bibendum et ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Morbi quam arcu, consectetur eu est ut, finibus cursus magna. Fusce ac nulla tristique, fermentum sem non, pharetra libero. Sed eleifend efficitur ante, vel ultrices nisi rhoncus at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel neque ex.</p>
<h2 id="example-3" tabindex="-1">Example 3</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque commodo id est ut tincidunt. Sed vel ipsum sit amet leo fringilla ornare. Nam sed metus dapibus, efficitur eros id, varius nulla. Cras tincidunt cursus ligula, vel aliquam ante ornare ut. Aliquam id consectetur neque. Nam venenatis, turpis sed dictum aliquet, augue diam varius leo, a consectetur eros ex sit amet elit. Etiam non mollis felis. Curabitur fermentum nunc ac pharetra iaculis. Aenean hendrerit nisl a felis accumsan mattis ac a quam.</p>
<p>Vivamus vitae ultrices orci. Donec molestie, ipsum sit amet ultricies cursus, tellus nisi congue elit, vel consectetur risus erat a magna. In gravida, tellus et molestie mattis, nisi justo tristique dui, sed feugiat urna ligula et erat. Phasellus tempus, quam at sollicitudin semper, nisi metus fringilla tellus, vel accumsan nulla lorem a tortor. Vivamus porta leo id magna accumsan elementum. Integer in finibus arcu, ut molestie sem. Cras semper scelerisque leo at vestibulum.</p>
<p>Vivamus varius feugiat justo eget sollicitudin. Cras feugiat consectetur nunc, et laoreet ligula suscipit a. Fusce rutrum, turpis at convallis tempus, ligula urna consectetur orci, ac hendrerit lorem elit ut tortor. Morbi non mauris rutrum, ullamcorper metus quis, auctor nibh. Curabitur quis libero non enim dapibus fringilla at eget tellus. Proin lobortis tellus quis fringilla sollicitudin. Mauris vitae velit pulvinar, dignissim mi dapibus, eleifend augue. Cras bibendum eleifend aliquam. Nam urna massa, tristique at convallis id, scelerisque eget purus. In id elit sit amet lectus feugiat pharetra. Suspendisse viverra nisi leo, ut auctor neque gravida sed. Quisque eleifend porta elit, ut sagittis quam dictum luctus. Mauris bibendum rutrum pellentesque. Nam ac libero eu purus luctus luctus. Pellentesque a auctor turpis. Sed blandit, metus sit amet finibus viverra, quam ex posuere risus, eu scelerisque nulla leo nec quam.</p>
<p>In hac habitasse platea dictumst. Morbi non venenatis enim. Sed eleifend mattis odio, dignissim malesuada diam placerat in. Nunc sed gravida neque. Proin pretium faucibus nunc et ultrices. Cras erat nisl, venenatis id maximus pretium, dictum et augue. Nunc semper venenatis est et venenatis.</p>
<p>Quisque laoreet semper nisi, eleifend interdum nisi maximus eget. Aliquam erat volutpat. Donec vel metus metus. Vestibulum pharetra pulvinar ligula, scelerisque dictum ligula hendrerit eu. Suspendisse fermentum nunc et mauris semper, sit amet ullamcorper augue sollicitudin. Mauris non nisl condimentum, fermentum arcu nec, consequat mauris. Suspendisse ligula nibh, fermentum eu tristique ac, bibendum et ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Morbi quam arcu, consectetur eu est ut, finibus cursus magna. Fusce ac nulla tristique, fermentum sem non, pharetra libero. Sed eleifend efficitur ante, vel ultrices nisi rhoncus at. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vel neque ex.</p>
<p><a href="https://perishablepress.com/css-background-hover-slide-effect/">Visit the tutorial »</a></p>
/*
*
* Event delegation is a bit cleaner imho
* and requires less horsepower if you've got a lot of links to work with. =)
*
*/
document.addEventListener('click', function(e) {
// If it isn't an anchor element, don't even bother...
if (e.target.tagName !== 'A') return;
if ((e.target.href && e.target.href.indexOf('#') != -1) && ((e.target.pathname == location.pathname) || ('/' + e.target.pathname == location.pathname)) && (e.target.search == location.search)) {
/**
* If everything checks out,
* pass the click event and the event target
* on through to the scrollAnchors function.
*/
scrollAnchors(e, e.target);
}
/**
*
* If you want to target links that have the scroll class, just uncomment the code below (after
* commenting out the code above, of course! =D ):
*
*/
// if (e.target.tagName !== 'A') return;
// if (e.target.className.contains('.scroll')) {
// scrollAnchors(e, e.target);
// }
});
function scrollAnchors(e, respond = null) {
// const distanceToTop = el => Math.floor(el.getBoundingClientRect().top);
function distanceToTop(el) {
return Math.floor(el.getBoundingClientRect().top);
}
e.preventDefault();
var targetID = (respond) ? respond.getAttribute('href') : this.getAttribute('href');
var targetAnchor = document.querySelector(targetID);
if (!targetAnchor) return;
var originalTop = distanceToTop(targetAnchor);
window.scrollBy({ top: originalTop, left: 0, behavior: 'smooth' });
var checkIfDone = setInterval(function() {
var atBottom = window.innerHeight + window.pageYOffset >= document.body.offsetHeight - 2;
if (distanceToTop(targetAnchor) === 0 || atBottom) {
targetAnchor.tabIndex = '-1';
targetAnchor.focus();
// Let's make sure the History API even exists first..
if ('history' in window) {
window.history.pushState('', '', targetID);
} else {
// Do it the old-fashioned way!
window.location = targetID;
}
clearInterval(checkIfDone);
}
}, 100);
}
Also see: Tab Triggers