Tabbed Content with CSS and JavaScript

Tabs are typically used to display the content of different subjects in the same section. Mainly two types of tabs are used in the web application, horizontal and vertical tabs. HTML tabs element can be used to manage space and make the web page user-friendly. You can display content from different sections without reloading the page multiple times.

The tabbed content is very useful to allow the user to navigate content with the tab menu on a single-page website. In this example code snippet, we will create horizontal tabs with CSS and JavaScript.

HTML Code:

<div class="wrapper">
    <div class="tabs">
        <h3 class="active">Tab 1</h3>
        <h3>Tab 2</h3>
        <h3>Tab 3</h3>
    </div>
    <div class="tab-content">
        <div class="active">
            <h4>First Tab Title</h4>
            <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
        </div>
        <div>
            <h4>Second Tab Title</h4>
            <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.</p>
        </div>
        <div>
            <h4>Third Tab Title</h4>
            <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures.</p>
        </div>
    </div>
</div>

JavaScript Code:

JAVASCRIPT
content_copyCopy
let tabs = document.querySelectorAll(".tabs h3");
let tabContents = document.querySelectorAll(".tab-content div");

tabs.forEach((tab, index) => {
    tab.addEventListener("click", () => {
        tabContents.forEach((content) => {
            content.classList.remove("active");
        });
        
        tabs.forEach((tab) => {
            tab.classList.remove("active");
        });
        
        tabContents[index].classList.add("active");
        tabs[index].classList.add("active");
    });
});

CSS Code:

.wrapper {
    max-width: 50rem;
    width: 100%;
    margin: 0 auto;
    background-color: #ffffff;
    box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
}
.tabs {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
}
.tabs .active {
    background-color: #e8e5e5;
    color: #16a085;
}
.tabs h3 {
    background-color: #1abc9c;
    color: #fff;
    text-align: center;
    margin: 0;
    padding: 15px 0;
    cursor: pointer;
    font-weight: 600;
    border-right: 0.125rem solid #16a085;
}
.tabs h3:first-child{
    margin-left: -1px;
}
.tabs h3:last-child{
    border-right: none;
    margin-right: -1px;
}
.tabs h3:hover {
    background-color: #e8e5e5;
    color: #16a085;
}
.tab-content {
    background-color: #ffffff;
    padding: 30px 25px;
}
.tab-content h4 {
    font-size: 28px;
    margin-bottom: 20px;
    margin-top: 0;
    color: #000224;
    font-weight: 600;
}
.tab-content p {
    text-align: justify;
    line-height: 1.9;
    letter-spacing: 0.4px;
    color: #202238;
}
.tab-content div {
    display: none;
}
.tab-content .active {
    display: block;
}

keyboard_double_arrow_up