Creating a visually striking layout with Bootstrap card columns masonry effect by seamlessly adjusting card heights to prevent any

When using the bootstrap card columns masonry and a user clicks on a button inside a card, the height of the card changes dynamically by adding a card-footer.

However, in certain situations, the cards change position causing a jumpy effect.

To see this issue in action, click on the select button of the first card in this JSFiddle. You will notice that the cards shift positions, especially when there are four cards lined up next to each other. (Adjusting the window size may help replicate the problem more easily)

https://jsfiddle.net/rvt8a690/2/

EDIT: If you're having trouble recreating the bug, try resizing the screen. I've also created a gif illustrating the issue: https://i.stack.imgur.com/29TXc.gif

$('.ccs-product-counter').on('click', function(e) {

  let selectedProductID = $(this).find('span.badge').attr("data-product-id");

  if (!$('#undo-p-id-' + selectedProductID).hasClass("UndoIsVisible")) {
    $('#undo-p-id-' + selectedProductID).removeClass('d-none');
    $('#undo-p-id-' + selectedProductID).addClass('fadeInDown');
    $('#undo-p-id-' + selectedProductID).addClass('UndoIsVisible');
    $('#card-p-id-' + selectedProductID).addClass('animationIsRunning');
    setTimeout(function() {
      $('#undo-p-id-' + selectedProductID).removeClass('fadeInDown');
      $('#card-p-id-' + selectedProductID).removeClass('animationIsRunning');
    }, 800);
  }

});
@media (min-width: 34em) {
  .card-columns {
    -webkit-column-count: 2;
    -moz-column-count: 2;
    column-count: 2;
  }
}

@media (min-width: 48em) {
  .card-columns {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;
  }
}

@media (min-width: 62em) {
  .card-columns {
    -webkit-column-count: 4;
    -moz-column-count: 4;
    column-count: 4;
  }
}

@media (min-width: 75em) {
  .card-columns {
    -webkit-column-count: 4;
    -moz-column-count: 4;
    column-count: 4;
  }
}
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container mt-3 mb-5">
  <div class="card-columns transaction">
    <div id="card-p-id-1" class="card product-card">
      <img class="card-img-top noselect" src="https://via.placeholder.com/120" />
      <div class="card-body">
        <h5 class="card-title mb-0">Title</h5>
        <p class="card-text mt-0">
          <i class="f-14">
                    Subtitle
                  </i>
        </p>
        <p class="card-text">
          Lorem ipsum dolor sit amet
        </p>
        <p>
          <strong>Price: 2$</strong>
          <br/>
          <strong>Stock: 5</strong>
        </p>
        <button type="button" class="btn btn-primary d-block center-margin-0-auto ccs-product-counter">Select <span id="add-p-id-1" class="badge badge-light" data-product-id="1">0</span>
              </button>
      </div>
      <div id="undo-p-id-1" class="card-footer text-muted text-center ccs-undo-selection animated d-none noselect">
        <div class="ccs-undo-selection-inner noselect">
          <i class="fas fa-eraser"></i> Undo selection
        </div>
      </div>
    </div>
    <!-- More card elements go here as needed -->
  </div>
</div>

Answer №1

The issue you're experiencing is likely due to the undo button being added at the bottom of your card. This can disrupt the layout of the card columns because they will rearrange themselves. To prevent this problem, consider placing a fixed undo button at the bottom of each card and keep it disabled until the user clicks on the select button. By implementing this quick solution, you can ensure that all card heights remain consistent and changes to the layout are avoided.

Answer №2

If you want to enhance your user experience, consider implementing this solution which allows you to dynamically display and hide buttons.

<button type="button" class="btn btn-primary center-margin-0-auto ccs-product-counter">Select <span id="add-p-id-1" class="badge badge-light" data-product-id="1">0</span></button> 

<button type="button" class="btn btn-secondary" style="float:right">Undo</button>

Answer №3

For the past few days, I've been struggling with a persistent issue in my code. Every time I tried to display things or create pop-ups, something would always go awry. But finally, after some trial and error, I believe I have stumbled upon the solution.

Within the card-columns style, I discovered an attribute named orphans with a value of 1. By changing it to orphans: unset;, I was able to prevent the cards from constantly readjusting themselves whenever the height changed.

If you're facing a similar dilemma, feel free to check out my ug9mLytojsFiddle for a clearer understanding.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Click the mouse to create a unique path from the list items within the <ul> <li> using jQuery

My current listing contains various files and folders: <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fon ...

Setting the default value for drop-down menus in jqGrid form editing

I have a data object with 3 attributes: ID Abbreviation Description In my jqGrid setup, I've configured the grid to display the Abbreviation. During editing (using the Form Edit feature), I populate the dropdown list with ID/Description pairs usin ...

