javascripthtmlbootstrap-4bootstrap-5bootstrap-grid

Bootstrap Grid Row does not align center if hidden then shown with JS


Apologies in advance for the wonky indentation.

I have a simple Bootstrap Grid setup like so

<div class="row justify-content-center" align="center" id="details">
    <div class="col-sm-2">
        <div class="card">
            <div class="card-body">
                <h4><?php echo getDAtimeWorked($envelope); ?></h4>
                 <h5 id=""><?php echo getDArecords($envelope); ?></h5>
                  </div>
                </div>
            </div>
            <div class="col-sm-2">
                <div class="card">
                  <div class="card-body">
                    <h4><?php echo getTNtimeWorked($envelope); ?></h4>
                    <h5 id=""><?php echo getTNrecords($envelope); ?></h5>
                  </div>
                </div>
            </div>
            <div class="col-sm-2">
                <div class="card">
                  <div class="card-body">
                    <h4><?php echo getMHtimeWorked($envelope); ?></h4>
                    <h5 id=""><?php echo getMHrecords($envelope); ?></h5>
                  </div>
                </div>
            </div>
            <div class="col-sm-2">
                <div class="card">
                  <div class="card-body">
                    <h4><?php echo getLTtimeWorked($envelope); ?></h4>
                    <h5 id=""><?php echo getLTrecords($envelope); ?></h5>
                  </div>
                </div>
            </div>
            <div class="col-sm-2">
                <div class="card">
                  <div class="card-body">
                  </div>
                </div>
            </div>
        </div>

function showDetails() {
  var x = document.getElementById("details");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

When I click on that cell to toggle show/hide, the hidden and then shown content is no longer aligned center. How can I get it to?

My end goal for transparency is to hide that content on page load and reveal it onclick.


Solution

  • In your js, you're switching it to block, x.style.display = "block" , so the class justify-content-center stops working because it's not a flex anymore.

    Try x.style.display = "flex" instead.