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="c-switching">
   <div class="c-switchingTabs">
     <button class="js-switchingTab is-current c-switchingTabs__tab" type="button" data-switch-tab="type01"><span>その1<small>(初期値には<code>.is-current</code>を付与)</small></span></button>
     <button class="js-switchingTab c-switchingTabs__tab" type="button" data-switch-tab="type02">その2</button>
     <button class="js-switchingTab c-switchingTabs__tab" type="button" data-switch-tab="type03">その3</button>
   </div>
  <div class="c-switchingContainer">
    <div class="js-switchingContents is-open c-switchingContainer__contents" data-switch-contents="type01">
      <p>その1のコンテンツ内容が表示されます。<br>
        (初期値にしたいコンテンツには<code>.is-open</code>を付与する)</p>
      <button class="js-switchingTab c-btn" type="button" data-switch-tab="type02">その2 を表示する</button>
    </div>
    <div class="js-switchingContents c-switchingContainer__contents" data-switch-contents="type02">
      <p>その2のコンテンツ内容が表示されます。</p>
      <button class="js-switchingTab c-btn" type="button" data-switch-tab="type03">その3 を表示する</button>
    </div>
    <div class="js-switchingContents c-switchingContainer__contents" data-switch-contents="type03">
      <p>その3のコンテンツ内容が表示されます。<br>ボタンがどこにあっても大丈夫!</p>
      <button class="js-switchingTab c-btn is-current" type="button" data-switch-tab="type01">その1 を表示する</button>
    </div>
  </div>
</div>
<button class="js-switchingTab c-btn is-current" type="button" data-switch-tab="type01">その1 を表示する</button>
<button class="js-switchingTab c-btn" type="button" data-switch-tab="type02">その2 を表示する</button>
<button class="js-switchingTab c-btn" type="button" data-switch-tab="type03">その3 を表示する</button>
              
            
!

CSS

              
                * {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
}
small {
  font-size: max(0.7em, 12px);
}

.c-btn {
  display: flex;
  justify-content: center;
  align-items: center;
  min-width: 200px;
  padding: 0.8em 1em;
  border: 1px solid #333;
  border-radius: 2em;
  background: #fff;
  color: #333;
  cursor: pointer;
  transition: .3s;
  &:hover, &.is-current {
    background: #333;
    color: #fff;
  }
  &:not(:first-child) {
    margin-top: 2em;
  }
}

.c-switchingTabs {
  display: flex;
  gap: 20px;
  margin-bottom: -1px;
  &__tab {
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 200px;
    padding: 0.5em 1em;
    border: 1px solid #333;
    border-bottom: none;
    border-radius: 20px 20px 0 0;
    background: lightgray;
    font-size: 20px;
    line-height: 1.4;
    cursor: pointer;
    transition: .3s;
    &:hover, &.is-current {
      background: lightblue;
    }
  }
}
.c-switchingContainer {
  width: min(100%, 1000px);
  padding: 5em 1em 6em;
  border: 1px solid #333;
  background: lightblue;
  &__contents {
    width: 100%;
  }
}

.js-switchingContents {
  height: 0;
  opacity: 0;
  overflow: hidden;
  transition: .3s;
  &.is-open {
    height: auto;
    opacity: 1;
  }
}
              
            
!

JS

              
                // HTML要素を取得
const switchingTabs = document.querySelectorAll('.js-switchingTab');
const switchingContents = document.querySelectorAll('.js-switchingContents');

// タブボタンにクリックイベントを追加
switchingTabs.forEach(tabButton => {
  tabButton.addEventListener('click', () => {
    // クリックされたタブボタンのdata-switch-tab属性の値を取得
    const targetTab = tabButton.getAttribute('data-switch-tab');
    
    // 対応するコンテンツ要素を探す
    const targetContent = document.querySelector(`[data-switch-contents="${targetTab}"]`);
    
    // すべてのコンテンツ要素から.is-openクラスを削除
    switchingContents.forEach(switchingContent => {
      switchingContent.classList.remove('is-open');
    });
    
    // 対応するコンテンツ要素に.is-openクラスを追加
    targetContent.classList.add('is-open');

    // すべてのタブボタンから.is-currentクラスを削除
    switchingTabs.forEach(tabButton => {
      tabButton.classList.remove('is-current');
    });

    // クリックされたタブボタンに.is-currentクラスを追加
    tabButton.classList.add('is-current');

    // 同じdata-switch-tab属性の値を持つ他のタブボタンにも.is-currentクラスを追加
    switchingTabs.forEach(otherTabButton => {
      if (otherTabButton !== tabButton && otherTabButton.getAttribute('data-switch-tab') === targetTab) {
        otherTabButton.classList.add('is-current');
      }
    });
  });
});

              
            
!
999px

Console