Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Custom Tabs</title> <style> .tabs { display: flex; gap: 10px; } .tab-button { padding: 10px 20px; cursor: pointer; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 5px; } .tab-button.active { background-color: #fff; border-bottom: 2px solid #000; } .tab-content { display: none; margin-top: 20px; } .tab-content.active { display: block; } </style> </head> <body> <div class="tabs"> <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</p> </div> <div class="tab-content" id="tab2"> <p>Content for Tab 2</p> </div> <div class="tab-content" id="tab3"> <p>Content for Tab 3</p> </div> <script> document.querySelectorAll('.tab-button').forEach(button => { button.addEventListener('click', function() { const tabId = this.getAttribute('data-tab'); document.querySelectorAll('.tab-button').forEach(btn => btn.classList.remove('active')); document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active')); this.classList.add('active'); document.getElementById(tabId).classList.add('active'); }); }); </script> </body> </html>