Como fazer - Guias
Aprenda a criar abas com CSS e JavaScript.
Abas
As guias são perfeitas para aplicativos da Web de página única ou para páginas da Web capazes de exibir diferentes assuntos:
Londres
Londres é a capital da Inglaterra.
Criar guias alternáveis
Etapa 1) Adicionar HTML:
Exemplo
<!-- Tab links -->
<div class="tab">
<button class="tablinks" onclick="openCity(event,
'London')">London</button>
<button class="tablinks" onclick="openCity(event,
'Paris')">Paris</button>
<button class="tablinks" onclick="openCity(event,
'Tokyo')">Tokyo</button>
</div>
<!-- Tab content -->
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
</div>
<div
id="Paris" class="tabcontent">
<h3>Paris</h3>
<p>Paris
is the capital of France.</p>
</div>
<div id="Tokyo" class="tabcontent">
<h3>Tokyo</h3>
<p>Tokyo is the capital of Japan.</p>
</div>
Crie botões para abrir o conteúdo da guia específica. Todos os elementos <div> com class="tabcontent"
estão ocultos por padrão (com CSS e JS). Quando o usuário clica em um botão - ele abrirá o conteúdo da guia que "corresponde" a este botão.
Etapa 2) Adicionar CSS:
Estilize os botões e o conteúdo da guia:
Exemplo
/* Style the tab */
.tab {
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Style the buttons that are used to open the tab content */
.tab button {
background-color: inherit;
float: left;
border: none;
outline: none;
cursor: pointer;
padding: 14px 16px;
transition: 0.3s;
}
/* Change background color of buttons on hover
*/
.tab button:hover {
background-color: #ddd;
}
/* Create an active/current tablink class */
.tab button.active
{
background-color: #ccc;
}
/* Style the tab content */
.tabcontent {
display: none;
padding: 6px 12px;
border: 1px solid #ccc;
border-top: none;
}
Etapa 3) Adicione JavaScript:
Exemplo
function openCity(evt, cityName) {
// Declare all
variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent
= document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove
the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i <
tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to
the button that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
Fade em guias:
Se você quiser desvanecer o conteúdo da guia, adicione o seguinte CSS:
Exemplo
.tabcontent {
animation: fadeEffect 1s; /* Fading effect takes 1 second */
}
/* Go
from zero to full opacity */
@keyframes fadeEffect {
from
{opacity: 0;}
to {opacity: 1;}
}
Mostrar uma guia por padrão
Para abrir uma guia específica no carregamento da página, use JavaScript para "clicar" no botão da guia especificada:
Exemplo
<button class="tablinks" onclick="openCity(event, 'London')" id="defaultOpen">London</button>
<script>
// Get the element with id="defaultOpen" and click on it
document.getElementById("defaultOpen").click();
</script>
Fechar uma guia
Se você deseja fechar uma guia específica, use JavaScript para ocultar a guia com o clique de um botão:
Exemplo
<!-- Click on the <span> element to close the tab -->
<div id="London" class="tabcontent">
<h3>London</h3>
<p>London is the capital city of England.</p>
<span onclick="this.parentElement.style.display='none'">x</span>
</div>
Dica: confira também Como - Guias verticais .