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.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Libre+Franklin:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">
<h1>Using the <span><details></span> tag to create tabs</h1>
<div class="details-tabs">
<details class="details-item" open> <!-- default first "tab" to open -->
<summary class="details-tab">The details tag</summary>
<div class="details-content">
<p>Imagine an <strong>accordion-like element</strong> with <strong>native keyboard support</strong> that doesn't require javascript for control.</p>
<p>Enter the details tag.</p>
<details>
<summary>The details tag</summary>
<p>This is a details tag with content</p>
</details>
<p>The details tag gives us a simple accordion with no scripts—that's great! But it feels like it could be something more...imagine if it could do tabs.</p>
</div>
</details>
<details class="details-item">
<summary class="details-tab">What about tabs?</summary>
<div class="details-content">
<p>Tabs and accordions work in technically the same way, although tabs typically require a shared parent container and containers to build out the tabs and content separate from each other.</p>
<p>Interactivity is another difference: the ways accordions or tabs are toggled. With accordions it's common to be able to open and close them individually, but with tabs there must always be one, and only one, open.</p>
<p>At first glance there doesn't seem to be a way to make this work. The structure isn't right. The layout isn't right. The functionality isn't quite right. But each of those problems can be solved if we look a bit closer.</p>
</div>
</details>
<details class="details-item">
<summary class="details-tab">CSS trickery</summary>
<div class="details-content">
<p>With a little help from the <strong>display: contents;</strong> property, we can transform a group of discrete details tags with tab and content area elements as children, into a group of the tab and content area elements from all the details elements mixed together.</p>
<p>Say you had 3 small buckets of red and blue balls. The buckets can interact with each other or bang together, but the balls inside each bucket can't interact with the balls from the other buckets.</p>
<p>You could think of what we are doing like pouring all of the small buckets into a larger bucket. Now all of the balls can interact with each other and we can organize them by color in the bigger bucket. With display: contents; it's as if the details tag isn't there at all—at least visually.</p>
<p>Now that all the tab and content area elements can interact with each other in the flow of the page, we can use <strong>display: flex;</strong> and <strong>order</strong> to make the tabs flow nicely but also be separate from the content area.</p>
<p>And that's what you see here! Open up the inspector and have a look.</p>
</div>
</details>
<details class="details-item">
<summary class="details-tab">UX patches with javascript</summary>
<div class="details-content">
<p>So you might have thought we could get by without any JS, but we need it a little. Luckily it's the type of script that you can write once and reuse without reconfiguration.</p>
<p>The default functionality of the details tab means that it can be toggled open or closed. That's not what we want for tabs. Each details tag must be manually closed and then another opened. We want a tab to stay open and then close when another is picked.</p>
<p>By watching for attribute changes and blocking keyboard and mouse input from toggling a details tab when a tab is open, we can emulate the expected tab behavior.</p>
<p>Then it's just a matter of toggling the sibling details tags when another is selected.</p>
</div>
</details>
</div>
body {
font-family: "Libre Franklin", Arial, sans-serif;
max-width: 50rem;
margin: 4rem auto;
background: #16262c;
color: #ddd;
padding: max( calc(50vh - 22rem), 1rem ) 1rem 1rem;
font-size: 18px;
}
h1 {
margin-bottom: 2em;
text-align: center;
color: #fff;
span {
color: #77b728;
}
}
p {
color: #ffffff7f;
font-size: 1.2rem;
line-height: 1.66;
font-weight: 300;
margin: 1rem 0 1.5rem;
strong {
color: #ddd;
font-weight: 700;
}
}
.details-tab {
padding: 0.5rem 1rem;
font-size: 1.1rem;
color: #ffffffcc;
font-weight: 600;
display: block;
order: 0; // tabs come first
background: #ffffff13;
border-radius: 1.5rem;
margin-right: 0.35rem;
margin-bottom: 0.5rem;
cursor: pointer;
border: 2px solid transparent;
&:hover, &:focus {
border-color: #fff;
}
&::-webkit-details-marker {
display: none; // hide the default arrow icon
}
}
.details-tabs {
position: relative;
justify-content: center; // center the tabs
display: flex;
flex-wrap: wrap;
}
.details-content {
order: 1; // content comes after tabs
padding: 1rem;
width: 100%; // make the content sit on it's own row
details {
summary {
font-weight: 600;
}
margin: 1.5rem;
}
}
.details-item {
display: contents;
&[open] {
& > .details-tab {
background: #fff;
color: #333;
}
}
}
// new syntax for chrome etc
// check support, if so apply style
@supports selector(::details-content) {
.details-item[open]::details-content {
display: contents;
}
}
// $(selector).attrchange(): a call back function for when an attribute changes
//
// can return attribute changes for selector
(function($) {
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
$.fn.attrchange = function(callback) {
if (MutationObserver) {
var options = {
subtree: false,
attributes: true
};
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(e) {
callback.call(e.target, e.attributeName);
});
});
return this.each(function() {
observer.observe(this, options);
});
}
}
})(jQuery);
// when one details opens, close the others
$('.details-item').attrchange(function(attribute){
if(attribute == "open" && $(this).attr("open")) {
$(this).siblings(".details-item").removeAttr("open");
}
});
// keyboard: prevent closing the open details to emulate tabs
$('.details-tab').on("keydown", function(e) {
if(e.keyCode == 32 || e.keyCode == 13) {
if($(this).parent().attr("open")) {
e.preventDefault();
}
}
});
// mouse: prevent closing the open details to emulate tabs
$('.details-tab').on("click", function(e) {
if($(this).parent().attr("open")) {
e.preventDefault();
}
});
Also see: Tab Triggers