Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                /* 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);
  }
}

              
            
!

JS

              
                
              
            
!
999px

Console