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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <p class="warning">
  This is an old and outdated implementation. Check the <a href="https://codepen.io/gsarig/pen/ExKJEoM" target="_blank">updated pen</a> and <a class="more" href="https://www.gsarigiannidis.gr/tabs-on-desktop-accordion-on-mobile/?GWgNdx" target="_blank">read more about the logic</a>
</p>
<!-- THE MARKUP:

<article class="tabbed-content tabs-side">
  <nav class="tabs">
    <ul>
      <li><a href="#{ID}" class="active">{TITLE}</a></li>
    </ul>
  </nav>
  <section id="{ID}" class="item active" data-title="{TITLE}">
    <div class="item-content">
      {CONTENT}
    </div>
  </section>
</article>

-->

<h2>Tabs on top</h2>
<article class="tabbed-content">
  <nav class="tabs">
    <ul>
      <li><a href="#tab1" class="active">Tab 1</a></li>
      <li><a href="#tab2">Tab 2</a></li>
      <li><a href="#tab3">Tab 3</a></li>
    </ul>
  </nav>
  <section id="tab1" class="item active" data-title="Tab 1">
    <div class="item-content">
      <p>A simple solution for having tabbed content on a large screen but turn it into accordion on a smaller one. Just resize your screen below 800 pixels to see the tabbed interface transforming into an accordion.</p>
      <p>In order to keep the HTML as clean as possible, without duplicating elements, we use element attributes as CSS content to display the accordion tabs.</p>
    </div>
  </section>
  <section id="tab2" class="item" data-title="Tab 2">
    <div class="item-content">
      Tab 2 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
    </div>
  </section>
  <section id="tab3" class="item" data-title="Tab 3">
    <div class="item-content">
      Tab 3 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
    </div>
  </section>
</article>

<h2>Tabs on the side</h2>
<article class="tabbed-content tabs-side">
  <nav class="tabs">
    <ul>
      <li><a href="#side_tab1" class="active">Tab 1</a></li>
      <li><a href="#side_tab2">Tab 2</a></li>
      <li><a href="#side_tab3">Tab 3</a></li>
    </ul>
  </nav>
  <section id="side_tab1" class="item active" data-title="Tab 1">
    <div class="item-content">
      <p>This example brings the tabs at the left side by simply adding an extra class "tabs-side" at the ".tabbed-content" container. Just resize your screen below 800 pixels to see the tabbed interface transforming into an accordion.</p>
      <p>The change applies only on large screens. On smaller screens (in this example: below 800 pixels) it falls back to the same accordion we have on the previous example.</p>
    </div>
  </section>
  <section id="side_tab2" class="item" data-title="Tab 2">
    <div class="item-content">
      Tab 2 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
    </div>
  </section>
  <section id="side_tab3" class="item" data-title="Tab 3">
    <div class="item-content">
      Tab 3 content. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
    </div>
  </section>
</article>
              
            
!

CSS

              
                p.warning {
  text-align: center;
  line-height: 2;
  a {
    background: #3bacff;
    color: #fff;
    padding: .2rem .5rem;
    border-radius: 3px;
    white-space: nowrap;
    &.more {
      background: #45ba04;
    }
  }
}
/*
SCSS variables and mixins
*/
$color__main: #444;
$color__active: grey;
$background__active: #eee;
$width__tabs-side: 150px;
$breakpoint: 800px;
@mixin tab-active() {
  background: $color__main;
  color: $background__active;
}

