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

122
Vistas
Javascript resets my existing CSS alignment

Below is the some javascript button I've written to hide/show the table. By default table is centered with margin: auto, and it works.

But, whenever I click the button to hide/show table, it resets the auto margins, and left-aligns the table.

If I disable the display:none, it shows table centered, and if I remove the javascript it displays it normally. I've tried setting it again with

document.getElementById("vcontrols").style.margin = "0 auto"

But it doesn't work

Here's the CSS used.

    <style>
        body {
            background-color: #111;
        }

        .container {
            max-width: 80%;
            margin: auto;
            background: #333;
            color: #fff;
            text-align: center;
        }

        .tabela {
            margin: 0 auto;
        }

        #vcontrols {
            display: none;
        }
</style>

Here's the script

 function toggleControls() {
    var Skrivalica = document.getElementById('vcontrols');
    var displaySetting = Skrivalica.style.display;
    var hideshow = document.getElementById('hideshow');

    if (displaySetting == 'block') {
      Skrivalica.style.display = 'none';
      hideshow.innerHTML = 'Full Details';
    }
    else {
      Skrivalica.style.display = 'block';
      hideshow.innerHTML = 'Hide Details';
    }
  }

And Here's the main body, table

        <div class="container">
        <button onclick="toggleControls()" id="hideshow">Full Details</button>
        <table class="tabela" id="vcontrols">
            <tr>
                <th>Action 1</th>
                <th>Action 2</th>
                <th>Action 3</th>
                <th>Action 4</th>
                <th>Action 5</th>
            </tr>
            <tr>
                <td>Item 1</td>
                <td>Item 2</td>
                <td>Item 3</td>
                <td>Item 4</td>
                <td>Item 5</td>
            </tr>
        </table>
    </div>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function toggleControls() {
    var Skrivalica = document.getElementById('vcontrols');
    var displaySetting = Skrivalica.style.display;
    var hideshow = document.getElementById('hideshow');
    
    if (displaySetting === 'block') {
      Skrivalica.style.display = 'none';
      hideshow.textContent = 'Full Details';
    }
    else {
      Skrivalica.style.display = 'block';
      hideshow.textContent = 'Hide Details';
    }
}
    body {
        background-color: #111;

    }
    
    .btn-container {
        display: flex;
        justify-content: center;
    }

    .container {
        max-width: 80%;
        margin-left: auto;
        margin-right: auto;
        background: #333;
        color: #fff;
        text-align: center;
        display: flex;
    }

    .tabela {
        margin: 0 auto;
    }

    #vcontrols {
        display: none;
    }
<div class="btn-container">
  <button onclick="toggleControls()" id="hideshow">Full Details</button>
  </div>
  <div class="container">
    <table class="tabela" id="vcontrols">
        <tr>
            <th>Action 1</th>
            <th>Action 2</th>
            <th>Action 3</th>
            <th>Action 4</th>
            <th>Action 5</th>
        </tr>
        <tr>
            <td>Item 1</td>
            <td>Item 2</td>
            <td>Item 3</td>
            <td>Item 4</td>
            <td>Item 5</td>
        </tr>
    </table>
</div>

Maybe an easier way but I changed up some HTML and CSS. Left the JS alone. Seemed to be working for me. Let me know if its not what you wanted!

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