Simple Tabs in HTML CSS and JavaScript

Simple Tabs in HTML CSS and JavaScript
Code Snippet:Tabs - HTML, CSS and pure Javascript
Author: Diogo
Published: January 11, 2024
Last Updated: January 22, 2024
Downloads: 4,021
License: MIT
Edit Code online: View on CodePen
Read More

Yet another simple tabs code snippet that is written in HTML, CSS, and JavaScript. It renders simple tabs penal for smoothly navigating site content. The tabs interface comes with a simple design that can be customized according to your needs.

How to Create Simple Tabs in HTML CSS and JavaScript

1. First of all, create the HTML structure as follows:

 <ul id="abas" class="teste">
  <li class="selecionada"><a id="aba_1" href="#aba_1">Tab 1</a></li>
  <li><a id="aba_2" href="#aba_2">Tab 2</a></li>
  <li><a id="aba_3" href="#aba_3">Tab 3</a></li>
</ul>
<div id="conteudos">
  <div id="conteudo_1" class="conteudo visivel">
    <p><b>What is CodeHim?</b></p>
    <p> CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts.</p>
  </div>
  <div id="conteudo_2" class="conteudo">
    <p>Conteúdo da Aba 2</p>
  </div>
  <div id="conteudo_3" class="conteudo">
    <p>Conteúdo da Aba 3</p>
  </div>
</div>
<!-- partial -->
  <script  src="./js/script.js"></script>

2. After that, add the following CSS styles to your project:

body{
  font-family: Verdana, Arial, calibri;
  font-size: 16px;
  color: #666666;
}
#abas{
  margin:0;
  padding:0;
}
#abas li{
  display:inline-block;
  border: 1px solid #CCCCCC;
  border-bottom:none;
  background-color:#EEEEEE;
}
#abas li a{
  padding: 10px 20px;
  display: block;
  text-decoration:none;
  color:#000000;
}
#abas li.selecionada{
  background-color:#FFFFFF;
  position:relative;
  top:1px;
  font-weight:bold;
}
#abas li.selecionada a{
  padding-top: 11px
}
.conteudo{
  display:block;
  padding:10px;
  border: 1px solid #CCCCCC;
  display:none;
}
.conteudo.visivel{
  display:block;
}

3. Finally, add the following JavaScript code and done.

var abas = document.getElementById("abas");
var conteudos = document.getElementById("conteudos");

/* Essa função retira a classe "selecionada" e esconde a DIV com o conteúdo visível */
function limparSelecao(){
  abas.getElementsByClassName("selecionada")[0].classList.remove("selecionada");
  conteudos.getElementsByClassName("visivel")[0].classList.remove("visivel");
}

/* Essa é executada quando alguma das abas é clicada */
abas.addEventListener("click", function(event){
  var abaClicada = event.target.id;
  var itemSelecionado = abaClicada.substring(abaClicada.lastIndexOf("_"));

  /* Chama função que tira a seleção do item atual */
  limparSelecao();

  /* Insere a classe "selecionada" na nova aba visível */
  event.target.parentElement.classList.add("selecionada");

  /* Insere a classe "visivel" para o conteúdo da aba selecionada */
  conteudos.querySelector("#conteudo"+ itemSelecionado).classList.add("visivel");
});

That’s all! hopefully, you have successfully integrated this simple tabs code snippet into your project. If you have any questions or facing any issues, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X