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="wrapper">
  <h3>タブ</h3>
  <div class="c-tab js-tab">
    <div class="c-tab__nav" role="tablist">
        <button class="c-tab__nav--item js-tab__item" role="tab" aria-selected="true" aria-controls="tab01" id="tab-nav01">tab01</button>
        <button class="c-tab__nav--item js-tab__item" role="tab" aria-selected="false" aria-controls="tab02" id="tab-nav02" tabindex="-1">tab02</button>
        <button class="c-tab__nav--item js-tab__item" role="tab" aria-selected="false" aria-controls="tab03" id="tab-nav03" tabindex="-1">tab03</button>
    </div>

    <div class="c-tab__pane js-tab__pane" tabindex="0" role="tabpanel" id="tab01" aria-labelledby="tab-nav01">
        <h4>Tab01</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto unde consequatur accusamus quae atque? Distinctio, necessitatibus impedit ipsum, itaque id corrupti error, sunt odio facere architecto suscipit aliquam veniam delectus?</p>
    </div>
    <div class="c-tab__pane js-tab__pane" tabindex="0" role="tabpanel" id="tab02" aria-labelledby="tab-nav02" hidden="hidden">
        <h4>Tab02</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto unde consequatur accusamus quae atque? Distinctio, necessitatibus impedit ipsum, itaque id corrupti error, sunt odio facere architecto suscipit aliquam veniam delectus?</p>
    </div>
    <div class="c-tab__pane js-tab__pane" tabindex="0" role="tabpanel" id="tab03" aria-labelledby="tab-nav03" hidden="hidden">
        <h4>Tab03</h4>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Architecto unde consequatur accusamus quae atque? Distinctio, necessitatibus impedit ipsum, itaque id corrupti error, sunt odio facere architecto suscipit aliquam veniam delectus?</p>
    </div>
  </div>

 </div>
              
            
!

CSS

              
                $tab-border-color:#eee;
$tab-border-radius:4px;
$tab-active-color:#3e82ff;

body{
  font-family: "Helvetica Neue",Arial,"Hiragino Kaku Gothic ProN","Hiragino Sans",Meiryo,sans-serif;
  color: #122322;
}
.wrapper{
  padding: 0 10px;
}

.c-tab{
	.c-tab__nav{
    display: flex;
    flex-wrap: wrap;
    margin-left: -2px;
    margin-right: -2px;
    margin-bottom: 20px;
    border-bottom: 1px solid $tab-border-color;
		.c-tab__nav--item{
      position: relative;
      -webkit-appearance: none;
      -ms-progress-appearance: none;
      appearance: none;
      cursor:pointer;
      margin: 0 2px -1px;
      background: none;
      display: block;
      text-align: left; 
      padding: 10px 20px;
      font-size: 14px;
      color:currentColor;
      border: 1px solid $tab-border-color;
      border-bottom-color: transparent;
      border-radius:$tab-border-radius $tab-border-radius 0 0;
      transition: background-color 0.3s cubic-bezier(0.19, 1, 0.22, 1), border-color 0.3s cubic-bezier(0.19, 1, 0.22, 1);
      &:hover{
        background-color: #f2f2f2;
      }
      &:focus:not(:focus-visible){
        outline: 0;
      }
      &.c-tab__active{
        background-color:#fff;
        color: $tab-active-color;
        border-color: $tab-border-color;
        border-bottom-color: #fff;
      }
      
    }
	}
	.c-tab__pane{
		  display: none;
      &.c-tab__active{
        display: block;
      }
	}
}
              
            
!

JS

              
                class Tab {
    constructor(){
        this.tab_wrapper = '.js-tab'
        this.tab_item = '.js-tab__item'
        this.tab_pane = '.js-tab__pane'
        this.active_class = 'c-tab__active'

        this.keys = {   
            "left": 37,
            "up": 38,
            "right": 39,
            "down": 40
        }
        this.init()
    }
    init(){
        this.tab = document.querySelectorAll(this.tab_wrapper);
        this.tab_navs = [];
    
        if(this.tab.length > 0){
            this.tab.forEach( (tab,i) => {
                const tab_items = tab.querySelectorAll( this.tab_item );
                this.tab_navs.push( tab_items )

                if(tab_items.length > 0){
                    tab_items.forEach( (tab_nav,index) => {
                        if(index == 0){
                            const pane_id = tab_nav.getAttribute('aria-controls');
                            this._open(tab_nav,pane_id)
                        }
                        this._addEvent(tab_nav,i,index,tab_items);
                    })
                }
            })
        }	
		
    }

    _addEvent (el,i,index,tab_items){
        
        el.addEventListener('click',(e) => {
            
            this._hide(i);

            const pane_id = el.getAttribute('aria-controls');
            this._open(el,pane_id)
        })
        el.addEventListener('keydown',(e)=>{
            const k = e.which || e.keyCode;
            let position = index
            if (k >= this.keys.left && k <= this.keys.down){
                if (k == this.keys.left || k == this.keys.up){
                    if (position > 0) {
                        e.preventDefault()
                        position--
                        tab_items[position].click()
                        tab_items[position].focus()
                    }
                }else if (k == this.keys.right || k == this.keys.down){
                    if (position < tab_items.length-1 ) {
                        e.preventDefault()
                        position++
                        tab_items[position].click()
                        tab_items[position].focus()
                    }
                }
            }
        })
    }
    _open(item,pane_id){
        const target_pane = document.querySelector( '#' + pane_id );
        if ( !target_pane ) return;

        item.classList.add(this.active_class);
        item.setAttribute("aria-selected",true)
        item.setAttribute("tabindex",0)
        target_pane.classList.add(this.active_class);
        target_pane.removeAttribute("hidden")
    }
    _hide (i){
        const tab = this.tab[i]
        const item_active = tab.querySelector(this.tab_item+'.'+this.active_class);
        const pane_active = tab.querySelector(this.tab_pane+'.'+this.active_class);
        if(item_active){
            item_active.classList.remove(this.active_class);
            item_active.setAttribute("aria-selected",false)
            item_active.setAttribute("tabindex","-1")
        }
        if(pane_active){
            pane_active.classList.remove(this.active_class);
            pane_active.setAttribute("hidden","hidden")
        }
    }
 }

document.addEventListener('DOMContentLoaded', ()=>{
  new Tab
})
              
            
!
999px

Console