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="card">
  <section class="card__tab__wrapper">
    <div data-tab-id="tab-1" class="card__tab card__tab--active">Tab 1</div>
    <div data-tab-id="tab-2" class="card__tab">Another one!</div>
    <div data-tab-id="tab-3" class="card__tab">Hello there :D</div>
  </section>
  <section class="card__body">
    <section class="card__section card__section--active" id="tab-1">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ipsum arcu, euismod eu
      mattis quis, consectetur nec ante. Aenean ultricies neque nibh, ac ullamcorper purus
      finibus sed. Sed sit amet turpis ullamcorper, molestie ante sed, volutpat ipsum. Praesent
      eros est, scelerisque viverra felis at, pulvinar volutpat felis. Vivamus leo arcu, tempus
      in rhoncus eu, tempus sed risus. Nunc vestibulum mauris quis laoreet posuere. Praesent est
      mauris, bibendum vel commodo in, auctor sit amet nisi. Sed blandit gravida ligula, eget
      imperdiet tellus efficitur luctus. Donec ac diam vitae nisi malesuada fringilla eu ut
      tortor. Curabitur sapien elit, tempor at congue ac, dapibus ac felis. Orci varius natoque
      penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque luctus blandit
      bibendum. Proin eget dictum orci.
    </section>
    <section class="card__section" id="tab-2">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ipsum arcu, euismod eu
      mattis quis, consectetur nec ante. Aenean ultricies neque nibh, ac ullamcorper purus
      finibus sed. Sed sit amet turpis ullamcorper, molestie ante sed, volutpat ipsum. Praesent
      eros est, scelerisque viverra felis at, pu Proin eget dictum orci.
    </section>
    <section class="card__section" id="tab-3">
      tempus in rhoncus eu, tempus sed risus. Nunc vestibulum mauris quis laoreet posuere.
      Praesent est mauris, bibendum vel commodo in, auctor sit amet nisi. Sed blandit gravida
      ligula, eget imperdiet tellus efficitur luctus. Donec ac diam vitae nisi malesuada
      fringilla eu ut tortor. Curabitur sapien elit, tempor at congue ac, dapibus ac felis. Orci
      varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque
      luctus blandit bibendum. Proin eget dictum orci
    </section>
  </section>
</div>
              
            
!

CSS

              
                // If You'd like to see the code on github, or get the minified css for that component, then I'd be more than happy, to share this link with You -> https://github.com/Vorbert-Kruk/tabs-component-idea 😁

// Variables
$accent: #66a6ff;
$accent-saturated: rgba($accent, 0.5);

$secondary: #bdc2c9;
$secondary-saturated: #b1c0d5;

$light: #fff;

// Global styling
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap");

*,
*::before,
*::after {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}

body {
  font-family: "Roboto", sans-serif;
  background-color: $accent-saturated;
}

// Component styling
.card {
  margin: 225px auto 0;
  color: $secondary-saturated;
  width: 92.5%;
  max-width: 750px;

  &__tab {
    &__wrapper {
      display: flex;
    }

    & {
      $tab-border-radius: 0.75em;

      display: inline-block;
      padding: 0.35em 1em;
      background-color: $accent;
      border-radius: $tab-border-radius $tab-border-radius 0 0;
      font-size: 1.5rem;
      color: $light;
      text-align: center;
      text-decoration: none;
      user-select: none;
      transition: all 150ms ease;

      @media screen and (max-width: 600px) {
        font-size: 1.25rem;
        padding: 0.35em 0.75em;
      }

      &:hover {
        cursor: pointer;
      }

      &:not(#{&}--active):hover {
        background-color: mix($accent, $light, 75);
      }
    }

    &--active {
      background-color: $light;
      color: $accent;
    }
  }

  &__body {
    position: relative;
    font-size: 1.25rem;
    background-color: $light;
    padding: 0.8em 1.2em;
    border-radius: 0.5rem;
    border-top-left-radius: 0;
    line-height: 140%;
    overflow: hidden;
    transition: all 350ms ease;

    @media screen and (max-width: 600px) {
      font-size: 1.15rem;
    }
  }

  &__section {
    & {
      position: absolute;
      top: 0;
      left: 0;
      padding: inherit;
      transition: all 150ms 350ms ease-in-out;
    }

    &:not(#{&}--active) {
      opacity: 0;
      transition: all 150ms ease-out;
    }
  }
}

              
            
!

JS

              
                const cardClassName = "card";
const tabDataAttributeName = "data-tab-id";

const tabClassName = `${cardClassName}__tab`;
const tabSectionClassName = `${cardClassName}__section`;
const tabSectionsContainerClassName = `${cardClassName}__body`;

const tabSectionsContainerSelector = `.${tabSectionsContainerClassName}`;

const tabSelector = `.${tabClassName}[${tabDataAttributeName}]`;
const tabSectionSelector = `.${tabSectionClassName}`;

const activeTabClassName = `${tabClassName}--active`;
const activeSectionClassName = `${tabSectionClassName}--active`;

const tabs = document.querySelectorAll(tabSelector);
const tabsSections = document.querySelector(tabSectionSelector);
const tabSectionsContainer = document.querySelector(
  tabSectionsContainerSelector
);

const setTabInactive = (tab) => tab && tab.classList.remove(activeTabClassName);
const setTabActive = (tab) => tab && tab.classList.add(activeTabClassName);

const setSectionInactive = (section) =>
  section && section.classList.remove(activeSectionClassName);
const setSectionActive = (sectionId) => {
  const currentSection = document.querySelector(
    `${tabSectionSelector}#${sectionId}`
  );

  if (currentSection) {
    changeSectionsContainerHeight(currentSection);
    currentSection.classList.add(activeSectionClassName);
  }
};

const getCurrentlyActiveTab = () =>
  document.querySelector(`.${activeTabClassName}`);
const getCurrentlyActiveSection = () =>
  document.querySelector(`.${activeSectionClassName}`);

const getSectionHeight = (section) =>
  section && section.getBoundingClientRect().height;
const changeSectionsContainerHeight = (section) =>
  (tabSectionsContainer.style.height = `${getSectionHeight(section)}px`);

const changeTab = (tab) => {
  const tabSectionId = tab.getAttribute(tabDataAttributeName);

  if (tabSectionId) {
    setTabInactive(getCurrentlyActiveTab());
    setSectionInactive(getCurrentlyActiveSection());
    setTabActive(tab);
    setSectionActive(tabSectionId);
  }
};

const updateSectionsContainerHeight = () => {
  const currentlyActiveSection = getCurrentlyActiveSection();
  currentlyActiveSection &&
    changeSectionsContainerHeight(currentlyActiveSection);
};

(() => {
  changeTab(getCurrentlyActiveTab());

  tabs.forEach((tab) => {
    tab.addEventListener("click", () => changeTab(tab));
  });

  window.addEventListener("resize", () => updateSectionsContainerHeight());
})();

              
            
!
999px

Console