About this snippet

Create a basic tabbed content interface using HTML, CSS, and a little JavaScript for toggling visibility. This is a common UI pattern for organizing content effectively without complex libraries.

Size
2.4 KB
Uses
HTML, CSS, JavaScript
Loads
Nothing external
Views
337
<div class="tabs-container">
  <div class="tab-buttons">
    <button class="tab-button active" data-tab="tab1">Tab 1</button>
    <button class="tab-button" data-tab="tab2">Tab 2</button>
    <button class="tab-button" data-tab="tab3">Tab 3</button>
  </div>
  <div class="tab-content active" id="tab1">
    <p>Content for Tab 1. This is the initially visible tab content.</p>
  </div>
  <div class="tab-content" id="tab2">
    <p>Content for Tab 2. This section becomes visible when Tab 2 is clicked.</p>
  </div>
  <div class="tab-content" id="tab3">
    <p>Content for Tab 3. Each tab can hold different information.</p>
  </div>
</div>
body {
  font-family: Arial, sans-serif;
  padding: 20px;
  background-color: #f9f9f9;
  display: flex; justify-content: center; align-items: center; min-height:100vh;
}
.tabs-container {
  max-width: 600px;
  width: 100%;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  overflow: hidden;
}
.tab-buttons {
  display: flex;
  background-color: #eef2f5;
}
.tab-button {
  padding: 15px 20px;
  cursor: pointer;
  border: none;
  background-color: transparent;
  font-size: 1em;
  color: #555;
  transition: background-color 0.3s, color 0.3s;
  flex-grow: 1; /* Distribute space equally */
  text-align: center;
  border-bottom: 3px solid transparent;
}
.tab-button:hover {
  background-color: #dfe6eb;
  color: #333;
}
.tab-button.active {
  color: #007bff;
  border-bottom: 3px solid #007bff;
  font-weight: bold;
}
.tab-content {
  display: none;
  padding: 20px;
  line-height: 1.6;
  color: #333;
  animation: fadeInContent 0.5s ease;
}
.tab-content.active {
  display: block;
}
@keyframes fadeInContent {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}
document.addEventListener('DOMContentLoaded', function() {
  const tabButtons = document.querySelectorAll('.tab-button');
  const tabContents = document.querySelectorAll('.tab-content');

  tabButtons.forEach(button => {
    button.addEventListener('click', () => {
      // Remove active class from all buttons and contents
      tabButtons.forEach(btn => btn.classList.remove('active'));
      tabContents.forEach(content => content.classList.remove('active'));

      // Add active class to the clicked button and corresponding content
      button.classList.add('active');
      const tabId = button.getAttribute('data-tab');
      document.getElementById(tabId).classList.add('active');
    });
  });
});
Esc
↑↓ moveEnter select

Start from a template

Open an .html file, or HTML, CSS, JS, Markdown, Less or TypeScript files. You can also drop them here. Styles and scripts go into their panes.

Share

Source

Keyboard shortcuts

Save