/*
Some styles to make this demo look pretty (or at least decent)
If you want to style everything yourself, you shouldn't need these
*/
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,900');
body {
  padding: 1em;
  background: $background__active;
  color: $color__main;
  font-family: 'Roboto', sans-serif;
  font-weight: 100;
  a {
    text-decoration: none;
    color: $color__main;
  }
  p {
    margin-top: 0;
  }
  div {
    box-sizing: border-box;
  }
}
h2 {
  margin: 2em auto 0;
  text-align: center;
}
.tabbed-content {
  background: #fff;
  box-shadow: 1px 1px 6px #ccc;
  max-width: 600px;
  padding: 1em;
  margin: 1em auto;  
}
.tabs {
  ul {
    margin: 0;
    padding: 0 0 1em 0;
    font-weight: bold;
    li {
      background: $background__active;
      a {
        padding: .5em 1em;
        &:hover,
        &.active {
          @include tab-active();
        }
      }
    } 
  }
}
.item {
  margin-bottom: 2px;
  &::before {
    cursor: pointer;
    font-weight: bold;
    background: $background__active;
    padding: .5em;
    display: block;
  }
  &.active {
    &::before {
      @include tab-active();
    }
    .item-content {
      padding: 1em;
      -webkit-transition: opacity 0.3s ease-in-out;
      -moz-transition: opacity 0.3s ease-in-out;
      -o-transition: opacity 0.3s ease-in-out;
      -ms-transition: opacity 0.3s ease-in-out;
      transition: opacity 0.3s ease-in-out;
    }
  }
}
@media all and (min-width: $breakpoint ) {
  .item {
    &.active {
      .item-content {
        padding-top: 0;
      }  
    }
  }
  .tabs-side {
    .tabs {
      li {
        margin-bottom: 2px;
      }
    }
  }
}

/* 
The project specific CSS starts here
This is the minimum CSS that you will need in order for this to work
*/

// Accordion for small screens (mobile first principle)
.tabbed-content {
  .tabs {
    display: none;
  }
  .item {
    min-height: 2em; // We need a minimum height for each item or else the accordion buttons would dissapear
    &::before {
      content: attr(data-title); // Instead of polluting the HTML with a duplicate set of tabs, we get the accordion using the data-title attribute.
    }
    .item-content { // We hide the inactive content with zero opacity instead of "display:none" because this allows us to apply transition effects if we want. 
      opacity: 0;
      visibility: hidden;
      height: 0;
    }
    &.active {
      .item-content {
        opacity: 1;
        visibility: visible;
        height:auto;
      }
    }
  }  
}


// Tabs for larger screens
@media all and (min-width: $breakpoint) {
  .tabbed-content {
    .tabs {
      display: block;
      li {
        display: inline-block;
        a {
          display: block;
        }
      }
    }
    .item {
      min-height: 0;
      &::before {
        display: none;
      }
    }
    // Tabs on the side (works only on big screens)
    &.tabs-side {
      .tabs {
        width: $width__tabs-side;
        float: left;
        li {
          display: block;
        }
      }
      .item {
        margin-left: $width__tabs-side;
      }
    }  
  }
}
              
            
!

JS

              
                tabControl();

/*
We also apply the switch when a viewport change is detected on the fly
(e.g. when you resize the browser window or flip your device from 
portrait mode to landscape). We set a timer with a small delay to run 
it only once when the resizing ends. It's not perfect, but it's better
than have it running constantly during the action of resizing.
*/
var resizeTimer;
$(window).on('resize', function(e) {
  clearTimeout(resizeTimer);
  resizeTimer = setTimeout(function() {
    tabControl();
  }, 250);
});

/*
The function below is responsible for switching the tabs when clicked.
It switches both the tabs and the accordion buttons even if 
only the one or the other can be visible on a screen. We prefer
that in order to have a consistent selection in case the viewport
changes (e.g. when you esize the browser window or flip your 
device from portrait mode to landscape).
*/
function tabControl() {
  var tabs = $('.tabbed-content').find('.tabs');
  if(tabs.is(':visible')) {
    tabs.find('a').on('click', function(event) {
      event.preventDefault();
      var target = $(this).attr('href'),
          tabs = $(this).parents('.tabs'),
          buttons = tabs.find('a'),
          item = tabs.parents('.tabbed-content').find('.item');
      buttons.removeClass('active');
      item.removeClass('active');
      $(this).addClass('active');
      $(target).addClass('active');
    });
  } else {
    $('.item').on('click', function() {
      var container = $(this).parents('.tabbed-content'),
          currId = $(this).attr('id'),
          items = container.find('.item');
      container.find('.tabs a').removeClass('active');
      items.removeClass('active');
      $(this).addClass('active');
      container.find('.tabs a[href$="#'+ currId +'"]').addClass('active');
    });
  } 
}
              
            
!
999px

Console