Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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>Some countries of Asia</h1>

<div class="container with-sidebar">
  <ul id="tablist" role="tablist" class="tablist sidebar">
    <li role="presentation">
      <a class="tab with-sidebar" role="tab" href="#panel1" id="tab1">
        <img loading="lazy" class="flag sidebar" src="https://cdn.countryflags.com/thumbs/mongolia/flag-button-round-250.png" alt="" />
        <span class="description content">Mongolia</span>
      </a>
    </li>
    <li role="presentation">
      <a class="tab with-sidebar" role="tab" href="#panel2" id="tab2">
        <img loading="lazy" class="flag sidebar" src="https://cdn.countryflags.com/thumbs/kyrgyzstan/flag-button-round-250.png" alt="" />
        <span class="description content">Kyrgystan</span>
      </a>
    </li>
    <li role="presentation">
      <a class="tab with-sidebar" role="tab" href="#panel3" id="tab3">
        <img loading="lazy" src="https://cdn.countryflags.com/thumbs/myanmar/flag-button-round-250.png" alt="" class="flag sidebar" />
        <span class="description content">Myanmar</span></a>
    </li>
    <li role="presentation">
      <a class="tab with-sidebar" role="tab" href="#panel4" id="tab4">
        <img loading="lazy" src="https://cdn.countryflags.com/thumbs/uzbekistan/flag-button-round-250.png" alt="" class="flag sidebar" />
        <span class="description content">Uzbekistan</span></a>
    </li>
  </ul>
  <div class="tabpanel-container content">
    <section class="tabpanel" role="tabpanel" tabindex="0" id="panel1" aria-labelledby="tab1">
      <div class="flow">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
          <a href="#panel1">ornare eros egestas</a>
          lectus lacinia, ut luctus turpis egestas. Aliquam erat volutpat.
          Duis sed nisi metus. Ut sodales dapibus efficitur.
        </p>
        <p>
          Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
          Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
          pretium. Integer dictum diam in sem fringilla lobortis.
          Pellentesque gravida dui id felis hendrerit, a tristique lorem
          pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
          luctus tellus.
        </p>
      </div>
      <figure>
        <img loading="lazy" src="https://images.unsplash.com/photo-1589654615616-6756a5653100?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="A mongolian brown grass field surrounded by snow mountains" />
      </figure>
    </section>
    <section class="tabpanel" role="tabpanel" tabindex="0" id="panel2" aria-labelledby="tab2">
      <div class="flow">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
          ornare eros egestas lectus lacinia, ut luctus turpis egestas.
          Aliquam erat volutpat. Duis sed nisi metus. Ut sodales dapibus
          efficitur.
        </p>
        <p>
          Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
          Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
          pretium. Integer dictum diam in sem fringilla lobortis.
          Pellentesque gravida dui id felis hendrerit, a tristique lorem
          pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
          luctus tellus.
        </p>
      </div>
      <figure>
        <img loading="lazy" src="https://images.unsplash.com/photo-1567886793773-d59bfd7f0aa6?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1032&q=80" alt="A lake in a forest surrounded by mountains" />
      </figure>
    </section>
    <section class="tabpanel" role="tabpanel" tabindex="0" id="panel3" aria-labelledby="tab3">
      <div class="flow">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
          ornare eros egestas lectus lacinia, ut luctus turpis egestas.
          Aliquam erat volutpat. Duis sed nisi metus. Ut sodales dapibus
          efficitur.
        </p>
        <p>
          Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
          Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
          pretium. Integer dictum diam in sem fringilla lobortis.
          Pellentesque gravida dui id felis hendrerit, a tristique lorem
          pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
          luctus tellus.
        </p>
      </div>
      <figure>
        <img loading="lazy" src="https://images.unsplash.com/photo-1584897356466-858d9b6c53d1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=774&q=80" alt="A temple in Myanmar with a lot of hot air balloons in the background" />
      </figure>
    </section>
    <section class="tabpanel" role="tabpanel" tabindex="0" id="panel4" aria-labelledby="tab4">
      <div class="flow">
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris
          ornare eros egestas lectus lacinia, ut
          <a href="#panel4">luctus turpis egestas</a>. Aliquam erat
          volutpat. Duis sed nisi metus. Ut sodales dapibus efficitur.
        </p>
        <p>
          Aliquam pulvinar vestibulum mauris, id vehicula sapien euismod et.
          Praesent ac elementum nibh. Suspendisse quis justo eu ante rhoncus
          pretium. Integer
          <a href="#panel4">dictum diam in sem</a> fringilla lobortis.
          Pellentesque gravida dui id felis hendrerit, a tristique lorem
          pretium. Donec vitae velit posuere, pellentesque sem scelerisque,
          luctus tellus.
        </p>
      </div>
      <figure>
        <img loading="lazy" src="https://images.unsplash.com/photo-1557841621-d9f6673405ed?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80" alt="A front view of a temple in Uzbekistan" />
      </figure>
    </section>
  </div>
