Create a visually appealing masonry grid layout by using CSS column-count and horizontal alignment

I have successfully created a masonry grid using the column-count property with 4 columns. However, all the div elements in the grid are displayed vertically.

Is there a way to change this layout and display them horizontally using JavaScript or jQuery?

Here is an example of the code: https://codepen.io/anon/pen/EzMbPo

<div id="categories">
    <div class="item">
      <p>1</p>
    </div>
    <div class="item">
      <p>2</p>
    </div>
    <div class="item">
      <p>3</p>
    </div>
    <div class="item">
      <p>4</p>
    </div>
    <div class="item">
      <p>5</p>
    </div>
    <div class="item">
      <p>6</p>
    </div>
    <div class="item">
      <p>7</p>
    </div>

Answer №1

To showcase them side by side, you can incorporate the display: flex property to the categories div, like so:

#categories{
  display: flex;
  -webkit-column-count: 4;-moz-column-count: 4;column-count: 4;
    -webkit-column-gap:1em;-moz-column-gap:1em;column-gap: 1em;
}

The default behavior of flex is to exhibit items in a row (i.e., horizontally).

For more details, refer to this link: https://developer.mozilla.org/en-US/docs/Web/CSS/flex

Edit:

Utilizing grid might be a better fit for your issue. If you attempt the following approach, it should function correctly:

#categories{
  -webkit-column-count: 4;
  -moz-column-count: 4;
  column-count: 4;
  -webkit-column-gap:1em;
  -moz-column-gap:1em;
  column-gap: 1em;
  display: grid;
  grid-template-rows: repeat(auto-fit, minmax(80px, 1fr));
  grid-auto-rows: 80px;
  grid-auto-flow: dense;
}

.item{
    display: inline-block;
    margin: 0 0 1em;
    padding:1em;
    width: 100%;
    border:3px solid #fff;
    border-radius: 30px;
    height:100px;
}

.item p{color:#59a3b6;text-align:center;font-weight:500;}

#categories .item:nth-child(1n+2){
  height:200px; 
  grid-row: span 1; 
  grid-column: span 2;}

#categories .item:nth-child(2n){height:250px; grid-row: span 2; grid-column: span 1;}

#categories .item:nth-child(3n){
  height:120px;
  grid-row: span 4; 
  grid-column: span 4;
}

#categories .item:nth-child(4n){
  height:140px;
  grid-row: span 1; 
  grid-column: span 1;
}

#categories .item:nth-child(5n){
  height:300px;
  grid-row: span 3; 
  grid-column: span 6;
}

In essence, I introduced display: grid; to the parent element and applied

grid-row: span 1; grid-column: span 2;
to the child elements. You may need to experiment with it, but it should set you on the right path towards achieving your desired layout.

Watch this video for a comprehensive explanation of the concept.

I hope this guidance proves useful. Best of luck!

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

Modify the color of the title in a bootstrap vue tab

Utilizing bootstrap-vue.js, I created a tab that looks like this: https://i.sstatic.net/EW76k.png The default color of the tab title doesn't fit with my project theme, so I attempted to change it. I found information on how to modify the tab title in ...

Ways to create spacing between vertically stacked columns in Bootstrap

