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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <div class="js-tab-panel">

    <div class="flex justify-start">
        <div class="js-tab active_tab">
            タブ1
        </div>

        <div class="js-tab inactive_tab">
            タブ2
        </div>

        <div class="js-tab inactive_tab">
            タブ3
        </div>

        <div class="js-tab inactive_tab">
            タブ4
        </div>
    </div>

    <div class="js-panel active_content">
        コンテンツ1
    </div>

    <div class="js-panel inactive_content">
        コンテンツ2
    </div>

    <div class="js-panel inactive_content">
        コンテンツ3
    </div>

    <div class="js-panel inactive_content">
        コンテンツ4
    </div>

</div>
              
            
!

CSS

              
                .flex {
    display: flex;
}

.justify-center {
    justify-content: center;
}

.active_tab {
  font-family: system-ui;
  background: #f06d06;
  color: white;
  text-align: center;
  padding: 5px 10px;
}

.inactive_tab{
  padding: 5px 10px;
}

.active_content{
  display: inline;
  font-size:3rem;
}

.inactive_content{
  display: none;
}
              
            
!

JS

              
                document.addEventListener("DOMContentLoaded", function () {
  // タブに対してクリックイベントを適用
  const tabs = document.getElementsByClassName("js-tab");
  for (let i = 0; i < tabs.length; i++) {
    tabs[i].addEventListener("click", tabSwitch);
  }

  // タブをクリックすると実行する関数
  function tabSwitch() {
    // 引数で指定したセレクターと一致する直近の祖先要素を取得
    const ancestorEle = this.closest(".js-tab-panel");

    const active_tab_classes = "active_tab";
    const inactive_tab_classes = "inactive_tab";
    // タブのclassの値を変更
    if (ancestorEle.getElementsByClassName(active_tab_classes)[0]) {
      ancestorEle
        .getElementsByClassName(active_tab_classes)[0]
        .classList.add(inactive_tab_classes);
      ancestorEle
        .getElementsByClassName(active_tab_classes)[0]
        .classList.remove(active_tab_classes);
      this.classList.add(active_tab_classes);
      this.classList.remove(inactive_tab_classes);
    }

    // コンテンツのclassの値を変更
    const active_content_classes = "active_content";
    const inactive_content_classes = "inactive_content";
    const targetEle = ancestorEle.getElementsByClassName(
      "js-panel " + active_content_classes
    )[0];
    targetEle.classList.remove(active_content_classes);
    targetEle.classList.add(inactive_content_classes);

    const groupTabs = ancestorEle.getElementsByClassName("js-tab");
    const arrayTabs = Array.prototype.slice.call(groupTabs);
    const index = arrayTabs.indexOf(this);
    const targetPanel = ancestorEle.getElementsByClassName("js-panel")[index];
    targetPanel.classList.add(active_content_classes);
    targetPanel.classList.remove(inactive_content_classes);
  }
});

              
            
!
999px

Console