20th Jul '22
/
2 comments

How to display a Query Loop in 3 columns in Bricks

How to display a Query Loop in 3 columns in Bricks

This tutorial provides the builder settings and CSS codes that can be pasted in order to create a 3-columns query loop container inside Bricks Builder.

Table of Contents

The structure

As you can notice, we run our query loop on a Div element which is wrapped by a parent container where we’ll add most of our CSS when needed. The elements inside the Div aren’t relevant to the layout and were added for a demonstration purpose.

Flexbox

As of version 1.5 of Bricks, Flexbox is the only method that is supported natively and doesn’t require any additional CSS.

Preview

Flexbox works great until you have to manage gaps between each item. We’ll have to create a CSS calc() function that will calculate the correct width (called flex-basis in flexbox) of our Divs minus the gaps distributed between them. In our example, we’ll use a 2rem gap but feel free to change this value with your own one.

The container settings:

  • we set flex-wrap to wrap
  • we changed the flex-direction to row
  • we added both column and row gaps

The div settings:

  • We set both flex-grow and flex-shrink to 1
  • We set the flex-basis as follows:
    • desktop: calc(33.333% - calc(calc(2rem * 2)/3))
    • tablet: calc(50% - calc(calc(2rem * 1)/2))
    • mobile: 100%

CSS Grid – 1st method (requires additional CSS)

The CSS grid is probably the method that makes more sense when dealing with card grids, though it’s not natively supported by Bricks yet (as per 1.5 version) so it requires us to add some additional CSS.

Preview

The Container CSS:

root {
  display: grid;
  grid-gap: 2rem;
}
@media screen and (min-width: 768px) {
   root{
      grid-template-columns: repeat(2, 1fr);
   }
}
@media screen and (min-width: 992px) {
   root{
      grid-template-columns: repeat(3, 1fr);
   }
}

CSS Grid – 2nd method (requires additional CSS)

This method is slightly different from the first one, as we are going to use CSS variables to define the gap between cards, the minimum width of each card, and the maximum number of columns we want to have on larger screens. The advantage of this method is that once you’ve set your CSS variables, you don’t have to use any media queries to define when the columns need to stack – the CSS formula will calculate it for you.

Preview

The Container CSS:

Just paste the following code inside the container’s CSS tab. If you want to change the gap, the maximum number of columns, or the minimum width of your cards, just change the value of the first 3 CSS variables:

root {
   /* Set the following variables as you need */
   --grid-layout-gap: 2rem;
   --grid-column-count: 3;
   --grid-item--min-width: 280px;
   /* End of variables - Don't modify the following values */

   display: grid;
   grid-gap: var(--grid-layout-gap);
}

@media screen and (min-width: 768px) {
   root {
      --gap-count: calc(var(--grid-column-count) - 1);
      --total-gap-width: calc(var(--gap-count) * var(--grid-layout-gap));
      --grid-item--max-width: calc((100% - var(--total-gap-width)) / var(--grid-column-count));
      grid-template-columns: repeat(auto-fill, minmax(max(var(--grid-item--min-width), var(--grid-item--max-width)), 1fr));
   }
}

Column-count (masonry style)

If you are looking for a “fake” masonry style, there is a pure CSS solution using the column-count property.

Preview

The container settings:

Make sure to set the container to display: block and add the following CSS code in the CSS field:

root {
   column-count: 1;
   gap: 2rem;
}
root > div {
   break-inside: avoid-column;
}
@media screen and (min-width: 768px) {
   root{
      column-count: 2;
   }
}
@media screen and (min-width: 992px) {
   root{
       column-count: 3;
   }
}

Conclusion

It has been repeatedly reported in the Facebook group that creating a 3 columns layout for query loop containers can sometimes be tricky for non-developers. We hope that this tutorial will clarify the different options you’ve got and give your actionable tips to apply modern techniques to your future project.

Get access to all 610 Bricks code tutorials with BricksLabs Pro

2 comments

  • Alfredo

    Hi! Works perfect but anyway post direction was in a row and not in a column like now?

  • Thanks for this awesome tutorial! I searched for the query loop on YouTube but none showed how to use it to create grid layout.

Leave your comment

 

Related Tutorials..

How to Show Future Events in Bricks

How to Show Future Events in Bricks

One of the common requirements in WordPress is being able to filter the events (or could be posts of any post type) to only those…
Categories:
Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

Filtering Bricks Posts Query Loop by Meta Box Group’s Radio Subfield Value

In the past we showed how a Bricks posts query loop can be filtered by Meta Box group's subfield of types text/number and Datepicker here.…
Categories:
Bricks Templates Query Loop

Bricks Templates Query Loop

How display Bricks Templates with saved screenshots in a query loop.
Categories:
Pro
Top-level Pages and Children Bricks Query Loop

Top-level Pages and Children Bricks Query Loop

This Pro tutorial for Bricks users can be considered Part 2 or a variation of the earlier Bricks Query Loop for Top-level Parent Pages with…
Categories:
Pro
CPT Posts Grouped by Custom Taxonomy Terms in Bricks

CPT Posts Grouped by Custom Taxonomy Terms in Bricks

The steps to set up a Service Category terms loop with an inner Service posts loop in Bricks without writing code.
Categories:
How to add SVG icon as list item bullets

How to add SVG icon as list item bullets

Add this sample CSS: Replace brxe-list with the class of your ul element. Here's how you can generate the value of background-image property: Copy your…
Categories:
Tags:
Pro
Current Single Post Reference From Inside a Bricks Query Loop

Current Single Post Reference From Inside a Bricks Query Loop

Use case: Get the single post’s custom field (like ACF Repeater or Meta Box Group) array row corresponding to the loop’s counter/index
Categories:
Pro
ACF Post Object Query Loop inside a Posts Query Loop in Bricks

ACF Post Object Query Loop inside a Posts Query Loop in Bricks

How to loop through a CPT and within each post, output a list of related posts.
Categories:
Pro
Post Titles and Post Content Tabs in Bricks

Post Titles and Post Content Tabs in Bricks

How to set up Nestable tabs with post titles as the tab menu, and post content and/or any other post-specific data as the tab content.
Categories: