Creating a dynamic matrix layout with CSS

On my e-commerce site's product display page, I am facing some alignment issues. There will be around 100 products on this page and I want to display them in a matrix format.

<div  style=" margin: 0 auto; width:640px; text-align: left">
     <div class="leftcol" >left column 1
           <p>my text</p>
           <p>my text</p>
     </div>
     <div class="leftcol" >right column
           <p>my textmy text my text my text</p>
     </div>   
     <div class="leftcol" >left column
           <p>my text</p>
           <p>my text</p>
           <p>my text</p>
           <p>my text</p>
           <p>my text</p>
     </div>
     <div class="leftcol" >right column
          <p>my textmy text my text my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
     </div>
     <div class="leftcol" >
           left column 2
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
          <p>my text</p>
    </div>

In my CSS:

.leftcol{width: 200px;background:yellow;margin-right:10px;float:left; }

All product divs are the same size of 200px, but the height of the divs will vary. I aim to display the divs similar to what is seen on Pinterest.

The display should look like this:

I would like to eliminate the gaps between the divs by filling them with the next set of divs. Visit this site for an example: [http://pinterest.com/][1] http://pinterest.com/

Answer №1

Similar to the question on how to get a fixed height div to continue on the next line, but not quite.

In order to meet your specific needs, you may find this example helpful: http://jsfiddle.net/bXvaj/

Answer №2

Both masonry and pinterest utilize absolute positioning to achieve the desired layout. It seems like a necessary approach for intricate designs that require responsiveness.

However, for a straightforward fixed-width layout, I would opt for the following structure:

<ul id="matte">
   <ul id="first-col">

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

   </ul>

   <ul id="second-col">

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

   </ul>


   <ul id="third-col">

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

      <ul class="product-cat">
         <li>...</li>
         <li>...</li>
         ...
      </ul>

   </ul>
</ul>

Therefore, by dividing the layout into columns and populating them based on the shortest one, you can avoid complex javascript calculations. This method could potentially work well with user-generated content, adapting based on the column length dynamically.

Answer №3

After struggling to align a matrix from the database using only CSS, it became clear that jQuery was necessary. Following the advice of "@Gaby aka G. Petrioli" in the comments, I decided to utilize their solution.

Check out masonry.desandro.com or isotope.metafizzy.co for further assistance.

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

Could someone offer some assistance in grasping the order of operations in jQuery syntax

Until now, my approach has been deciphering and analyzing various codes and techniques for effectively utilizing jQuery, but I often find myself overwhelmed by the choices presented. This often leads to unnecessary complexity in my code. What I aim to ach ...

Is Jquery not tallying up the numbers accurately?

Looking to secure a ticket with a complimentary offer? Here are the guidelines: A single individual can purchase 1 or more tickets, but the maximum is 4 An offer of at least 1 euro can be made for tickets, with no upper limit. For instance, if 4 tickets ...

Can you guide me on implementing an onclick event using HTML, CSS, and JavaScript?

I am looking for a way to change the CSS codes of the blog1popup class when the image is clicked. I already know how to do this using hover, but I need help making it happen on click instead. The element I want to use as the button; <div class=&quo ...

Troubleshooting: Magento checkout page keeps scrolling to the top

We are experiencing an issue where, during the one page checkout process, the next step is not automatically scrolling to the top of the page when it loads. After a user fills out all their billing information and clicks continue, the next step appears ha ...

What could be causing this JavaScript code to use 50% of the CPU and consume a significant amount of memory in the

I'm experiencing an issue with my banner rotator code. Here is the code snippet: function ban_rot() { //First preload images // counter var i = 0; // create object imageObj = new Image(); // set image list images = new A ...

Obtain the content from a dynamically generated dropdown menu and incorporate it into a separate file

My website features two dropdown menus - one for selecting countries and another for cities. The country menu is populated with data from a database, and once a country is selected, the city dropdown is dynamically filled with corresponding cities using a ...

The leaflet map I created is not showing up on my website for some reason

I am currently working on a project using ReactJS and NextJS. One issue I am facing is that my leaflet map is not filling the entire parent div on my page, and it is not showing up at all. I had expected the map to automatically adjust to the parent div s ...

How to center align a button in the footer of a Bootstrap card

I'm currently using Bootstrap 5.1.3 and facing a challenge in centering a button within the footer of a card. Below is the code snippet I am working with: CSS .btnStandard { min-width: 10vw; } HTML <div class="card-footer justify-content- ...

Tips on how to make a <li> element float independently without impacting other elements on the page

I have a dilemma with two divs in my HTML code. One is designated for the menu, and the other is simply for the title. The issue arises when I attempt to float the <li> items next to each other within the menu; they end up shifting downwards from the ...

What is the best method for adding lengthy text into a mySQL database with PHP?

My current setup involves using a basic HTML form and PHP to insert strings into a MySQL Database. This method works well for shorter strings, but I've encountered issues with longer strings. Interestingly, when using phpmyadmin, I can successfully i ...

Strange behavior of dropdown submenu

I've encountered an issue where my child's ul li elements are not behaving as expected, despite following advice from other sources. After days of debugging with no success, I am unable to pinpoint the problem. The image showcases the issue perfe ...

Retrieving precise passed arguments in the $(document).ready function

I am working on a gridview where I need the rows to expand and display the BatchID that is passed. Currently, I am using href="javascript:switchViews('div<%# Eval("BatchID")%>', 'one');" to pass this information, but I'm stru ...

The inline-block property can become unstable when dealing with a large number

When I create a parent container div with three child divs and set the display property to inline-block, everything looks great on jsfiddle CSS #container { border: 1px solid blue; padding: 2px; display: inline-block; } .child { width: ...

When clicking, populate the content of another div in a vertical top-to-bottom fashion

I am in the process of creating a functionality where clicking on one element will fill another element's content from bottom to top. Although I have managed to make it work on hover, the same functionality is not triggered when clicking on the eleme ...

When the "x" close icon is clicked, the arrow should toggle back to 0 degrees

I've been tackling the challenge of creating an accordion and I'm almost there. However, I'm facing an issue where the arrow doesn't return to its original position after clicking the close "x" icon. The toggle works fine but the arrow ...

Creating an SQL select statement with a three-level expression is a complex task that requires careful planning and

How can I create a SQL select query with 3 levels of expressions and statements? Typically, my website operates on an SQLite database and the search results are displayed using the following SQL query: "SELECT DISTINCT * FROM amz WHERE Title LIKE \" ...

Utilize CSS to position an element absolutely above the scrollbar

I'm encountering an issue with CSS absolute positioning, where the element appears above the scrollbar... Here is my code: document.addEventListener("DOMContentLoaded", () => { const h1 = overflow.querySelector("header h1") overflow.ons ...

Utilizing objects from a JSON file within an HTML document

I'm currently in the process of creating a comprehensive to-do list, and I want to establish a JSON file that will link all the items on the list together. Despite my efforts, I find myself uncertain about the exact steps I need to take and the speci ...

Display HTML Tables, along with the <tr>, <td>, and <a> tags in a Textview on an Android device

I am currently working on a chat app that retrieves HTML content as JSON via an API. One issue I am facing is how to properly display Table and tags within each chat message, as the current setup only allows for basic HTML elements to be displayed in a Tex ...

Obtaining select options in jQuery by utilizing the $(this) selector

Looking for a way to loop through and log each option available in a select dropdown box, but struggling with how to use $(this). This is the code I have so far: $('select').each(function(){ $(this).find('options').each(function() ...