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.
<details open>
<summary>Have you heard about <code>details</code>?</summary>
<p>It's a pretty useful element that handles accordion / collapsed text natively!</p>
</details>
<details>
<summary>So how does it work?</summary>
<p>You wrap a <code>details</code> element around any block of HTML content. The browser will collapse that block of text until a user opens the <code>details</code> block.</p>
<p>Once a user opens a <code>details</code> block, they'll be able to read all that hidden content!</p>
<p>If you want the details block open by default, include the <code>open</code> attribute on the opening tag:</p>
<pre><code><details open>...</details></code></pre>
</details>
<details>
<summary>But how do I set a custom title?</summary>
<p>That's pretty manageable too! Use the <code>summary</code> element.</p>
<p>Put a <code>summary</code> at the beginning of your details element and <b>Boom!</b> - you've got a custom title for your <code>details</code> block.</p>
<p>No worries if you don't add a <code>summary</code>. The browser will put the word "Details" in there for you. (After all, users need something to click!)</p>
</details>
<details>
<summary>That's cool, but what about styles?</summary>
<p>Yes, you're covered there too! Style the <code>details</code> element however you like. Give it a border, some padding, whatever.</p>
<p>The <code>summary</code> element is where the <code>▸</code> marker lives. If you want to get rid of that, there is a prefixed pseudo-element selector <code>::-webkit-details-marker</code>: set that to <code>display: none</code> for WebKit browsers. In Firefox, it's much simpler: set the <code>summary</code> to <code>display: block</code> or <code>flex</code> (anything but the native <code>display: list-item</code>) and you'll get rid of the <code>▸</code> for you.</p>
</details>
<details>
<summary>Ok, ok, but what about styling based on state?</summary>
<p>Once again, <code>details</code> has got your back! When a <code>details</code> block is open, it has the <code>open</code> attribute that I mentioned earlier. To style it (or its children) based on its state, use <code>details[open] { }</code>.</p>
<p>Note: there's no <code>closed</code> attribute: styles you apply by "default" will be used on the closed state.</p>
</details>
<details>
<summary>But this requires JavaScript, right?</summary>
<p>Open the JS panel on this pen. Clean as a whistle! This is handled totally by the browser.</p>
</details>
<details>
<summary>What about accessibility? Is that the catch?</summary>
<p>Sorry to disappoint you. Since these are native HTML elements, they provide useful semantic information to screen readers.</p>
<p>Screen readers will typically read the <code>summary</code> for a collapsed <code>details</code> block (and communicate that it's collapsed). They'll also provide an interactive hook for users to open the <code>details</code> block. If the <code>details</code> is already expanded, they'll read the whole content.</p>
<p>I don't rely on assistive tech to read the web, so I'm probably not aware of some limitations or drawbacks to using <code>details</code> and <code>summary</code>, but I suspect their AX is at least as good as (if not better than) most JavaScript-dependent accordion solutions.</p>
</details>
<details>
<summary>Excellent! More information, please!</summary>
<p>You bet! Here are some great resources on <code>details</code> & <code>summary</code>:</p>
<ul>
<li>MDN: <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details" target="_blank" rel="noopener">details</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/summary" target="_blank" rel="noopener">summary</a></li>
<li><a href="http://html5 doctor.com/the-details-and-summary-elements/" target="_blank" rel="noopener">HTML5Doctor</a></li>
<li><a href="https://www.scottohara.me/blog/2018/09/03/details-and-summary.html" target="_blank" rel="noopener">Scott O'Hara</a></li>
<li><a href="https://blog.teamtreehouse.com/use-details-summary-elements" target="_blank" rel="noopener">Treehouse Blog</a></li>
<li><a href="https://webdesign.tutsplus.com/tutorials/explaining-the-details-and-summary-elements--cms-21999" target="_blank" rel="noopener">Envato Tuts+</a></li>
<li><a href="https://caniuse.com/#feat=details" target="_blank" rel="noopener">caniuse table for <code>details</code></a></li>
</ul>
</details>
<details>
<summary>But what about cross-browser compatibility?</summary>
<p>Yeah, sorry. Here's some bad news. IE, Edge, and Opera Mini don't currently support <code>details</code>/<code>summary</code> with native open/close behavior (check out <a href="https://caniuse.com/#feat=details" target="_blank" rel="noopener">caniuse data for <code>details</code></a>).<p>
<p>These browsers will show all your <code>details</code> elements expanded. That's not the worst though: it's a bit of progressive enhancement: if the browser doesn't support the native UI behavior, the content will still be visible to users.</p>
<p>It's unlikey IE11 will be getting any updates on this front, but there's hope for Edge! If this is important to you, please <a href="https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/6261266-details-summary-elements" target="_blank" rel="noopener">cast a vote a vote for Edge to support <code>details</code>/<code>summary</code></a>. (Or just wait for Edge to use Chromium, I guess. 😕)</p>
<p>If you do need to have open/close behavior in IE11 (or any other non-supporting browser), you'll probably need a polyfill. This <a href="https://www.smashingmagazine.com/2014/11/complete-polyfill-html5-details-element/" target="_blank" rel="noopener">Smashing Magazine <code>details</code> polyfill tutorial</a> looks like a good place to start.</p>
</details>
*,
::before,
::after {
box-sizing: border-box;
}
html {
background-color: #CFD8DC;
}
details {
margin: 1rem auto;
padding: 0 1rem;
width: 35em;
max-width: calc(100% - 2rem);
position: relative;
border: 1px solid #78909C;
border-radius: 6px;
background-color: #ECEFF1;
color: #263238;
transition: background-color .15s;
> :last-child {
margin-bottom: 1rem;
}
&::before {
width: 100%;
height: 100%;
content: '';
position: absolute;
top: 0;
left: 0;
border-radius: inherit;
opacity: .15;
box-shadow: 0 .25em .5em #263238;
pointer-events: none;
transition: opacity .2s;
z-index: -1;
}
&[open] {
background-color: #FFF;
&::before {
opacity: .6;
}
}
}
summary {
padding: 1rem 2em 1rem 0;
display: block;
position: relative;
font-size: 1.33em;
font-weight: bold;
cursor: pointer;
&::before,
&::after {
width: .75em;
height: 2px;
position: absolute;
top: 50%;
right: 0;
content: '';
background-color: currentColor;
text-align: right;
transform: translateY(-50%);
transition: transform .2s ease-in-out;
}
&::after {
transform: translateY(-50%) rotate(90deg);
[open] & {
transform: translateY(-50%) rotate(180deg);
}
}
&::-webkit-details-marker {
display: none;
}
}
p {
margin: 0 0 1em;
line-height: 1.5;
}
ul {
margin: 0 0 1em;
padding: 0 0 0 1em;
}
li:not(:last-child) {
margin-bottom: 0.5em;
}
code {
padding: 0.2em;
border-radius: 3px;
background-color: #E0E0E0;
pre > & {
display: block;
padding: 1em;
margin: 0;
}
}
// Nothing to see here…
Also see: Tab Triggers