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.
<section class="scroll-snap-panel" role="tablist" aria-label="users and groups">
<div class="tabs-container container">
<button data-target="people-tab" role="tab" aria-selected="true" class="people tab active" aria-controls="people-tab">People</button>
<button data-target="groups-tab" role="tab" aria-selected="false" class="groups tab" aria-controls="groups-tab" tabindex="-1">Groups</button>
</div>
<div class="scroll-container container">
<div class="scroll-list people" role="tabpanel" tabindex="0" id="people-tab" aria-label="people">
<ul>
<li>
<img src="http://placeimg.com/80/80/people?1" class="scroll-list-image" alt="" />
<div>
<h2>John Doe</h2>
<small>Developer</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?8" class="scroll-list-image" alt="" />
<div>
<h2>Jane Doe</h2>
<small>Front-end developer</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?3" class="scroll-list-image" alt="" />
<div>
<h2>Alfred Horus</h2>
<small>Marketeer</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?4" class="scroll-list-image" alt="" />
<div>
<h2>Dane Dorenski</h2>
<small>Director</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?5" class="scroll-list-image" alt="" />
<div>
<h2>Bass Strongarm</h2>
<small>Functional analyst</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?6" class="scroll-list-image" alt="" />
<div>
<h2>Frederick Fancis</h2>
<small>Technical analyst</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?7" class="scroll-list-image" alt="" />
<div>
<h2>Elise Berend</h2>
<small>Ninja</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?8" class="scroll-list-image" alt="" />
<div>
<h2>Keith Wheelbarrow</h2>
<small>Coffee snob</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?9" class="scroll-list-image" alt="" />
<div>
<h2>Oliver Greenwood</h2>
<small>IT-support</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?10" class="scroll-list-image" alt="" />
<div>
<h2>Frank Demaska</h2>
<small>Team lead</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?11" class="scroll-list-image" alt="" />
<div>
<h2>Frank Dorangeorgio</h2>
<small>Sales</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?12" class="scroll-list-image" alt="" />
<div>
<h2>Marcus Webbers</h2>
<small>OG</small>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/people?13" class="scroll-list-image" alt="" />
<div>
<h2>Mary Hercules</h2>
<small>Geek</small>
</div>
</li>
</ul>
</div>
<div class="scroll-list groups" role="tabpanel" tabindex="-1" id="groups-tab" aria-label="groups" hidden="hidden">
<ul>
<li>
<img src="http://placeimg.com/80/80/arch?1" class="scroll-list-image" alt="" />
<div>
<h2>Developers</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?2" class="scroll-list-image" alt="" />
<div>
<h2>Analysts</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?3" class="scroll-list-image" alt="" />
<div>
<h2>IT-support</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?4" class="scroll-list-image" alt="" />
<div>
<h2>Designers</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?5" class="scroll-list-image" alt="" />
<div>
<h2>Project managers</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?6" class="scroll-list-image" alt="" />
<div>
<h2>Sales</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?6" class="scroll-list-image" alt="" />
<div>
<h2>Marketing</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?7" class="scroll-list-image" alt="" />
<div>
<h2>Team leads</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?7" class="scroll-list-image" alt="" />
<div>
<h2>Ninjas</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?8" class="scroll-list-image" alt="" />
<div>
<h2>Foodies</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?9" class="scroll-list-image" alt="" />
<div>
<h2>Hipsters</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?10" class="scroll-list-image" alt="" />
<div>
<h2>Geeks</h2>
</div>
</li>
<li>
<img src="http://placeimg.com/80/80/arch?11" class="scroll-list-image" alt="" />
<div>
<h2>Coffee snobs</h2>
</div>
</li>
</ul>
</div>
</div>
</section>
:root {
--container-width: 480px;
--groups-bg: #8B008B;
--people-bg: #4B0082;
--groups-color: #FFF;
--people-color: #F0FFFF;
--main-bg: #FFFAF0;
}
.container {
display: grid;
}
.scroll-snap-panel {
width: 100%;
margin: 0 auto;
box-shadow: rgba(0, 0, 0, 0.3) 0px 10px 50px;
max-width: var(--container-width);
}
/* the scroll magic */
.scroll-container {
position: relative;
grid-template-columns: repeat(2, var(--container-width));
-webkit-overflow-scrolling: touch;
overflow-x: scroll;
scroll-snap-type: x mandatory;
scroll-behavior: smooth;
max-height: 85vh;
border: 1px solid #CCC;
border-top: 0;
}
.scroll-list {
display: block;
width: 100%;
height: 80vh;
margin: 0;
padding: 0;
list-style: none;
scroll-snap-align: start;
scroll-snap-type: y proximity;
overflow-y: scroll;
}
.scroll-list > ul {
list-style: none;
margin: 0;
padding: 0;
}
.scroll-list li {
display: grid;
grid-template-columns: 80px 1fr;
gap: 20px;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #aaa;
scroll-snap-align: start;
}
/* styling of the inside of the list */
.scroll-list > li:last-child {
border: 0;
}
.scroll-list-image {
border-radius: 50%;
}
.people {
background: var(--people-bg);
color: var(--people-color);
}
.groups {
background: var(--groups-bg);
color: var(--groups-color);
}
/* some styling of the tabs */
.tabs-container {
grid-template-columns: repeat(2, 1fr);
margin-top: 20px;
}
.tab {
display: block;
margin: 0;
padding: 16px 8px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
color: #FFF;
border: 1px solid #CCC;
border-left: 0;
cursor: pointer;
}
.tab:first-child {
border-top-left-radius: 5px;
border: 1px solid #CCC;
}
.tab:last-child {
border-top-right-radius: 5px;
}
.tab.active {
border-bottom-color: transparent;
}
/* some basic styling (added to the bottom for convenience) */
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
font-family: 'Lato', sans-serif;
font-size: 1.2rem;
line-height: 1.5;
color: #FEFEFE;
background: var(--main-bg);
}
img {
max-width: 100%;
margin: 0;
}
h2 {
margin: 0;
font-family: 'Merriweather', serif;
font-size: 1.5rem;
line-height: 1.2;
}
/*
using the scroll position to calculate the active state of tabs instead of click
Uses the The tabs.js for better accessibility
https://www.w3.org/TR/wai-aria-practices/examples/tabs/tabs-1/tabs.html
*/
const scrollContainer = document.querySelector(".scroll-container");
const scrollLists = document.querySelectorAll(".scroll-list");
const tablist = document.querySelectorAll('[role="tablist"]')[0];
let tabs;
let panels;
generateArrays();
function generateArrays() {
tabs = document.querySelectorAll('[role="tab"]');
panels = document.querySelectorAll('[role="tabpanel"]');
}
// For easy reference
var keys = {
end: 35,
home: 36,
left: 37,
up: 38,
right: 39,
down: 40
};
// Add or subtract depending on key pressed
var direction = {
37: -1,
38: -1,
39: 1,
40: 1
};
// Bind listeners
for (var i = 0; i < tabs.length; ++i) {
addListeners(i);
}
function addListeners(index) {
tabs[index].addEventListener("click", clickEventListener);
tabs[index].addEventListener("keydown", keydownEventListener);
tabs[index].addEventListener("keyup", keyupEventListener);
// Build an array with all tabs (<button>s) in it
tabs[index].index = index;
}
// When a tab is clicked, activateTab is fired to activate it
function clickEventListener(event) {
var tab = event.target;
activateTab(tab, false);
}
// When a tab is clicked, activateTab is fired to activate it
function clickEventListener(event) {
var tab = event.target;
activateTab(tab, false);
}
// Handle keydown on tabs
function keydownEventListener(event) {
var key = event.keyCode;
switch (key) {
case keys.end:
event.preventDefault();
// Activate last tab
activateTab(tabs[tabs.length - 1]);
break;
case keys.home:
event.preventDefault();
// Activate first tab
activateTab(tabs[0]);
break;
// Up and down are in keydown
// because we need to prevent page scroll >:)
case keys.up:
case keys.down:
determineOrientation(event);
break;
}
}
// Handle keyup on tabs
function keyupEventListener(event) {
var key = event.keyCode;
switch (key) {
case keys.left:
case keys.right:
determineOrientation(event);
break;
case keys.delete:
determineDeletable(event);
break;
}
}
// When a tablist’s aria-orientation is set to vertical,
// only up and down arrow should function.
// In all other cases only left and right arrow function.
function determineOrientation(event) {
var key = event.keyCode;
var vertical = tablist.getAttribute("aria-orientation") == "vertical";
var proceed = false;
if (vertical) {
if (key === keys.up || key === keys.down) {
event.preventDefault();
proceed = true;
}
} else {
if (key === keys.left || key === keys.right) {
proceed = true;
}
}
if (proceed) {
switchTabOnArrowPress(event);
}
}
// Either focus the next, previous, first, or last tab
// depending on key pressed
function switchTabOnArrowPress(event) {
var pressed = event.keyCode;
for (var x = 0; x < tabs.length; x++) {
tabs[x].addEventListener("focus", focusEventHandler);
}
if (direction[pressed]) {
const target = event.target;
if (target.index !== undefined) {
if (tabs[target.index + direction[pressed]]) {
tabs[target.index + direction[pressed]].focus();
} else if (pressed === keys.left || pressed === keys.up) {
focusLastTab();
} else if (pressed === keys.right || pressed == keys.down) {
focusFirstTab();
}
}
}
}
// Make a guess
function focusFirstTab() {
tabs[0].focus();
}
// Make a guess
function focusLastTab() {
tabs[tabs.length - 1].focus();
}
// Activates any given tab panel
function activateTab(tab, setFocus) {
setFocus = setFocus || true;
// Deactivate all other tabs
deactivateTabs();
// Remove tabindex attribute
tab.removeAttribute("tabindex");
// Set the tab as selected
tab.setAttribute("aria-selected", "true");
// Get the value of aria-controls (which is an ID)
var controls = tab.getAttribute("aria-controls");
// New: set the container to scroll to show active state
const target = document.getElementById(tab.dataset.target).offsetLeft;
scrollContainer.scrollLeft = target;
// Set focus when required
if (setFocus) {
tab.focus();
}
}
// Deactivate all tabs and tab panels
function deactivateTabs() {
for (let t = 0; t < tabs.length; t++) {
tabs[t].setAttribute("tabindex", "-1");
tabs[t].setAttribute("aria-selected", "false");
tabs[t].removeEventListener("focus", focusEventHandler);
}
}
function focusEventHandler(event) {
var target = event.target;
setTimeout(checkTabFocus, 0, target);
}
// Only activate tab on focus if it still has focus after the delay
function checkTabFocus(target) {
var focused = document.activeElement;
if (target === focused) {
activateTab(target, false);
}
}
/* when the scrollcontainer is scrolled horizontally, check the current scrollposition is the same as the left offset, and set the tab active when that's the case */
scrollContainer.addEventListener("scroll", () => {
let scrollPos = scrollContainer.scrollLeft;
scrollLists.forEach((list) => {
let listId = list.id;
let listButton = document.querySelector(`.tab[data-target="${listId}"]`);
if (scrollPos === list.offsetLeft) {
[...listButton.parentElement.children].forEach((sib) => {
sib.classList.remove("active");
});
listButton.classList.add("active");
deactivateTabs();
activateTab(listButton);
list.tabIndex = "0";
} else {
list.tabIndex = "-1";
}
});
});
Also see: Tab Triggers