Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
How can I horizontally center these 3 buttons

At first there were 4 buttons I only wanted 3 so I removed the 4th one and that's why I want to make the remaining 3 look horizontally centered, it looks weird because of the 4th button's blank space. I tried a few things but they didn't work out.

var tabButtons = document.querySelectorAll(".tabContainer .buttonContainer button");
var tabPanels = document.querySelectorAll(".tabContainer  .tabPanel");

function showPanel(panelIndex, colorCode) {
  tabButtons.forEach(function(node) {
    node.style.backgroundColor = "";
    node.style.color = "";
  });
  tabButtons[panelIndex].style.backgroundColor = colorCode;
  tabButtons[panelIndex].style.color = "white";
  tabPanels.forEach(function(node) {
    node.style.display = "none";
  });
  tabPanels[panelIndex].style.display = "block";
  tabPanels[panelIndex].style.backgroundColor = colorCode;
}
showPanel(0, '#f44336');
.title {
  color: #dc2d5e;
  text-align: center;
}

.tabContainer {
  width: 100%;
  height: 350px;
}

.tabContainer .buttonContainer {
  height: 15%;
  align-items: center;
  align-content: center;
  justify-content: center;
  text-align: center;
}

.tabContainer .buttonContainer button {
  width: 25%;
  height: 100%;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 10px;
  font-family: sans-serif;
  font-size: 18px;
  background-color: #eee;
}

.tabContainer .buttonContainer button:hover {
  background-color: #d7d4d4;
}

.tabContainer .tabPanel {
  height: 85%;
  background-color: gray;
  color: white;
  text-align: center;
  padding-top: 105px;
  box-sizing: border-box;
  font-family: sans-serif;
  font-size: 22px;
  display: none;
}
<h1 class="title">Recieved Data</h1>
<div class="tabContainer">
  <div class="buttonContainer">
    <button onclick="showPanel(0,'#f44336')">Feedbacks</button>
    <button onclick="showPanel(1,'#4caf50')">Suggestions</button>
    <button onclick="showPanel(2,'#2196f3')">Messages</button>
  </div>
  <div class="tabPanel">Tab 1:Content</div>
  <div class="tabPanel">Tab 2:Content</div>
  <div class="tabPanel">Tab 3:Content</div>
</div>

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You are really close, adding these two flex properties can obtain the desired solution:

.tabContainer .buttonContainer{
  display: flex;
  justify-content: space-between;
}

.title{
    color: #dc2d5e;
    text-align: center;
}
.tabContainer{
    width: 100%;
    height: 350px;
}

.tabContainer .buttonContainer{
    display: flex;
    height: 15%;
    align-items: center;
    align-content: center;
    justify-content: space-between;
    text-align: center;
}

.tabContainer .buttonContainer button{
    width: 25%;
    height: 100%;
    float: left;
    border: none;
    outline:none;
    cursor: pointer;
    padding: 10px;
    font-family: sans-serif;
    font-size: 18px;
    background-color: #eee;
}
.tabContainer .buttonContainer button:hover{
    background-color: #d7d4d4;
}
.tabContainer .tabPanel{
    height: 85%;
    background-color: gray;
    color: white;
    text-align: center;
    padding-top: 105px;
    box-sizing: border-box;
    font-family: sans-serif;
    font-size: 22px;
    display: none;
}
<h1 class="title">Recieved Data</h1>
<div class="tabContainer">
    <div class="buttonContainer">
        <button onclick="showPanel(0,'#f44336')">Feedbacks</button>
        <button onclick="showPanel(1,'#4caf50')">Suggestions</button>
        <button onclick="showPanel(2,'#2196f3')">Messages</button>
    </div>
    <div class="tabPanel">Tab 1:Content</div>
    <div class="tabPanel">Tab 2:Content</div>
    <div class="tabPanel">Tab 3:Content</div>
</div>

This flexbox guide is my go-to any time I run into this type of issue!

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since there are only 3 buttons now, I would just make each one's width a third.

You can accomplish this by doing:

.tabContainer .buttonContainer button{
    width: calc(100% / 3);
}

EDIT: So it would look like this:

var tabButtons = document.querySelectorAll(".tabContainer .buttonContainer button");
var tabPanels = document.querySelectorAll(".tabContainer  .tabPanel");

function showPanel(panelIndex, colorCode) {
  tabButtons.forEach(function(node) {
    node.style.backgroundColor = "";
    node.style.color = "";
  });
  tabButtons[panelIndex].style.backgroundColor = colorCode;
  tabButtons[panelIndex].style.color = "white";
  tabPanels.forEach(function(node) {
    node.style.display = "none";
  });
  tabPanels[panelIndex].style.display = "block";
  tabPanels[panelIndex].style.backgroundColor = colorCode;
}
showPanel(0, '#f44336');
.title {
  color: #dc2d5e;
  text-align: center;
}

.tabContainer {
  width: 100%;
  height: 350px;
}

.tabContainer .buttonContainer {
  height: 15%;
  align-items: center;
  align-content: center;
  justify-content: center;
  text-align: center;
}

.tabContainer .buttonContainer button {
  width: calc(100% / 3);
  height: 100%;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 10px;
  font-family: sans-serif;
  font-size: 18px;
  background-color: #eee;
}

.tabContainer .buttonContainer button:hover {
  background-color: #d7d4d4;
}

.tabContainer .tabPanel {
  height: 85%;
  background-color: gray;
  color: white;
  text-align: center;
  padding-top: 105px;
  box-sizing: border-box;
  font-family: sans-serif;
  font-size: 22px;
  display: none;
}
<h1 class="title">Recieved Data</h1>
<div class="tabContainer">
  <div class="buttonContainer">
    <button onclick="showPanel(0,'#f44336')">Feedbacks</button>
    <button onclick="showPanel(1,'#4caf50')">Suggestions</button>
    <button onclick="showPanel(2,'#2196f3')">Messages</button>
  </div>
  <div class="tabPanel">Tab 1:Content</div>
  <div class="tabPanel">Tab 2:Content</div>
  <div class="tabPanel">Tab 3:Content</div>
</div>

calc() is widely supported on modern browsers.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You just need to add this to your css !

.buttonContainer{
    display: flex;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda