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="content-tabs">
<nav role="tablist">
<a
href="#tab-1"
aria-selected="true"
aria-controls="tabpanel-1"
id="tab-1"
role="tab"
>Lorem ipsum</a
>
<a
href="#tab-2"
aria-selected="false"
aria-controls="tabpanel-2"
id="tab-2"
role="tab"
>Sed ut perspiciatis</a
>
<a
href="#tab-3"
aria-selected="false"
aria-controls="tabpanel-3"
id="tab-3"
role="tab"
>At vero eos et accusamus</a
>
<a
href="#tab-4"
aria-selected="false"
aria-controls="tabpanel-4"
id="tabpanel-4"
role="tab"
>Eu mel impedit</a
>
<a
href="#tab-5"
aria-selected="false"
aria-controls="tabpanel-5"
id="tab-5"
role="tab"
>Noster deseruisse</a
>
<a
href="#tab-6"
aria-selected="false"
aria-controls="tabpanel-6"
id="tab-6"
role="tab"
>Erat concludaturque eam</a
>
<a
href="#tab-7"
aria-selected="false"
aria-controls="tabpanel-7"
id="tab-7"
role="tab"
>Ut dolorem reprimique est</a
>
<a
href="#tab-8"
aria-selected="false"
aria-controls="tabpanel-8"
id="tab-8"
role="tab"
>Falli persequeris</a
>
</nav>
<section aria-live="polite" role="region">
<article id="tabpanel-1" role="tabpanel">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
<br />
</article>
<article id="tabpanel-2" role="tabpanel" hidden>
Sed ut perspiciatis unde omnis iste natus error sit voluptatem
accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae
ab illo inventore veritatis et quasi architecto beatae vitae dicta
sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit
aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos
qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui
dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed
quia non numquam eius modi tempora incidunt ut labore et dolore magnam
aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum
exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex
ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in
ea voluptate velit esse quam nihil molestiae consequatur, vel illum
qui dolorem eum fugiat quo voluptas nulla pariatur?
</article>
<article id="tabpanel-3" role="tabpanel" hidden>
At vero eos et accusamus et iusto odio dignissimos ducimus qui
blanditiis praesentium voluptatum deleniti atque corrupti quos dolores
et quas molestias excepturi sint occaecati cupiditate non provident,
similique sunt in culpa qui officia deserunt mollitia animi, id est
laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita
distinctio. Nam libero tempore, cum soluta nobis est eligendi optio
cumque nihil impedit quo minus id quod maxime placeat facere possimus,
omnis voluptas assumenda est, omnis dolor repellendus. Temporibus
autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe
eveniet ut et voluptates repudiandae sint et molestiae non recusandae.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis
voluptatibus maiores alias consequatur aut perferendis doloribus
asperiores repellat.
</article>
<article id="tabpanel-4" role="tabpanel" hidden>
Eu mel impedit adversarium liberavisse. At viris dignissim inciderint
vix, in quo altera scripserit. Eu nusquam evertitur mei, at sed posse
graeci eleifend. No suas salutatus quo, no vix vitae possim detraxit.
Causae consulatu eum cu.
</article>
<article id="tabpanel-5" role="tabpanel" hidden>
Noster deseruisse ei est, ad sed reque abhorreant. Ad meis ferri cum,
in quando placEu mel impedit adversarium liberavisse. At viris
dignissim inciderint vix, in quo altera scripserit. Eu nusquam
evertitur mei, at sed posse graeci eleifend. No suas salutatus quo, no
vix vitae possim detraxit. Causae consulatu eum cu.
</article>
<article id="tabpanel-6" role="tabpanel" hidden>
erat concludaturque eam, ubique eirmod has ex. At sea quas erroribus,
cu mea prodesset tincidunt. Probo inani no has, id eum vero
reprimique. Te etiam aliquid vis, scripta delenit vix at, utroque
tacimates mnesarchum ad pro. An minimum accusata nec, novum ceteros
adversarium sed ne, id vim elit aeterno. Sed option bonorum maluisset
at, te duo hinc clita admodum, perpetua periculis voluptatibus an mel.
</article>
<article id="tabpanel-7" role="tabpanel" hidden>
Ut dolorem reprimique est. An ius blandit dignissim consectetuer. Ea
sit consulatu persecuti, duo eu quis fabulas. Sale utinam pro ex, sed
ne sale soleat tacimates, quo id sale habemus electram. Qui ad integre
iudicabit.
</article>
<article id="tabpanel-8" role="tabpanel" hidden>
Falli persequeris accommodare an per, te habeo doctus delectus quo.
Autem iisque accumsan sea eu. Ei mazim nobis omnes est, quaestio
maiestatis elaboraret ad his. Vel latine utroque oporteat eu.
</article>
</section>
</section>
body {
background-color: #eee;
}
.content-tabs {
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: space-between;
background-color: #fff;
overflow: hidden;
padding: 10px 25px;
}
.content-tabs *,
.content-tabs *:before,
.content-tabs *:after {
box-sizing: border-box;
}
.content-tabs [role="tablist"] {
display: flex;
flex-wrap: nowrap;
flex: 1 1 100%;
padding: 0;
position: relative;
overflow-x: auto;
background-image:
--indicator-width: 0;
--indicator-left: 0;
/* Shadows */ linear-gradient(
to right,
white,
white
),
linear-gradient(to right, white, white),
/* Shadow covers */
linear-gradient(
to right,
rgba(80, 80, 80, 0.2),
rgba(255, 255, 255, 0)
),
linear-gradient(
to left,
rgba(80, 80, 80, 0.2),
rgba(255, 255, 255, 0)
);
background-position: left center, right center, left center,
right center;
background-repeat: no-repeat;
background-color: white;
background-size: 20px 100%, 20px 100%, 10px 100%, 10px 100%;
/* Opera doesn't support this in the shorthand */
background-attachment: local, local, scroll, scroll;
}
.content-tabs [role="tablist"]::-webkit-scrollbar {
display: none;
}
.content-tabs [role="tablist"] [role="tab"] {
padding: 20px 0;
margin: 0 20px;
border: none;
text-decoration: none;
display: block;
white-space: nowrap;
user-select: none;
font-family: sans-serif;
color: #000;
}
.content-tabs [role="tablist"] [role="tab"][aria-selected="true"] {
font-weight: bold;
}
.content-tabs [role="tablist"] [role="tab"]:first-of-type {
margin-left: 0;
}
.content-tabs [role="tablist"] [role="tab"]:last-of-type {
margin-right: 0;
}
.content-tabs [role="tablist"]:after {
display: inline-block;
position: absolute;
height: 2px;
min-width: var(--indicator-width);
max-width: var(--indicator-width);
bottom: 0;
left: var(--indicator-left);
background-color: red;
content: "";
transition: left 0.8s ease-in-out, min-width 0.8s ease-in-out,
max-width 0.8s ease-in-out;
}
.content-tabs [role="region"] {
display: flex;
flex-direction: column;
flex: 1 1 100%;
}
.content-tabs [role="tabpanel"] {
width: 100%;
overflow: auto;
min-height: 200px;
opacity: 1;
transform: translateX(0);
transition: opacity 0.5s linear, transform 0.4s ease-out;
will-change: opacity, transform;
overflow: auto;
padding: 20px 0;
}
.content-tabs [role="tabpanel"].slided-right,
.content-tabs [role="tabpanel"].slided-left {
opacity: 0;
pointer-events: none;
}
.content-tabs [role="tabpanel"].slided-left {
transform: translateX(-100%);
}
.content-tabs [role="tabpanel"].slided-right {
transform: translateX(100%);
}
/*******************************************************************************/
const _delegate = function (e, t, n, o, r) {
let c;
c = e
? "string" == typeof e || e instanceof String
? document.querySelector(e)
: e
: document.querySelector("body");
let l = t.split(",");
for (var i = 0, s = l.length; i < s; i++)
c.addEventListener(
l[i],
function (e) {
e.target.matches(n) && o(e);
},
r || "scroll" == l[i]
);
};
/*******************************************************************************/
const _beginMomentumTracking = (scroller) => {
_cancelMomentumTracking(scroller);
scroller.scrollingStatus.momentumID = requestAnimationFrame(
function () {
_momentumLoop(scroller);
}
);
};
/*******************************************************************************/
const _cancelMomentumTracking = (scroller) => {
cancelAnimationFrame(scroller.scrollingStatus.momentumID);
};
/*******************************************************************************/
const _momentumLoop = (scroller) => {
scroller.element.scrollLeft += scroller.scrollingStatus.velX;
scroller.scrollingStatus.velX *= 0.95;
if (Math.abs(scroller.scrollingStatus.velX) > 0.5) {
scroller.scrollingStatus.momentumID = requestAnimationFrame(
function () {
_momentumLoop(scroller);
}
);
}
};
/******************************************************************************/
const _setIndicator = function (tabList,tab) {
tabList.style.setProperty(
"--indicator-width",
tab.offsetWidth + "px"
);
tabList.style.setProperty(
"--indicator-left",
tab.offsetLeft + "px"
);
};
/******************************************************************************/
document.querySelectorAll(".content-tabs").forEach((tabs) => {
const panelsContainer = tabs.querySelector('[role="region"]'),
tabList = tabs.querySelector('[role="tablist"]'),
scroller = {
element: tabList,
scrollingStatus: {
isDown: false,
startX: null,
scrollLeft: null,
velX: 0,
momentumID: null,
},
};
/*---------------------------------------------------------------------------*/
tabList.addEventListener("mousedown", function (e) {
scroller.scrollingStatus.isDown = true;
scroller.element.classList.add("scrolling");
scroller.scrollingStatus.startX =
e.pageX - scroller.element.offsetLeft;
scroller.scrollingStatus.scrollLeft = scroller.element.scrollLeft;
_cancelMomentumTracking(scroller);
});
/*---------------------------------------------------------------------------*/
tabList.addEventListener("mouseup", () => {
scroller.scrollingStatus.isDown = false;
scroller.element.classList.remove("scrolling");
_beginMomentumTracking(scroller);
});
/*---------------------------------------------------------------------------*/
tabList.addEventListener("mousemove", (e) => {
if (!scroller.scrollingStatus.isDown) return;
e.preventDefault();
const x = e.pageX - scroller.element.offsetLeft;
const walk = (x - scroller.scrollingStatus.startX) * 3; //scroll-fast
var prevScrollLeft = scroller.element.scrollLeft;
scroller.element.scrollLeft =
scroller.scrollingStatus.scrollLeft - walk;
scroller.scrollingStatus.velX =
scroller.element.scrollLeft - prevScrollLeft;
});
/*---------------------------------------------------------------------------*/
tabList.addEventListener(
"wheel",
(e) => {
_cancelMomentumTracking(scroller);
},
{ passive: true }
);
/*---------------------------------------------------------------------------*/
_delegate(tabs, "click", '[role="tab"][aria-controls]', (event) => {
const tab = event.target;
if (tab.getAttribute("aria-selected") == "true") return false;
if (Math.abs(scroller.scrollingStatus.startX - (event.pageX - scroller.element.offsetLeft)) > 0) return false; //prevent false clicks during dragging
const tabPanelIn = panelsContainer.querySelector(
"#" + tab.getAttribute("aria-controls")
),
tabOut = tabs.querySelector('[aria-selected="true"]'),
tabPanelOut = panelsContainer.querySelector(
"#" + tabOut.getAttribute("aria-controls")
),
indexIn = [...panelsContainer.children].indexOf(tabPanelIn), //index of element relative to container
indexOut = [...panelsContainer.children].indexOf(tabPanelOut),
directionOut = indexIn > indexOut ? "left" : "right",
directionIn = directionOut == "right" ? "left" : "right";
tab.setAttribute("aria-selected", true);
tabOut.setAttribute("aria-selected", false);
_setIndicator(tabList,tab); //set after appliyng aria-selected styles fo correct width & position
tabPanelIn.classList.remove("slided-left", "slided-right");
tabPanelIn.classList.add("slided-" + directionIn);
tabPanelOut.addEventListener(
"transitionend",
() => {
tabPanelOut.setAttribute("hidden", true);
tabPanelOut.classList.remove("slided-" + directionOut);
tabPanelIn.removeAttribute("hidden");
tab.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
});
setTimeout(() => {
tabPanelIn.classList.remove("slided-" + directionIn);
}, 1);
},
{ once: true }
);
tabPanelOut.classList.add("slided-" + directionOut);
tabs.dispatchEvent(
new CustomEvent("content-tabs:selected", {
detail: {
tabId: tab.getAttribute("aria-controls"),
tab: tab,
tabs: tabs,
},
})
);
});
/*---------------------------------------------------------------------------*/
_setIndicator(tabList, tabs.querySelector('[role="tab"]')); //first one set on initialization
tabs.dispatchEvent(
new CustomEvent("content-tabs:initialized", {
detail: { tabs: tabs },
})
);
});
Also see: Tab Triggers