JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/main.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tabs js</title>
</head>
<body>
<div class="tabs">
<div class="tabs__nav">
<button class="tabs__nav-btn" type="button" data-tab="#tab__first">Tab 1</button>
<button class="tabs__nav-btn" type="button" data-tab="#tab__second">Tab 2</button>
<button class="tabs__nav-btn" type="button" data-tab="#tab__third">Tab 3</button>
</div>
<div class="tabs__content">
<div class="tabs__item" id="tab__first">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
</div>
<div class="tabs__item" id="tab__second">
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English.</p>
</div>
<div class="tabs__item" id="tab__third">
<p>The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many...
CSS
body {
margin: 0;
background-color: #121212;
font-family: Roboto, sans-serif;
font-size: 16px;
line-height: 1.5;
}
p {
margin: 0 0 10px;
}
button {
cursor: pointer;
border: 0;
outline: none;
}
button:active, button:focus, button:hover {
border: 0;
}
.tabs {
max-width: 600px;
margin: 50px auto;
}
.tabs__nav {
display: flex;
}
.tabs__nav-btn {
padding: 15px 30px;
font-size: 20px;
background-color: #f5f5f5;
border: 0;
border-right: 1px solid #ddd;
cursor: pointer;
}
.tabs__nav-btn:last-child {
border-right: none;
}
.tabs__nav-btn:hover {
background-color: #ddd;
}
.tabs__nav-btn.active {
background-color: #5e3ddf;
border-right-color: #5e3ddf;
color: #fff;
}
.tabs__content {
background-color: #fff;
}
.tabs__item {
display: none;
}
.tabs__item.active {
display: block;
padding: 30px;
}
JavaScript
const tabsBtn = document.querySelectorAll(".tabs__nav-btn");
const tabsItems = document.querySelectorAll(".tabs__item");
tabsBtn.forEach(onTabClick);
function onTabClick(item) {
item.addEventListener("click", function() {
let currentBtn = item;
let tabId = currentBtn.getAttribute("data-tab");
let currentTub = document.querySelector(tabId);
if ( ! currentBtn.classList.contains('active') ) {
tabsBtn.forEach(function(item) {
item.classList.remove('active');
});
tabsItems.forEach(function(item) {
item.classList.remove('active');
});
currentBtn.classList.add('active');
currentTub.classList.add('active');
}
});
}
// document.querySelector('.tabs__nav-btn:nth-child(2)').click();