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 id="scroll-container">
<div id="indicator"></div>
</div>
<article>
<h1 id="header-1">Scroll Indicator</h1>
<p>This is a page scroll indicator inspired by the one on <a href="https://lesswrong.com">LessWrong</a>. Headers are noted and immediately jumpable, and it has two possible appearances that swap out based on breakpoints for a responsive experience best tailored to the user.</p>
<p>On viewports with sufficient horizontal space, the indicator is displayed vertically and has a thumb that represents the height of the user's viewport. On narrower viewports, the indicator manifests as a horizontal progress bar at the top of the screen.</p>
<p>Ideally, the base of this effect could be achieved using scroll-driven animations with additional JavaScript-based functionality considered progressive enhancement, but that has restrictions if the page has content beyond the main article, such as comments, where one must start implementing <code>overflow: scroll</code> hackery.</p>
<h2 id="header-2">Header 2</h2>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<h3 id="header-3">Header 3</h3>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<h2 id="header-4">Header 4</h2>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
</article>
<section>
<h2>Other, non-article content</h2>
<p>This content shouldn't be shown in the scrollbar</p>
<p>Deserunt molestias esse occaecati. Et nesciunt ad inventore tenetur quos quisquam. Adipisci labore nemo rerum rerum sit doloribus blanditiis quia. Voluptatem sequi odit ea repellendus. Quasi vero cupiditate quia esse.</p>
</section>
:root {
color-scheme: light dark;
--black: oklch(18% 0.003 17.5);
--dark_grey: oklch(24% 0.006 17.6);
--grey: oklch(36% 0.003 17.3);
--bright_grey: oklch(47% 0.005 17.3);
--white: oklch(76% 0.03 55);
--bright_white: oklch(94.75% 0.04 73);
--blue: oklch(56.5% 0.13 253);
--cyan: oklch(71.2% 0.112 198.7);
--magenta: oklch(61.2% 0.21 5.4);
}
body {
margin: 3rem 3rem 3rem 5rem;
font-family: "EB Garamond", serif;
color: light-dark(var(--black), var(--bright_white));
background: light-dark(var(--bright_white), var(--black));
@media (max-width: 45rem) {
margin: 0 0.8rem;
}
}
article {
max-width: 80ch;
* + h2,
* + h3 {
margin-top: 2rem;
}
> * + * {
margin-top: 1rem;
}
}
h1,
h2,
h3,
h4,
h5,
h6 {
text-transform: uppercase;
text-wrap: balance;
font-weight: 700;
}
h1 {
font-size: 2.5rem;
line-height: 2.8rem;
}
h2 {
font-size: 1.8rem;
line-height: 2rem;
}
h3 {
font-size: 1.2rem;
line-height: 1.4rem;
}
a {
color: inherit;
text-decoration: underline 0.1rem light-dark(var(--cyan), var(--blue));
-webkit-text-decoration-line: underline;
-webkit-text-decoration-color: light-dark(var(--cyan), var(--blue));
text-decoration-skip-ink: none;
text-underline-offset: 0.2rem;
&:hover,
&:active {
text-decoration-thickness: 0.7rem;
text-underline-offset: -0.37rem;
}
}
code {
font-size: 80%;
}
#scroll-container {
display: none; /* Set to block in JS */
position: fixed;
inset: 2rem auto 2rem 1rem;
width: 2px;
background-color: light-dark(var(--white), var(--grey));
z-index: 1000;
#indicator {
position: absolute;
left: -0.5px;
width: 3px;
background-color: var(--magenta);
min-height: 5%;
}
.heading-indicator {
position: absolute;
left: -0.5px;
width: 3px;
height: 5px;
background-color: light-dark(var(--bright_white), var(--black));
&:hover::after {
content: attr(title);
position: absolute;
left: 15px;
top: 50%;
background-color: var(--bright_grey);
padding: 4px 10px;
white-space: nowrap;
}
}
}
@media (max-width: 45rem) {
#scroll-container {
inset: 0 0 auto;
height: 4px;
width: 100%;
#indicator {
left: 0;
height: 100%;
}
.heading-indicator {
width: 5px;
height: 4px;
}
}
}
class ScrollProgressIndicator {
constructor() {
this.scrollContainer = document.getElementById("scroll-container");
this.indicator = document.getElementById("indicator");
this.article = document.querySelector("article");
this.headers = this.article.querySelectorAll("h2");
this.breakpoint = 700;
this.currentLayout = null;
this.init();
}
init() {
if (!this.article) {
console.warn("No article element found");
return;
}
this.scrollContainer.style.display = "block";
this.setupEventListeners();
this.update();
}
setupEventListeners() {
document.addEventListener("scroll", () => this.update());
document.addEventListener("DOMContentLoaded", () => this.update());
window.addEventListener("resize", () => this.update());
}
getArticleDimensions() {
const articleRect = this.article.getBoundingClientRect();
const scrollTop = window.scrollY - this.article.offsetTop;
const adjustedScrollTop = Math.max(
0,
Math.min(scrollTop, this.article.scrollHeight)
);
return {
articleTop: articleRect.top + window.scrollY,
articleHeight: this.article.scrollHeight,
articleBottom: articleRect.bottom + window.scrollY,
viewportHeight: window.innerHeight,
viewportWidth: window.innerWidth,
scrollTop: adjustedScrollTop
};
}
isScrolledIntoArticle() {
const { articleTop, articleBottom } = this.getArticleDimensions();
const viewportTop = window.scrollY;
const viewportBottom = viewportTop + window.innerHeight;
return viewportBottom >= articleTop && viewportTop <= articleBottom;
}
createHeaderMarker(header, position, isHorizontal) {
const line = document.createElement("a");
line.href = `#${header.id}`;
line.className = "heading-indicator";
line.setAttribute("title", header.textContent);
if (isHorizontal) {
line.style.left = `${position}%`;
line.style.top = "";
} else {
line.style.top = `${position}%`;
line.style.left = "";
}
return line;
}
resetIndicatorStyles() {
this.indicator.style.width = "";
this.indicator.style.height = "";
this.indicator.style.top = "";
this.indicator.style.left = "";
}
updateIndicator(isHorizontal) {
const {
articleHeight,
viewportHeight,
scrollTop
} = this.getArticleDimensions();
if (isHorizontal) {
const width = (scrollTop / (articleHeight - viewportHeight)) * 100;
const clampedWidth = Math.max(0, Math.min(100, width));
this.indicator.style.width = `${clampedWidth}%`;
this.indicator.style.left = "";
this.indicator.style.top = "";
} else {
const height = (viewportHeight / articleHeight) * 100;
const position = (scrollTop / articleHeight) * 100;
const clampedPosition = Math.max(0, Math.min(100 - height, position));
this.indicator.style.height = `${height}%`;
this.indicator.style.top = `${clampedPosition}%`;
this.indicator.style.left = "";
}
}
updateHeaderMarkers(isHorizontal) {
const { articleHeight, articleTop } = this.getArticleDimensions();
this.scrollContainer
.querySelectorAll(".heading-indicator")
.forEach((marker) => marker.remove());
this.headers.forEach((header) => {
const headerOffset = header.offsetTop - this.article.offsetTop;
const position = (headerOffset / articleHeight) * 100;
const marker = this.createHeaderMarker(header, position, isHorizontal);
this.scrollContainer.appendChild(marker);
});
}
update() {
const isHorizontal = window.innerWidth <= this.breakpoint;
if (this.currentLayout !== isHorizontal) {
this.currentLayout = isHorizontal;
this.resetIndicatorStyles();
this.updateHeaderMarkers(isHorizontal);
}
this.updateIndicator(isHorizontal);
}
}
const scrollIndicator = new ScrollProgressIndicator();
Also see: Tab Triggers