Organize objects of varying heights in a grid formation

Looking for a solution to arrange UI components of varying dimensions in a grid layout? The challenge is to have each row be equal to the height of the tallest component in that row, with no fixed number of columns. Components should maintain their original dimensions and automatically wrap to the next row when needed.

Bootstrap requires specifying column widths like col-xs-number, but it's not feasible when the number of items per row is dynamic.

Note: The application uses bootstrap version 3 and cannot upgrade to V4.

Wondering if there's a way to achieve this design?

Answer №1

You don't need to rely on Bootstrap for this, simply utilize display: inline-block for the elements.

Here is a demonstration where I have included 12 elements with different heights in a container div. The width of the container div changes randomly each time the demo runs to showcase how the elements wrap differently each time.

var heights = [100, 20, 80, 140, 65, 90, 30, 70, 130, 55, 75, 100];
for (var i = 0; i < heights.length; i++) {
  $('#x').append($('<div style="height:' + heights[i] + 'px"></div>'))
}
var w = Math.floor((Math.random() * 400) + 100);
$('#x').css("width", w + "px");
#x div {
  display: inline-block;
  vertical-align: text-top;
  width: 50px;
  background: green;
  margin: 10px;
}

#x {
  border: solid 1px #888
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<div id="x"></div>
<hr>

Answer №2

Try using flexbox for creating columns in Bootstrap 4 without specifying numbers.

For example, you can achieve this with the following CSS:

.container{
  border:1px solid black;
}

.col:first-of-type{
  background:white;
}

.col:nth-of-type(2){
  background:green;
}

.col:last-of-type{
  background:red;
}

And here is how you would structure your HTML:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class='container'>
  <div class="row">
    <div class="col">First</div>
    <div class="col">Second</div>
    <div class="col">Third</div>
  </div>
</div>

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

How can I implement a thumbnail editing feature similar to Facebook's display image uploader in an Asp.net application using c#?

How can I upload an image and display a specific part of it in a thumbnail of a custom size? I also want to personalize the appearance of the thumbnail. ...

arranging elements within a container

<td id="name_3869-0_0" style="position: relative; " colspan="4"> <div style="float:left;width:100%"> <img src="/img/1.gif" align="middle" style="margin-right: 10px;" class="company_symbol" border="0"> <strong style= ...

Is there a way to switch the classList between various buttons using a single JavaScript function?

I'm currently developing a straightforward add to cart container that also has the ability to toggle between different product sizes. However, I am facing difficulties in achieving this functionality without having to create separate functions for ea ...

The 'float' property in HTML and CSS

Good day everyone! I apologize in advance if my explanation is not very clear. Below is my code along with a link to an image that will help illustrate the issue I am facing. Here is the code snippet: <header> <div> <a href="index.html ...

Using an SVG filter to create a solid blue color overlay on an image

For a while now, I've been applying -webkit-filter: grayscale(100%); to render an image in grayscale. But recently, I've been wondering if it's possible to tint the image blue instead. My initial thought was to utilize an SVG filter and refe ...

Codepen fails to function properly after being uploaded to a server

I have encountered an issue with my code on CodePen. After exporting it as a .zip file and attempting to run it on a local server, it failed to work. I am unsure of the exact reason behind this. I made sure to include all the necessary dependencies in the ...

What is the best way to ensure that text and SVG elements are properly aligned?

I am facing an issue where the text is not aligned with the SVG icons in my project. Here is an example of the code causing the problem: <span class="cardSpan"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fi ...

Using Sass to Loop through Specific Items

Currently, I am in the process of developing a SASS loop to iterate over a series of images (let's say 50). My goal is to increment the transition delay by 50ms for every nth-of-type image. However, it appears that the current for loop I have implemen ...

The navbar slide toggle is supposed to slide behind the navbar, but instead it slides in front of

Recently, I created an animated navbar that works perfectly fine. However, there is one issue that bothers me - when the list of menu items slides open, it appears in front of the navbar instead of behind it. If you'd like to see the problem in actio ...

The dropdown div does not activate when the image is clicked

When the image in the simple dropdown is clicked, it fails to activate the dropdown feature. This issue was encountered while working on a Shopify project, making it challenging to troubleshoot. To demonstrate the problem, I have re-created the scenario i ...

Challenges that arise from IE6 css issues

I am encountering an issue with the jquery slider in IE6. To see the problem, please visit the link below and click on the "dodaj u košaricu" button located at the bottom right. Then, navigate to the "košarica" tab and attempt to move the slider up and d ...

When the hover effect is triggered, the background animation smoothly resets back to its initial position once the

I have a picture with a clear background that I want to add a CSS3 background animation to when the mouse hovers over it. Here is the CSS code I am currently using: @keyframes in { from {background-color: #fff;} to {background-color: #900;} } @-webkit-k ...

Issues with implementing CSS Icon Generated Content on a live Azure Web Role

My dilemma lies with my MVC4 web application that I'm attempting to deploy as an Azure web role. The site incorporates Metro UI CSS. While the deployed web role is functioning properly overall, I am encountering an issue with the CSS-generated conten ...

Styling WordPress Ninja Tables with CSS

I am currently seeking a solution to customize each table generated through CSS. In the past, I have utilized tools like TablePress which assigned a unique "id" to each table, allowing for individual customizations when inspecting through Google Chrome - f ...

Firefox: log shows no CSS errors or warnings

I decided to play around and create a website with a completely made-up CSS code: a { foobar: 8888; } Upon opening the page in Firefox, I checked the web console expecting to see errors or warnings. However, surprisingly, there were none. Why is tha ...

Organize and eliminate items within a vessel

Looking to manipulate a container with span elements by removing two and re-arranging one. I attempted the following approach: $('span.class5').insertBefore('span.class0'); However, this inserted each span.class5 into every repeating ...

Eliminate any undefined data in the popup map of Javascript

I am working with JSON data that is being displayed in a pop-up on a map. In cases where there is missing data (Visibility), the word undefined appears in the pop-up. Is there a way to remove the undefined text so that it does not show up in the pop-up? ...

What is causing Mag Pagination and Mag Sort to malfunction?

Struggling to incorporate pagination and sorting in the frontend after retrieving data from nodejs. Trying to follow a guide per this link: here While attempting to apply a similar logic, there seems to be an issue as it's not functioning correctly. ...

Unable to click on links due to issues with the CSS and HTML

On my page at , there are links on the right side that I am unable to click. Does anyone know why this might be happening? Content on the Left Side: <nav class="woocommerce-breadcrumb" itemprop="breadcrumb"><a class="home" href="http://artendije ...

The position of the <input> element shifts once the user provides input

I'm currently developing a web-based self-quizzing application, but I've encountered an issue with my input form. Each input field is placed in its own cell within a table for organization purposes. To maintain an aesthetically pleasing structure ...