I desire a three-column layout where the middle column will expand in width if columns 1 and 3 are vacant

I have a three-column layout designed without using tables. <div id="main"> <div id="left"></div> <div id="content"></div> <div id="right"></div> </div> This is the CSS code: #left {width: 200px;fl ...

When using Math.floor(Math.random() * val.length), the result will be a letter rather than a number

Looking to implement a feature where a different background image is displayed randomly upon page visit or refresh. Currently, specifying the images in an array like the example below works well, but I want to be able to pull from a folder. $(document).r ...

When using the GET method to load a PHP file via AJAX, the page may not display certain jQuery plugins even after the file

Hello, I am a beginner learning AJAX and JQuery. Maybe this is a silly question, but please bear with me. I am trying to dynamically show data without refreshing the page using AJAX. I have a function loaded in the body tag which handles this. Everything ...

Struggling to display Firebase Auth information resulting in 'undefined' value within React web application

When loading a user's profile page, I am trying to display their displayName and email information retrieved from Firebase Auth. I have implemented this logic within the 'componentDidMount' method by updating the state with the response dat ...

Moving Cursor Automatically to the End Upon Rejecting Input

I have a form input where users can enter values. I need to validate the inputs to ensure they only contain numbers, dots, and commas. If the input is invalid, I want to prevent it from being stored in the input field. To achieve this, I have implemented ...

What is the best way to delete markers from a leaflet map?

I need to remove markers from my map. I am looking to create a function that will specifically clear a marker based on its ID. I am utilizing Leaflet for the map implementation. Here is my function: public clearMarkers(): void { for (var id in this. ...

Unspecified tag name attribute in Vue that can be accessed in the browser

At the moment, I have encountered an issue with a selector in Vue returning undefined for {{ lens.price }}. However, when I use a = lens[1].getAttribute('price') in the browser console, it correctly displays the value as "0". Why is Vue showing ...

JavaScript array object alerts as empty

Having an issue sending a JavaScript array to my PHP as it appears to be empty []. This is not the usual JSON format that I have worked with in the past. Here is an example code snippet: var blah = []; var letters = ['a', 'b', &ap ...

Classy method for displaying AJAX data in a container

In my app, I often encounter situations where external data needs to be loaded. Currently, I am using AJAX to fetch the data and then inserting it into the JS file using $().html() function. This approach leads to messy HTML code within the JavaScript. $ ...

Display/Hide Location Pins on the Map

Looking for some assistance, please. I'm currently working on a script to toggle the visibility of locations on a map. The main code I've been using can be found here, and my own code is displayed below: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

An easy way to incorporate a fade-in effect for every word in a text

I am trying to make the text "Eat. Sleep. Repeat." slide up and fade in one word at a time. I have experimented with various methods like anime.js, keyframes, and adding css classes, but so far none of them have worked for me. Here is the code I currently ...

How can one gain access to a specifically generated element?

In my task of dynamically generating multiple elements, I am facing an issue related to accessing specific ones. To illustrate this problem as simply as possible, I have provided the following code: $(document).ready(function() { var num1 = 0; v ...

Setting up a simple configuration for WebGl and Javascript

I am currently using OSX 10.9.5 (Mavericks) and following a WebGL tutorial script provided on . I copied and pasted the code from the tutorial into TextEdit and saved it as HTML with the following file structure: webgl | +--index.html | ...

What is the best way to upload a local image using JavaScript for TensorFlow?

I am having trouble loading a data.jpg file to be used by tensorflow mobilenet. When I try to require the file normally, I encounter an error message: So, my question is how can I load an image in my JavaScript code for use? I have tried various methods of ...

Implementing JavaScript to Take an Array of Integers from an HTML Input Field and Sort It

Task: Retrieve 10 Array Values from HTML Input Field and Arrange Them in Ascending Order In order to add 10 values from an HTML input field to a JavaScript array, I have created an input field through which data is passed to the array in JS. Two labels ar ...

Height not being applied to DIV container

I'm facing an issue with my CSS code that is causing images to break the row after reaching the end of the DIV. However, the container behind it is not adjusting its height according to the images. The height should expand along with the images. Here ...

Choose the ul element with two distinct classes using Jquery

Visit this link for the source code I currently have a multi-level navigation bar that is solely CSS-based. I would like to implement JQuery functionality so that the dropdowns occur on tap or click events. Here's a snippet of the JQuery toggle fun ...

The state update does not seem to be affecting the DOM

I've implemented this component in my React app. It updates the state every second, but oddly enough the value <p>{this.state.time}</p> is not changing as expected. When I print the state value, it does change every second. import React f ...