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.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.