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.
<h1>Some countries of Asia</h1>
<div class="container with-sidebar">
<ul id="tablist" role="tablist" class="tablist sidebar">
<li role="presentation">
<a class="tab with-sidebar" role="tab" href="#panel1" id="tab1">
<img loading="lazy" class="flag sidebar" src="https://cdn.countryflags.com/thumbs/mongolia/flag-button-round-250.png" alt="" />
<span class="description content">Mongolia</span>
</a>
</li>
<li role="presentation">
<a class="tab with-sidebar" role="tab" href="#panel2" id="tab2">
<img loading="lazy" class="flag sidebar" src="https://cdn.countryflags.com/thumbs/kyrgyzstan/flag-button-round-250.png" alt="" />
<span class="description content">Kyrgystan</span>
</a>
</li>
<li role="presentation">
<a class="tab with-sidebar" role="tab" href="#panel3" id="tab3">
<img loading="lazy" src="https://cdn.countryflags.com/thumbs/myanmar/flag-button-round-250.png" alt="" class="flag sidebar" />
<span class="description content">Myanmar</span></a>
</li>
<li role="presentation">
<a class="tab with-sidebar" role="tab" href="#panel4" id="tab4">
<img loading="lazy" src="https://cdn.countryflags.com/thumbs/uzbekistan/flag-button-round-250.png" alt="" class="flag sidebar" />
<span class="description content">Uzbekistan</span></a>
</li>
</ul>
<div class="tabpanel-container content">
<section class="tabpanel" role="tabpanel" tabindex="0" id="panel1" aria-labelledby="tab1">
<div class="flow">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
<a href="#panel1">ornare eros egestas</a>
lectus lacinia, ut luctus turpis egestas. Aliquam erat volutpat.
Duis sed nisi metus. Ut sodales dapibus efficitur.
</p>
<p>
Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
pretium. Integer dictum diam in sem fringilla lobortis.
Pellentesque gravida dui id felis hendrerit, a tristique lorem
pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
luctus tellus.
</p>
</div>
<figure>
<img loading="lazy" src="https://images.unsplash.com/photo-1589654615616-6756a5653100?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="A mongolian brown grass field surrounded by snow mountains" />
</figure>
</section>
<section class="tabpanel" role="tabpanel" tabindex="0" id="panel2" aria-labelledby="tab2">
<div class="flow">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
ornare eros egestas lectus lacinia, ut luctus turpis egestas.
Aliquam erat volutpat. Duis sed nisi metus. Ut sodales dapibus
efficitur.
</p>
<p>
Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
pretium. Integer dictum diam in sem fringilla lobortis.
Pellentesque gravida dui id felis hendrerit, a tristique lorem
pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
luctus tellus.
</p>
</div>
<figure>
<img loading="lazy" src="https://images.unsplash.com/photo-1567886793773-d59bfd7f0aa6?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1032&q=80" alt="A lake in a forest surrounded by mountains" />
</figure>
</section>
<section class="tabpanel" role="tabpanel" tabindex="0" id="panel3" aria-labelledby="tab3">
<div class="flow">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
ornare eros egestas lectus lacinia, ut luctus turpis egestas.
Aliquam erat volutpat. Duis sed nisi metus. Ut sodales dapibus
efficitur.
</p>
<p>
Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
pretium. Integer dictum diam in sem fringilla lobortis.
Pellentesque gravida dui id felis hendrerit, a tristique lorem
pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
luctus tellus.
</p>
</div>
<figure>
<img loading="lazy" src="https://images.unsplash.com/photo-1584897356466-858d9b6c53d1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80" alt="A temple in Myanmar with a lot of hot air balloons in the background" />
</figure>
</section>
<section class="tabpanel" role="tabpanel" tabindex="0" id="panel4" aria-labelledby="tab4">
<div class="flow">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
ornare eros egestas lectus lacinia, ut
<a href="#panel4">luctus turpis egestas</a>. Aliquam erat
volutpat. Duis sed nisi metus. Ut sodales dapibus efficitur.
</p>
<p>
Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
pretium. Integer
<a href="#panel4">dictum diam in sem</a> fringilla lobortis.
Pellentesque gravida dui id felis hendrerit, a tristique lorem
pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
luctus tellus.
</p>
</div>
<figure>
<img loading="lazy" src="https://images.unsplash.com/photo-1557841621-d9f6673405ed?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="A front view of a temple in Uzbekistan" />
</figure>
</section>
</div>
</div>
:root {
--bg: hsl(239deg 12% 92%);
--component-bg: hsl(0deg 0% 85%);
--accent: royalblue;
}
/* Css reset https://piccalil.li/blog/a-modern-css-reset/ */
*,
*::before,
*::after {
box-sizing: border-box;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
body,
h1,
p,
figure {
margin: 0;
}
ul[role="tablist"] {
list-style: none;
padding-left: 0;
}
html:focus-within {
scroll-behavior: smooth;
}
body {
min-height: 100vh;
text-rendering: optimizeSpeed;
line-height: 1.5;
}
a:not([class]) {
text-decoration-skip-ink: auto;
}
img,
picture {
max-width: 100%;
display: block;
}
@media (prefers-reduced-motion: reduce) {
html:focus-within {
scroll-behavior: auto;
}
*,
*::before,
*::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}
/* Layout */
.flow > * + * {
margin-top: var(--flow-space, 1em);
}
.with-sidebar {
display: flex;
flex-wrap: wrap;
max-width: var(--max-width, 100%);
gap: var(--gap, 0em);
margin-inline: auto;
}
.sidebar {
flex-basis: var(--sidebar-width, 9rem);
flex-grow: 1;
}
.content {
display: grid;
place-items: center;
flex-basis: 0;
flex-grow: 999;
min-width: var(--content-width, min(100%, 43.75rem));
}
/* Global styles */
body {
background-color: var(--bg);
}
h1 {
text-align: center;
margin-block: 0.5em;
color: var(--accent);
}
p {
max-width: 80ch;
}
a:not([class]):focus {
outline: 2px solid var(--accent);
outline-offset: 0.1875rem;
border-radius: 0.3125rem;
}
[role="tabpanel"]:focus {
outline: 4px solid var(--accent);
}
/* Component */
.container {
max-width: 110ch;
align-items: start;
background-color: var(--component-bg);
}
.tablist {
display: flex;
flex-wrap: wrap;
margin: 0.2em;
gap: 1em 0.2em;
height: 100%;
}
.tablist li {
flex: 1 1 4.375rem;
}
.tab {
--sidebar-width: 2rem;
--content-width: 60%;
--gap: 0.1em 0.5em;
padding: 0.5em 0.2em;
text-decoration-color: transparent;
border: 2px solid transparent;
background-color: whitesmoke;
color: #121212;
font-size: clamp(0.88rem, calc(0.78rem + 0.49vw), 1.13rem);
}
.tab[aria-selected="true"] {
background-color: var(--component-bg);
}
.tab:focus-visible {
background-color: var(--accent);
color: whitesmoke;
outline: 0.2em solid transparent;
}
.flag {
margin-inline: auto;
aspect-ratio: 1;
max-width: 2rem;
height: auto;
border-radius: 50%;
}
.description {
font-weight: 500;
}
.tabpanel-container {
position: relative;
min-height: 50vh;
font-size: clamp(1rem, calc(0.93rem + 0.37vw), 1.19rem);
background-color: hsl(239deg 12% 92%);
border: 0.2em solid var(--component-bg);
isolation: isolate;
}
.tabpanel {
display: flex;
position: relative;
flex-wrap: wrap;
align-items: center;
gap: 1em;
padding: 1.5rem;
background-color: var(--bg);
}
.tabpanel:nth-child(even) {
flex-direction: row-reverse;
}
.tabpanel > * {
flex: 1 1 20rem;
}
.tabpanel:not(:target):not(.visible) {
display: none;
}
const TABLIST = document.querySelector("#tablist");
const TABS = [...TABLIST.querySelectorAll(".tab")];
const TABPANELS = [...document.querySelectorAll(".tabpanel")];
const showActivePanel = (element) => {
const selectedId = element.id;
TABPANELS.forEach((e) => {
e.hidden = "true";
});
const activePanel = document.querySelector(
`[aria-labelledby="${selectedId}"]`
);
activePanel.removeAttribute("hidden");
};
const setSelectedTab = (element) => {
const selectedId = element.id;
TABPANELS[0].classList.remove("visible");
TABS.forEach((e) => {
const id = e.getAttribute("id");
if (id === selectedId) {
e.removeAttribute("tabindex", "0");
e.setAttribute("aria-selected", "true");
} else {
e.setAttribute("tabindex", "-1");
e.setAttribute("aria-selected", "false");
}
});
};
const createArrowNavigation = () => {
const firstTab = TABS[0];
const lastTab = TABS[TABS.length - 1];
TABS.forEach((element) => {
element.addEventListener("keydown", function (e) {
if ((e.keyCode || e.which) === 38 || (e.keyCode || e.which) === 37) {
if (element == firstTab) {
e.preventDefault();
lastTab.focus();
} else {
e.preventDefault();
const focusableElement = TABS.indexOf(element) - 1;
TABS[focusableElement].focus();
}
} else if (
(e.keyCode || e.which) === 40 ||
(e.keyCode || e.which) === 39
) {
if (element == lastTab) {
e.preventDefault();
firstTab.focus();
} else {
e.preventDefault();
const focusableElement = TABS.indexOf(element) + 1;
TABS[focusableElement].focus();
}
}
});
});
};
const determineTabindex = () => {
TABPANELS.forEach((element) => {
const focusableElements = element.querySelectorAll(
'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled]), details:not([disabled]), summary:not(:disabled)'
).length;
focusableElements
? element.setAttribute("tabindex", "-1")
: element.setAttribute("tabindex", "0");
});
};
const handleClick = () => {
TABS.forEach((element) => {
element.addEventListener("click", function () {
setSelectedTab(element);
showActivePanel(element);
});
});
TABS.forEach((element) => {
element.addEventListener("keydown", function (e) {
if ((e.keyCode || e.which) === 32) {
setSelectedTab(element);
showActivePanel(element);
element.click();
}
});
});
};
const activateFirstPanel = () => {
TABS[0].setAttribute("tabindex", "0");
TABS[0].setAttribute("aria-selected", "true");
TABPANELS[0].classList.add("visible");
};
const checkInitialSelectedTab = () => {
const targetedTabPanel = document
.querySelector(".tabpanel:target")
.getAttribute("aria-labelledby");
const selectedTab = document.querySelector(`#${targetedTabPanel}`);
selectedTab.setAttribute("aria-selected", "true");
selectedTab.removeAttribute("tabindex");
};
const handleInitialState = () => {
TABS.forEach((e) => {
e.setAttribute("tabindex", "-1");
e.setAttribute("aria-selected", "false");
});
window.location.href.indexOf("#panel") === -1
? activateFirstPanel()
: checkInitialSelectedTab();
determineTabindex();
};
handleClick();
createArrowNavigation();
window.onload = handleInitialState;
Also see: Tab Triggers