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.
<body style="--color: hsl(340, 60%, 30%)">
<!-- This element will be animated while transitioning from one tab to another -->
<div class="background"></div>
<!-- List of tabs. Set the desired color for each tab in their inline style attribute with the --color property -->
<div class="tab-list">
<button class="tab on" id="tab1"
style="--color: hsl(340, 60%, 30%)">
First tab
</button>
<button class="tab" id="tab2"
style="--color: hsl(160, 60%, 25%)">
Tab 2
</button>
<button class="tab" id="tab3"
style="--color: hsl(220, 60%, 35%)">
Tab the 3rd
</button>
</div>
<!-- List of sections. Each one is linked to its tab through the data-tab="tab id" attribute -->
<div class="section-list">
<section data-tab="tab1" class="on">
<p>Pokem ipsum dolor sit amet Vanillish Growl Rufflet Earthquake Torkoal Drifloon. V for victory Noctowl Terrakion Ludicolo Lickilicky Togekiss Elekid. Misty Flareon you teach me and I'll teach you Mantine Exeggutor Rayquaza Ditto. Tail Whip Strength Registeel Dunsparce Pinsir Houndoom Yamask. Consectetur adipisicing elit Poochyena Kakuna Phanpy Gigalith Lairon Carnivine.
<p>Sonic Boom Leafeon Vanilluxe deserunt mollit Espeon Electrode Togepi. Leech Seed Spinda excepteur sint occaecat cupidatat non proident Sandile Shellder Ninetales Virizion. Team Rocket Omastar Swanna Lunatone Cacnea Combee Froslass. Anim id est laborum Gorebyss Vaporeon Bonsly Missingno Thundurus Flygon. Fog Badge Ice Berry make it double Fighting Durant Slowking Mandibuzz.
<p>Johto Vigoroth Luxio Ash Ketchum Castform Bellsprout Monferno. Water Primeape Chingling Leech Seed Arcanine Houndour Dustox. Hoenn Lairon Gible Shinx Bastiodon Stantler Ponyta. Consectetur adipisicing elit Earth Badge Youngster wants to fight Chikorita Magnezone Treecko Kecleon. Plain Badge Scolipede Reshiram Minun Shiftry Porygon Remoraid.
<p>This Pokem ipsum text comes from pokemipsum.com
</section>
<section data-tab="tab2">
<p>Pokem ipsum dolor sit amet Golett Reshiram Roselia Dragon Rage Staryu Escape Rope. Gotta catch 'em all Swellow Cranidos Blaziken Soda Pop Officer Jenny Dusclops. Pokemon 4Ever Shuppet Bayleef Salamence Wormadam Cherrim Geodude. Water Bisharp Lickilicky Teddiursa Cresselia Fearow Litwick. Viridian City Tepig Barboach Pikachu Krookodile Mystery Gift Gloom.
<p>Strength Camerupt Scolipede Exeggutor Grumpig Kyurem Shaymin. Pewter City Camerupt Skorupi Growlithe Hitmonlee Swadloon Piplup. Hive Badge Persian Beartic Galvantula Boldore Typhlosion Vanillish. Water Gun Sceptile Ultra Ball Articuno Swellow Mime Jr Beldum. Dragon Pewter City Rotom Whirlipede Pupitar Tauros Mismagius.
<p>Mewtwo Strikes Back Luvdisc Trubbish Alomomola Woobat Blizzard Starmie. Psychic Woobat Flying Ash Mandibuzz Koffing Volcarona. Fog Badge duis aute irure dolor in reprehenderit Reuniclus Cascade Badge Celadon Department Store Celadon City Torkoal. Ice Escavalier Noctowl Hidden Machine Mr. Mime Druddigon you teach me and I'll teach you. Normal Samurott Sableye Zephyr Badge Phanpy Budew Carnivine.
</section>
<section data-tab="tab3">
<p>Pokem ipsum dolor sit amet Poochyena Cryogonal Omanyte Corphish but nothing happened Body Slam. Ut labore et dolore magna aliqua Reuniclus Gothorita Lopunny Archen Water Gun Galvantula. Storm Badge Flamethrower Missingno Dark Furret Zweilous Pansear. Teleport Cloyster Cofagrigus Flamethrower Krookodile Burnt Berry Water Gun. Ut labore et dolore magna aliqua Whirlipede Darkrai Nidoking Nosepass Lickilicky Vanilluxe.
<p>Sed do eiusmod tempor incididunt Teleport Girafarig Ludicolo Blaziken Keldeo Dragonair. Misty Golett Lairon Carvanha Marill Steelix Togekiss. Glitch City Dragonite Helix Fossil Charizard Ludicolo Razor Leaf Grovyle. Bubble Swanna Rhyperior Professor Elm Pidgey Giratina Nidoran. Splash Suicune Combusken Gothorita Simisear Officer Jenny Eelektross.
<p>Ut enim ad minim veniam Teddiursa Misdreavus Raticate Lunatone Diglett Escape Rope. Kanto Mewtwo Strikes Back Mewtwo Strikes Back Empoleon Psychic Magnemite Giratina. Thunder Badge Pignite Slash Doduo Cerulean City Electrode Skorupi. Dragon Turtwig Manectric Palpitoad James Swadloon Azurill. Harden Smeargle Drifblim Fire Sandslash Deerling the power that's inside.
</section>
</div>
</body>
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: #ddd;
font-size: 1.1rem;
background: var(--color, #222);
}
body {
display: grid;
grid-template-rows: auto 1fr;
}
/* Animated background during transition */
.background {
width: 100vw;
height: 100vh;
position: fixed;
top: 0;
left: 0;
transform-origin: top left;
}
.tab-list {
padding: 1rem 0;
display: flex;
flex-direction: row;
justify-content: space-evenly;
grid-row: 1 / 2;
max-width: 25rem;
}
.tab {
/* Resetting default button appearance */
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
border: none;
background: none;
/* Customize text */
color: inherit;
font-size: inherit;
/* Customize size */
padding: 0 1rem;
height: 3rem;
position: relative;
z-index: 1;
cursor: pointer;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}
/* Tab background color, expands on hover */
.tab::before {
content: '';
/* Fills the whole tab */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: var(--color);
z-index: -1;
transition: transform .12s ease-out;
}
/* Expand background color of the tab on hover, as a hint it can expand further by clicking it */
.tab:hover::before {
transform: scale(1.2);
}
/* Make currently active tab unclickable */
.tab.on {
cursor: auto;
pointer-events: none;
}
.tab:focus:hover {
outline: none;
}
/* Tab text underline */
.tab::after {
content: '';
display: block;
background-color: white;
/* Size and position */
width: calc(100% - 2rem);
height: 2px;
position: absolute;
bottom: .5rem;
left: 1rem;
z-index: 3;
/* Ready to animate on tab activation */
transform-origin: top left;
transform: scaleX(0);
transition: transform .2s ease-out;
}
/* Display the underline on active tab */
.tab.on::after {
transform: scaleX(1);
}
/* Change side of underline origin */
.tab.fromright::after {
transform-origin: top right;
}
.section-list {
overflow-y: auto;
padding: 0 .5rem;
grid-row: 2 / 3;
}
/* Hide sections by default */
section {
display: none;
width: 100%;
max-width: 42rem;
}
/* Display section corresponding to active tab */
section.on {
display: block;
}
const allTabs = document.querySelectorAll('.tab');
let futureTab;
function changeTab(id) {
futureTab = id;
const tab = document.getElementById(id);
const section = document.querySelector('[data-tab='+id+']');
const background = document.querySelector('.background');
const newColor = tab.style.getPropertyValue('--color');
const previousTab = document.querySelector('.tab.on');
// Do nothing if we're already on this tab
if (tab.classList.contains('on')) return;
// Checking if the new tab is on the left or right of the currently activated one, to animate the underline
if (previousTab.id.replace('tab', '') > id.replace('tab', '')) {
tab.classList.add('fromright');
previousTab.classList.remove('fromright');
} else {
tab.classList.remove('fromright');
previousTab.classList.add('fromright');
}
// Resetting all tabs and sections
allTabs.forEach(t => {
t.classList.remove('on');
document.querySelector('section[data-tab='+t.id+']').classList.remove('on');
});
// Activating new tab
tab.classList.add('on');
tab.blur();
// Calculating the starting position of the expanding background color
const tabPos = tab.getBoundingClientRect();
const scaleX = (tabPos.right - tabPos.left) / window.innerWidth;
const scaleY = (tabPos.bottom - tabPos.top) / window.innerHeight;
background.style.backgroundColor = newColor;
// Animating the expanding background color
const keyframes = [
{ transform: 'translate3D(' + tabPos.left + 'px, ' + tabPos.top + 'px, 0) scale(' + scaleX + ', ' + scaleY + ')' },
{ transform: 'translate3D(0, 0, 0) scale(1, 1)'}
];
const coloration = background.animate(
keyframes, {
easing: 'cubic-bezier(0.2, 0.45, 0.3, 1)',
duration: 400,
fill: 'forwards'
});
// After the animation...
coloration.addEventListener('finish', () => {
// If another tab is opening already, do nothing
if (futureTab != id) return;
// Else, display and animate the new section
document.body.style.setProperty('--color', newColor);
section.classList.add('on');
section.animate([
{ opacity: '0', transform: 'translate3D(0, 1rem, 0)' },
{ opacity: '1', transform: 'translate3D(0, 0, 0)' }
], {
easing: 'cubic-bezier(0.0, 0.0, 0.2, 1)',
duration: 150,
fill: 'forwards'
});
});
}
// Detect clicks on all tabs
allTabs.forEach(t => {
t.addEventListener('click', () => changeTab(t.id));
});
Also see: Tab Triggers