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

              
                <!-- tab -->
<div class="tab">
    
    <!-- tab-menu -->
    <div class="tab-menu">
        <div class="tab-menu__item tab-menu__item--active" data-tab-menu-item="0">Tab Menu1</div>
        <div class="tab-menu__item" data-tab-menu-item="1">Tab Menu2</div>
        <div class="tab-menu__item" data-tab-menu-item="2">Tab Menu3</div>
        <div class="tab-menu__item" data-tab-menu-item="3">Tab Menu4</div>
    </div><!-- /tab-menu -->
    
    <!-- tab-content -->
    <div class="tab-content">
        <div class="tab-content__item tab-content__item--active">Tab Content1</div>
        <div class="tab-content__item">Tab Content2</div>
        <div class="tab-content__item">Tab Content3</div>
        <div class="tab-content__item">Tab Content4</div>
    </div><!-- /tab-content -->
    
</div><!-- /tab -->
              
            
!

CSS

              
                // _tab-menu.scss
.tab-menu {
    display: flex;
    
    &__item {
        flex: 1;
        height: 60px;
        line-height: 60px;
        text-align: center;
        cursor: pointer;
        border: 1px solid #000;
        box-sizing: border-box;
        
        &--active {
            color: #fff;
            background-color: #000;
        }
    }
}

// _tab-content.scss
.tab-content {
    
    &__item {
        display: none;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: calc(100vh - 60px);
        font-size: 32px;
        
        &--active {
            display: flex
        }
    }
}
              
            
!

JS

              
                const tab = ()=>{
    
    // 要素取得(引数の値をセレクターに使用)
    let tabMenuItems = document.querySelectorAll('.tab-menu__item');
    let tabContentItems = document.querySelectorAll('.tab-content__item');
    
    // アクティブクラス名を指定
    let tabMenuItemActiveClass = 'tab-menu__item--active';
    let tabContentItemActiveClass = 'tab-content__item--active';
    
    // タブメニューアイテム、タブコンテンツアイテムの全アクティブクラスを削除する関数
    let removeAllActiveClass = ()=>{
        tabMenuItems.forEach(item => item.classList.remove(tabMenuItemActiveClass));
        tabContentItems.forEach(item => item.classList.remove(tabContentItemActiveClass));
    };

    // タブメニューアイテム分ループする
    tabMenuItems.forEach((item)=>{
        
        // 各タブメニューアイテムにクリックイベントを設定する
        item.addEventListener('click', (e)=>{
            
            // 関数実行
            removeAllActiveClass();
            
            // クリックされたタブメニューアイテムにアクティブクラスを付加する
            e.target.classList.add(tabMenuItemActiveClass);
            
            // クリックされたタブメニューアイテムのdata属性の値の順番にあるタブコンテンツアイテムにアクティブクラスを付加する
            tabContentItems[e.target.dataset.tabMenuItem].classList.add(tabContentItemActiveClass);
            
        });
    });
}

// 関数実行
tab();
              
            
!
999px

Console