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.
<h1>A responsive tablist that can become vertical, without JavaScript</h1>
<p>This tablist is based on flexbox and the :has selector.</p>
<p>It is responsive in the sense that you can assign a CSS class named <code>vertical</code> to the tablist, which will turn the tablist into an accordeon for small-width viewports, like on a phone.</p>
<div>
<label>
Focus this text field and then press the tab key on your keyboard to jump into the active tab. It´s then possible to change the active tab with the arrow keys of the keyboard.
<input type="text" style="display:block; border: 1px solid currentColor; font-size: 1em;">
</label>
<p>You can as well select any tab by clicking on it with the mouse pointer.</p>
</div>
<!-- below come two tablists
- They use the exact same HTML, the second list in addition has the CSS class vertical assigned to it.
- Roles are used for accessibility. The tablist role is used as the CSS selector for the tablist.
- The name of the input radioboxes has to be adjusted when there are multiple tablists on one page. For each new tablist it needs to be the same for all radio inputs inside of the tablist, but different to the other tablists of the page.
-->
<h2>Horizontal tablist</h2>
<div role="tablist">
<!-- tab 1 -->
<label role="tab">
<input type="radio" name="tab" checked><span>Tab 1</span>
</label>
<div role="tabpanel"><span style="color:blue">Tab 1 content</span></div>
<!-- tab 2 -->
<label role="tab">
<input type="radio" name="tab"><span>Tab 2</span>
</label>
<div role="tabpanel"><span style="color:red">Tab 2 content</span></div>
<!-- tab 3 -->
<label role="tab">
<input type="radio" name="tab"><span>Tab 3</span>
</label>
<div role="tabpanel"><span style="color:green">Tab 3 content</span></div>
</div>
<h2>Vertical tablist (accordeon)</h2>
<p>The vertical tablist leverages the same HTML as the horizontal tablist, but it has in addition the CSS class <code>vertical</code> assigned to it, and the <code>name</code> attribute for the three radio inputs is <code>vert-tab</code>.</p>
<div role="tablist" class="vertical">
<!-- tab 1 -->
<label role="tab">
<input type="radio" name="vert-tab" checked><span>Tab 1</span>
</label>
<div role="tabpanel"><span style="color:blue">Tab 1 content</span></div>
<!-- tab 2 -->
<label role="tab">
<input type="radio" name="vert-tab"><span>Tab 2</span>
</label>
<div role="tabpanel"><span style="color:red">Tab 2 content</span></div>
<!-- tab 3 -->
<label role="tab">
<input type="radio" name="vert-tab"><span>Tab 3</span>
</label>
<div role="tabpanel"><span style="color:green">Tab 3 content</span></div>
</div>
<h2>Responsive tablist</h2>
<p>The responsive tablist leverages the same HTML as the other tablists, but the <code>name</code> attribute for the three radio inputs is <code>resp-tab</code> and the tablist has the class <code>max-sm:vertical</code> assigned to it, which means, for a screen of max sm (small) size, the vertical styles will be applied. Above the sm screen size the vertical styles will be ignored. Small in our case is a 600 px wide screen.</p>
<p>Up to a screen width of 600 px the tablist will be shown vertical. For wider screen widths the tablist will be visualized horizontally.</p>
<div role="tablist" class="max-sm:vertical">
<!-- tab 1 -->
<label role="tab">
<input type="radio" name="resp-tab" checked><span>Tab 1</span>
</label>
<div role="tabpanel"><span style="color:blue">Tab 1 content</span></div>
<!-- tab 2 -->
<label role="tab">
<input type="radio" name="resp-tab"><span>Tab 2</span>
</label>
<div role="tabpanel"><span style="color:red">Tab 2 content</span></div>
<!-- tab 3 -->
<label role="tab">
<input type="radio" name="resp-tab"><span>Tab 3</span>
</label>
<div role="tabpanel"><span style="color:green">Tab 3 content</span></div>
</div>
<h2 dir="rtl">Responsive tablist, right to left direction, set with the attribute <code>dir=rtl</code></h2>
<p dir="rtl">Note: As of October 2024 Safari is not using the arrow keys of the keyboard correct when selecting tabs of right-to-left directed content with the keyboard</p>
<div role="tablist" dir="rtl" class="max-sm:vertical">
<!-- tab 1 -->
<label role="tab">
<input type="radio" name="rtl-tab" checked><span>Tab 1</span>
</label>
<div role="tabpanel"><span style="color:blue">Tab 1 content</span></div>
<!-- tab 2 -->
<label role="tab">
<input type="radio" name="rtl-tab"><span>Tab 2</span>
</label>
<div role="tabpanel"><span style="color:red">Tab 2 content</span></div>
<!-- tab 3 -->
<label role="tab">
<input type="radio" name="rtl-tab"><span>Tab 3</span>
</label>
<div role="tabpanel"><span style="color:green">Tab 3 content</span></div>
</div>
/* helper */
body {
padding: 0 0 3em 0;
}
h2 {
margin: 2em 0 1em 0;
}
/* tablist styling */
[role="tablist"] {
--outline: auto Highlight;
--tab-label-padding: 0.5em;
--tab-label-next-padding: 2em;
--tab-label-radius: 0.15em;
--tab-label-active-background: white;
--tab-label-active-color: currentColor;
--tab-content-x-padding: 0;
--tab-content-y-padding: 1em;
--tab-border-color: currentColor;
--tab-border-width: 1px;
--vertical-tab-label-active-background: white;
--vertical-tab-label-active-color: currentColor;
display: flex;
align-items: flex-end;
flex-wrap: wrap;
}
/* hide everything, except the tab labels */
[role="tablist"] > *:not(label[role="tab"]) {
display: none;
}
/* keep the inputs on the page, but make them invisible to allow for keyboard focus */
[role="tablist"] > label[role="tab"] > input[type="radio"] {
opacity: 0;
width: 0;
height: 0;
margin: 0;
}
/* when using the tab key on the keyboard to focus into the tablist, indicate the default outline */
[role="tablist"] > label[role="tab"]:has(:focus-visible) {
outline: var(--outline);
outline-offset: calc(-1 * var(--tab-label-padding));
}
/* tab label */
[role="tablist"] > label[role="tab"] {
padding: var(--tab-label-padding);
padding-inline-end: var(--tab-label-next-padding);
cursor: pointer;
margin-bottom: calc(-1 * var(--tab-border-width));
border-bottom: none;
border-left: var(--tab-border-width) solid transparent;
border-top: var(--tab-border-width) solid transparent;
border-right: var(--tab-border-width) solid transparent;
}
/* selected tab label */
[role="tablist"] > label[role="tab"]:has(input[type="radio"]:checked) {
background: var(--tab-label-active-background);
border-left: var(--tab-border-width) solid var(--tab-border-color);
border-top: var(--tab-border-width) solid var(--tab-border-color);
border-right: var(--tab-border-width) solid var(--tab-border-color);
border-bottom: none;
border-radius: var(--tab-label-radius) var(--tab-label-radius) 0 0;
}
[role="tablist"] > label[role="tab"]:has(input[type="radio"]:checked) > * {
color: var(--tab-label-active-color);
}
/* tab content */
[role="tablist"] > label[role="tab"]:has(input:checked) + [role="tabpanel"] {
border-top: var(--tab-border-width) solid var(--tab-border-color);
order: 99;
display: block;
width: 100%;
position: relative;
z-index: -1;
padding: var(--tab-content-y-padding) var(--tab-content-x-padding);
}
/* vertical tablist styling */
/* tab label */
.vertical[role="tablist"] > label[role="tab"] {
display: block;
width: 100%;
border: var(--tab-border-width) solid var(--tab-border-color);
border-bottom: none;
}
.vertical[role="tablist"] > label[role="tab"]:first-of-type {
border-radius: var(--tab-label-radius) var(--tab-label-radius) 0 0;
}
.vertical[role="tablist"] > label[role="tab"]:last-of-type {
border-radius: 0 0 var(--tab-label-radius) var(--tab-label-radius);
border-bottom: var(--tab-border-width) solid var(--tab-border-color);
}
/* selected tab label */
.vertical[role="tablist"] > label[role="tab"]:has(input[type="radio"]:checked) {
border-bottom: none;
background: var(--vertical-tab-label-active-background);
}
.vertical[role="tablist"]
> label[role="tab"]:has(input[type="radio"]:checked)
> * {
color: var(--vertical-tab-label-active-color);
}
.vertical[role="tablist"]
> label[role="tab"]:has(input[type="radio"]:checked):last-of-type {
border-radius: 0;
}
/* tab content */
.vertical[role="tablist"]
> label[role="tab"]:has(input:checked)
+ [role="tabpanel"] {
order: unset;
padding: var(--tab-label-padding) var(--tab-label-padding);
border-left: var(--tab-border-width) solid var(--tab-border-color);
border-right: var(--tab-border-width) solid var(--tab-border-color);
}
.vertical[role="tablist"]
> label[role="tab"]:has(input:checked)
+ [role="tabpanel"]:last-of-type {
border-bottom: var(--tab-border-width) solid var(--tab-border-color);
border-radius: 0 0 var(--tab-label-radius) var(--tab-label-radius);
}
/* Make the vertical tablist responsive by introducing a max-sm: variant, like known from Tailwind CSS.
The variant is to be used like class="max-sm:vertical", which means, for a screen of max sm (small) size,
the vertical styles will be applied.
Above the sm screen size the vertical styles will be ignored.
Small in our case is a 600 px wide screen.
Unfortunately, the entire code for the vertical class has to be repeated within the media query.
When you use Tailwind CSS, you can achieve the same result much simpler by defining the vertical class within your
Tailwind CSS files and then apply the standard screen variants that Tailwind already has on board.
You do not need to define the variant yourself in that case!
The backslash in .max-sm\:vertical is required to allow the : being part of a CSS class name.
*/
@media screen and (max-width: 600px) {
/* tab label */
.max-sm\:vertical[role="tablist"] > label[role="tab"] {
display: block;
width: 100%;
border: var(--tab-border-width) solid var(--tab-border-color);
border-bottom: none;
}
.max-sm\:vertical[role="tablist"] > label[role="tab"]:first-of-type {
border-radius: var(--tab-label-radius) var(--tab-label-radius) 0 0;
}
.max-sm\:vertical[role="tablist"] > label[role="tab"]:last-of-type {
border-radius: 0 0 var(--tab-label-radius) var(--tab-label-radius);
border-bottom: var(--tab-border-width) solid var(--tab-border-color);
}
/* selected tab label */
.max-sm\:vertical[role="tablist"]
> label[role="tab"]:has(input[type="radio"]:checked) {
border-bottom: none;
background: var(--vertical-tab-label-active-background);
}
.max-sm\:vertical[role="tablist"]
> label[role="tab"]:has(input[type="radio"]:checked)
> * {
color: var(--vertical-tab-label-active-color);
}
.max-sm\:vertical[role="tablist"]
> label[role="tab"]:has(input[type="radio"]:checked):last-of-type {
border-radius: 0;
}
/* tab content */
.max-sm\:vertical[role="tablist"]
> label[role="tab"]:has(input:checked)
+ [role="tabpanel"] {
order: unset;
padding: var(--tab-label-padding) var(--tab-label-padding);
border-left: var(--tab-border-width) solid var(--tab-border-color);
border-right: var(--tab-border-width) solid var(--tab-border-color);
}
.max-sm\:vertical[role="tablist"]
> label[role="tab"]:has(input:checked)
+ [role="tabpanel"]:last-of-type {
border-bottom: var(--tab-border-width) solid var(--tab-border-color);
border-radius: 0 0 var(--tab-label-radius) var(--tab-label-radius);
}
}
Also see: Tab Triggers