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="widget-wrap">
  <div id="tabs"></div>
  <h1>JS CSS TABS</h1>
  
  <!-- (A) TABS WRAPPER -->
  <div class="tabs">
    <!-- (A1) HEADERS -->
    <ul class="tabHead">
      <li>First</li>
      <li>Second</li>
      <li>Third</li>
    </ul>

    <!-- (A2) CONTENTS -->
    <ul class="tabBody">
      <li>First content.</li>
      <li>Second content.</li>
      <li>Third content.</li>
    </ul>
  </div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-tabs-html-css-js/"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) WRAPPER & SHARED */
.tabs {
  max-width: 600px; /* optional */
  border: 1px solid #d9d9d9;
}
ul.tabHead, ul.tabBody {
  list-style: none;
  padding: 0; margin: 0;
}

/* (B) HEADER TABS */
.tabHead {
  display: flex;
  border-bottom: 1px solid #d9d9d9;
  background: #efefef;
}
.tabHead li {
  padding: 10px 20px;
  cursor: pointer;
}
.tabHead li.open {
  color: #fff;
  background: #2a87d7;
}

/* (C) CONTENT TABS */
.tabBody li {
  background: #fff;
  display: none;
  padding: 20px;
  max-height: 300px; overflow: auto; /* optional */
} 
.tabBody li.open { display: block; }


/* (X) DOES NOT MATTER */
/* PAGE & BODY */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1529697216570-f48ef8f6b2dd?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MzEyMjMwNw&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  min-width: 600px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.4);
}

/* SVG */
#tabs {
  width: 100%; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 512 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M96 288H32c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32zm160 0h-64c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32zm160 0h-64c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32zM96 96H32c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32zm160 0h-64c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32zm160 0h-64c-17.67 0-32 14.33-32 32v64c0 17.67 14.33 32 32 32h64c17.67 0 32-14.33 32-32v-64c0-17.67-14.33-32-32-32z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 50px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
#code-boxx , h1 { text-align: center; }
              
            
!

JS

              
                function tabify () { for (let tab of document.querySelectorAll(".tabs")) {
  // (A) GET HEADERS & BODY
  let htabs = tab.querySelectorAll("ul.tabHead > li"),
      btabs = tab.querySelectorAll("ul.tabBody > li");
  if (htabs.length != btabs.length) {
    console.error("Number of tabs do not match!");
    continue;
  }

  // (B) CLICK TO TOGGLE TAB
  for (let i=0; i<htabs.length; i++) {
    htabs[i].onclick = () => {
      for (let j=0; j<htabs.length; j++) {
        if (i==j) {
          htabs[j].classList.add("open");
          btabs[j].classList.add("open");
        } else {
          htabs[j].classList.remove("open");
          btabs[j].classList.remove("open");
        }
      }
    };
  }

  // (C) OPEN FIRST TAB BY DEFAULT - IF NONE IS OPEN
  if (tab.querySelector(".open") == null) {
    htabs[0].classList.add("open");
    btabs[0].classList.add("open");
  }
}}

// (D) ATTACH TABS
window.onload = tabify;
              
            
!
999px

Console