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

              
                <div class="tablist-wrapper">
  <div class="tablist" role="tablist">
    <button class="tab" role="tab" aria-selected="true" tab-index="0">Tab one</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab two</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab three</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab four</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab five</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab six</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab seven</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab eight</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab nine</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab ten</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab eleven</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab twelve</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab thirteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab fourteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab fifteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab sixteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab seventeen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab eighteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab nineteen</button>
    <button class="tab" role="tab" aria-selected="false" tabindex="-1">Tab twenty</button>
  </div>
  <div class="indicator"></div>
</div>

<p>See <a href="https://philparsons.co.uk/blog/building-scrollable-tabs-with-css/">this post</a> for an explanation of the example above.</p>
              
            
!

CSS

              
                :root {
  color-scheme: light dark;
}

.tablist-wrapper {
  margin: 3rem auto;
  max-width: 600px;
  overflow: hidden;
  padding: 0 2rem;
  position: relative;
}

.tablist {
  anchor-name: --tab-list;
  background: var(--color-bg-secondary);
  display: flex;
  gap: 0.125rem;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-behavior: smooth;

  &::scroll-button(*) {
    align-self: anchor-center;
    background: var(--color-bg-secondary);
    border: none;
    border-bottom: 2px solid var(--color-bg-secondary);
    font-size: 1rem;
    line-height: 1;
    padding: 0.75rem 0.5rem;
    position: absolute;
    position-anchor: --tab-list;
    width: 2rem;
    z-index: 2;
  }

  &::scroll-button(inline-start) {
    content: "<" / "Previous";
    left: calc(anchor(start) - 2rem);
  }

  &::scroll-button(inline-end) {
    content: ">" / "Next";
    right: calc(anchor(end) - 2rem);
  }
}

.tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  padding: 0.75rem 1rem;
  text-transform: uppercase;
  white-space: nowrap;

  &[aria-selected="true"] {
    anchor-name: --selected-tab;
    color: light-dark(rgb(18, 92, 165), rgb(144, 202, 249));
  }

  &:focus-visible {
    background: light-dark(rgb(0 0 0 / 5%), rgb(255 255 255 / 8%));
    outline: 0;
  }
}

.indicator {
  background: light-dark(rgb(25, 118, 210), rgb(144, 202, 249));
  bottom: anchor(bottom);
  height: 2px;
  left: anchor(start);
  right: anchor(end);
  position: absolute;
  position-anchor: --selected-tab;
  transition: left 0.3s ease-in-out, right 0.3s ease-in-out;
  z-index: 1;
}

p {
  margin: 0;
  padding: 0 1rem 1rem;
  text-align: center;
}

              
            
!

JS

              
                const tabList = document.querySelector(".tablist");
const tabs = document.querySelectorAll(".tab");

tabList.addEventListener("click", (e) => {
  const targetTab = e.target.closest('[role="tab"]');
  if (!targetTab) return; // Ignore clicks outside tabs

  // Activate the clicked tab
  activateTab(targetTab);
});

// Add keyboard navigation
tabList.addEventListener("keydown", (e) => {
  const targetTab = e.target;
  const previousTab = targetTab.previousElementSibling;
  const nextTab = targetTab.nextElementSibling;
  const firstTab = tabs[0];
  const lastTab = tabs[tabs.length - 1];

  // Only handle key events if a tab triggered them
  if (targetTab.getAttribute("role") !== "tab") return;

  switch (e.key) {
    case "ArrowLeft":
      e.preventDefault();
      if (previousTab && previousTab.getAttribute("role") === "tab") {
        activateTab(previousTab);
      } else {
        activateTab(lastTab); // Cycle to end
      }
      break;
    case "ArrowRight":
      e.preventDefault();
      if (nextTab && nextTab.getAttribute("role") === "tab") {
        activateTab(nextTab);
      } else {
        activateTab(firstTab); // Cycle to beginning
      }
      break;
    case "Home":
      e.preventDefault();
      activateTab(firstTab);
      break;
    case "End":
      e.preventDefault();
      activateTab(lastTab);
      break;
  }
});

function activateTab(tab) {
  // Deactivate all tabs
  tabs.forEach((t) => {
    t.setAttribute("aria-selected", "false");
    t.setAttribute("tabindex", "-1");
  });

  // Activate the current tab
  tab.setAttribute("aria-selected", "true");
  tab.setAttribute("tabindex", "0");
  tab.focus();
}

              
            
!
999px

Console