I have created a website using Bootstrap and PHP to display data from 18 categories in columns. The code for listing the categories in a single row is shown below: <div class="row"> <?php foreach($categories as $val){?> <div class="col ...

What could be the reason behind the unexpected outcome of process.argv[3] when its value is set to '*'?

My calculator app is very straightforward: if (process.argv[3]==='+') console.log(parseInt(process.argv[2]) + parseInt(process.argv[4])); if (process.argv[3]==='-') console.log(parseInt(process.argv[2]) - parseInt(process.argv[4])); i ...

Bootstrap is having trouble with aligning flex items and preventing them from wrapping to a new row

I am working on a code implementation that dynamically displays flex items, with each item taking up half of the display width. So, after two items, I need to start a new row. Is there a way to add a line break after every two items displayed on the page ...

Exploring various templates with AngularJS Directives

Let's delve into a slightly complex topic, but I'll simplify it as much as possible for you. There is a directive in play: .directive('configuratorRows', function () { return { restrict: 'A', scope: { ...

Attempting to select an input field

I'm having trouble clicking on the Select Files button that is nested within a SPAN and Input Tag. Despite using Xpath, Id, and Name, I am unable to successfully click on the button. <span> Select <u>a</u> Files... </span> & ...

Is it better to use Asynchronous or Synchronous request with XMLHttpRequest in the Firefox Extension for handling multiple requests

As a newcomer to developing Firefox Add-Ons, my initial project involves calling an external API in two steps: Step 1) Retrieve data from the API. Step 2) Use the data retrieved in Step 1 to make another call to the same API for additional information. ...

Issue with flex-grow property not functioning as expected in Internet Explorer 11

Hello! I'm running into an issue with flexbox in Internet Explorer: http://jsfiddle.net/EvvBH/ I've noticed that the #two element has flex: auto, which is meant to make it expand to fill the container, even if there's limited content. Ho ...

Achieving automatic restarts with grunt-express when files are updated

I am working on implementing grunt-express and grunt-watch in my project. My goal is to have the server automatically reload whenever I make changes to my server file. Below is the setup I currently have: Gruntfile.js var path = require('path' ...

Exporting Data and Utilizing a Steady Data Table

I have incorporated the Fixed Data Grid into my latest project. https://facebook.github.io/fixed-data-table/example-sort.html My goal is to generate csv and pdf reports from the data displayed on the grid. Could you please advise me on how to export gri ...

Understanding the difference between Parameters in Javascript and Java

Currently, I am facing an issue where I am losing some information while sending an ajax request to a servlet. The specific parameter that I am losing data from is the "comment" parameter. Below are the last 4 lines of my ajax code: var params = "name=" + ...

Attempting to conceal image previews while incorporating pagination in Jquery

I'm working on implementing pagination at the bottom of a gallery page, allowing users to navigate between groups of thumbnail images. On this page, users can click on thumbnails on the left to view corresponding slideshows on the right. HTML <di ...

Passing data between Vue.js components effortlessly without relying on events

At the moment, I am utilizing Laravel Nova. It's worth noting that it operates using vue.js. I've created a personalized vue component which requires the ability to modify data inside another component. This specific component is located in the ...

Using AngularJs, eliminate parameters prior to hashing

I am facing an issue that may appear to be a duplicate, but I have already tried the following solutions without success: $location.search('lang', null) $location.url($location.path()); $location.$$search = {}; The Issue: Upon initial redirec ...

Guide on finding and replicating a particular string in HTML connected websites

Allow me to outline the issue at hand. By visiting the provided link, you will encounter a directory of html links containing stories by Aesop, each with its own moral. My goal is to extract and save only the strings that include "Moral of Aesop's Fab ...

Challenge with Transferring Data to be Displayed

My issue lies in calling a function with 3 inputs - 2 numbers and 1 string. The two numbers are being passed correctly, but I need the string to be printed within the element. I suspect the problem is related to passing the parameter to an HTML part of th ...

Working with time durations in Moment.js to manipulate datetime

How can I properly combine the variable secondsToMinutes with the startdate? The value of secondsToMinutes is "3:20" and the startDate is "2:00 PM". The desired endDate should be "2:03:20 PM". Despite my efforts, I keep encountering errors every time I at ...

The art of website creation: incorporating images into the background and using image tracing techniques

Trying to figure out where to find a solution for this. I had the idea of using a semi-transparent Photoshop design as a background and building on top of it, almost like tracing over it. This way, aligning things would be much easier. I remember Microsoft ...

What is the best way to target changing elements displayed by *ngIf?

Trying to access a dynamic element generated by ngIf in the code below has proven to be challenging. I attempted two different methods: Using ElementRef and querySelector Component template: `<div class="test" *ngIf="expr"> <a id="b ...

React Hook is failing to trigger an update

Learning React and JavaScript has been quite a challenge for me, especially when it comes to understanding React Hooks and the issue of them not updating sometimes. I have tried searching online but either end up with solutions for class-based components o ...