</div>
              
            
!

CSS

              
                :root {
  --bg: hsl(239deg 12% 92%);
  --component-bg: hsl(0deg 0% 85%);
  --accent: royalblue;
}

/* Css reset https://piccalil.li/blog/a-modern-css-reset/ */

*,
*::before,
*::after {
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

body,
h1,
p,
figure {
  margin: 0;
}

ul[role="tablist"] {
  list-style: none;
  padding-left: 0;
}

html:focus-within {
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  text-rendering: optimizeSpeed;
  line-height: 1.5;
}

a:not([class]) {
  text-decoration-skip-ink: auto;
}

img,
picture {
  max-width: 100%;
  display: block;
}

@media (prefers-reduced-motion: reduce) {
  html:focus-within {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Layout */

.flow > * + * {
  margin-top: var(--flow-space, 1em);
}

.with-sidebar {
  display: flex;
  flex-wrap: wrap;
  max-width: var(--max-width, 100%);
  gap: var(--gap, 0em);
  margin-inline: auto;
}

.sidebar {
  flex-basis: var(--sidebar-width, 9rem);
  flex-grow: 1;
}

.content {
  display: grid;
  place-items: center;
  flex-basis: 0;
  flex-grow: 999;
  min-width: var(--content-width, min(100%, 43.75rem));
}

/* Global styles */

body {
  background-color: var(--bg);
}

h1 {
  text-align: center;
  margin-block: 0.5em;
  color: var(--accent);
}

p {
  max-width: 80ch;
}

a:not([class]):focus {
  outline: 2px solid var(--accent);
  outline-offset: 0.1875rem;
  border-radius: 0.3125rem;
}

[role="tabpanel"]:focus {
  outline: 4px solid var(--accent);
}

/* Component */

.container {
  max-width: 110ch;
  align-items: start;
  background-color: var(--component-bg);
}

.tablist {
  display: flex;
  flex-wrap: wrap;
  margin: 0.2em;
  gap: 1em 0.2em;
  height: 100%;
}

.tablist li {
  flex: 1 1 4.375rem;
}

.tab {
  --sidebar-width: 2rem;
  --content-width: 60%;
  --gap: 0.1em 0.5em;
  padding: 0.5em 0.2em;
  text-decoration-color: transparent;
  border: 2px solid transparent;
  background-color: whitesmoke;
  color: #121212;
  font-size: clamp(0.88rem, calc(0.78rem + 0.49vw), 1.13rem);
}

.tab[aria-selected="true"] {
  background-color: var(--component-bg);
}

.tab:focus-visible {
  background-color: var(--accent);
  color: whitesmoke;
  outline: 0.2em solid transparent;
}

.flag {
  margin-inline: auto;
  aspect-ratio: 1;
  max-width: 2rem;
  height: auto;
  border-radius: 50%;
}

.description {
  font-weight: 500;
}

.tabpanel-container {
  position: relative;
  min-height: 50vh;
  font-size: clamp(1rem, calc(0.93rem + 0.37vw), 1.19rem);
  background-color: hsl(239deg 12% 92%);
  border: 0.2em solid var(--component-bg);
  isolation: isolate;
}

.tabpanel {
  display: flex;
  position: relative;
  flex-wrap: wrap;
  align-items: center;
  gap: 1em;
  padding: 1.5rem;
  background-color: var(--bg);
}

.tabpanel:nth-child(even) {
  flex-direction: row-reverse;
}

.tabpanel > * {
  flex: 1 1 20rem;
}

.tabpanel:not(:target):not(.visible) {
  display: none;
}

              
            
!

JS

              
                const TABLIST = document.querySelector("#tablist");
const TABS = [...TABLIST.querySelectorAll(".tab")];
const TABPANELS = [...document.querySelectorAll(".tabpanel")];

const showActivePanel = (element) => {
  const selectedId = element.id;
  TABPANELS.forEach((e) => {
    e.hidden = "true";
  });
  const activePanel = document.querySelector(
    `[aria-labelledby="${selectedId}"]`
  );
  activePanel.removeAttribute("hidden");
};

const setSelectedTab = (element) => {
  const selectedId = element.id;
  TABPANELS[0].classList.remove("visible");

  TABS.forEach((e) => {
    const id = e.getAttribute("id");
    if (id === selectedId) {
      e.removeAttribute("tabindex", "0");
      e.setAttribute("aria-selected", "true");
    } else {
      e.setAttribute("tabindex", "-1");
      e.setAttribute("aria-selected", "false");
    }
  });
};

const createArrowNavigation = () => {
  const firstTab = TABS[0];
  const lastTab = TABS[TABS.length - 1];

  TABS.forEach((element) => {
    element.addEventListener("keydown", function (e) {
      if ((e.keyCode || e.which) === 38 || (e.keyCode || e.which) === 37) {
        if (element == firstTab) {
          e.preventDefault();
          lastTab.focus();
        } else {
          e.preventDefault();
          const focusableElement = TABS.indexOf(element) - 1;
          TABS[focusableElement].focus();
        }
      } else if (
        (e.keyCode || e.which) === 40 ||
        (e.keyCode || e.which) === 39
      ) {
        if (element == lastTab) {
          e.preventDefault();
          firstTab.focus();
        } else {
          e.preventDefault();
          const focusableElement = TABS.indexOf(element) + 1;
          TABS[focusableElement].focus();
        }
      }
    });
  });
};

const determineTabindex = () => {
  TABPANELS.forEach((element) => {
    const focusableElements = element.querySelectorAll(
      'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]):not([disabled]), details:not([disabled]), summary:not(:disabled)'
    ).length;

    focusableElements
      ? element.setAttribute("tabindex", "-1")
      : element.setAttribute("tabindex", "0");
  });
};

const handleClick = () => {
  TABS.forEach((element) => {
    element.addEventListener("click", function () {
      setSelectedTab(element);
      showActivePanel(element);
    });
  });

  TABS.forEach((element) => {
    element.addEventListener("keydown", function (e) {
      if ((e.keyCode || e.which) === 32) {
        setSelectedTab(element);
        showActivePanel(element);
        element.click();
      }
    });
  });
};

const activateFirstPanel = () => {
  TABS[0].setAttribute("tabindex", "0");
  TABS[0].setAttribute("aria-selected", "true");
  TABPANELS[0].classList.add("visible");
};

const checkInitialSelectedTab = () => {
  const targetedTabPanel = document
    .querySelector(".tabpanel:target")
    .getAttribute("aria-labelledby");
  const selectedTab = document.querySelector(`#${targetedTabPanel}`);
  selectedTab.setAttribute("aria-selected", "true");
  selectedTab.removeAttribute("tabindex");
};

const handleInitialState = () => {
  TABS.forEach((e) => {
    e.setAttribute("tabindex", "-1");
    e.setAttribute("aria-selected", "false");
  });

  window.location.href.indexOf("#panel") === -1
    ? activateFirstPanel()
    : checkInitialSelectedTab();

  determineTabindex();
};

handleClick();
createArrowNavigation();

window.onload = handleInitialState;

              
            
!
999px

Console