Posts / April 3, 2019

Responsive Grid For Varying Height Elements In Bootstrap4

The quest for developers to handle varying height elements in a responsive grid is a lot like looking for the holy grail. Everyone is looking (including myself) for the perfect solution that’s also fast and lightweight.

Searching for responsive grid for varying height elements, the quest for the holy grail.

For a detailed explanation of the problem, I’d recommend reading Varying Column Heights in Bootstrap by Carol Skelly. She does a great job outlining the problem and providing some possible solutions. 👍

One of those solutions that Carol points out is Masonry, which is often people’s default solution. I’ve used it in the past although based on the search results for “masonry alternatives” it’s pretty easy to see it’s not the perfect solution. Although Masonry is incredibly robust it’s also really big including features and bloat that’s just not required in most projects.

So in the past, my goto solution to handle varying height elements has been Grid-A-Licous created by Andreas Pihlström. It’s a great library that’s been around since 2009. It’s fairly lightweight and has handled most of my needs on the different projects I’ve worked on.

In Rails, I would generally do something like:

<div id="cards" data-width="350" data-gutter="50">
  <% @cards.each do |card| %>
    <div class="card">
      <div class="card-body">
        <div class="card-title"><%= card.title %></div>
        <p class="card-text"><%= card.description %></p>
      </div>
    </div>
  <% end %>
</div>

And then in my Coffeescript I have something like:

gridCardsReady = ->
  if $('div[data-width]').length > 0
    $cards = $("div[data-width]")
    $cards.gridalicious({
      width: $cards.data('width'),
      selector: '.card',
      gutter: $cards.data('gutter'),
      animate: true,
      animationOptions: {
        speed: 80,
        duration: 100,
        queue: true
    }});
$(document).ready(gridCardsReady);
$(document).on('turbolinks:load', gridCardsReady);

And voila, Grid-A-Licous takes care of the rest! Changing the page size automatically adjusts and repositions the elements correctly into their columns. To have more or less columns, I simply change the data-widthattribute and it handles calculating the correct number of columns that fit. To add more padding between elements I simply increase the data-gutter attribute. As Pete the Cat says, “it’s all good!”

One of my minor complaints with Grid-A-Licous is with the padding, for example when you scale down to a single column, there’s still left and right padding. Ideally there would be no padding or margin so the elements in a single column are aligned with other page elements above and below. I’ve often gotten around this by not having margins or padding on that section with the cards, but it’s still a small annoyance.

Recently though I ran into another problem using Grid-A-Licous with lazysizes, a lazy loading image framework. Unfortunately the lazy loading of images combined with the repositioning of elements based on their height, caused a random ordering of elements. In the case where ordering of element wasn’t important then this wasn’t a big deal. But for the times when ordering was important, the solution kind of fell apart.

So this led me back on the holy grail quest for another solution. After some searching and digging I stumbled onto Colcade, a lightweight masonry layout. With Colcade you define your columns in HTML and CSS for various breakpoints, and it handles moving elements into the appropriate visible column. On the initial tests things worked well, cards loaded fast and responsiveness was handled as expected. 👌

In Colcade, you normally define your responsive columns in CSS:

/* Using floats */
.grid-col {
  float: left;
  width: 50%;
}

/* 2 columns by default, hide columns 2 & 3 */
.grid-col--2, .grid-col--3 { display: none }

/* 3 columns at medium size */
@media ( min-width: 768px ) {
  .grid-col { width: 33.333%; }
  .grid-col--2 { display: block; } /* show column 2 */
}

/* 4 columns at large size */
@media ( min-width: 1080px ) {
  .grid-col { width: 25%; }
  .grid-col--3 { display: block; } /* show column 3 */
}

Although since I’m using Bootstrap, I really didn’t want to define my own column widths when the rest of my site is using Bootstrap’s grid system. So this led me to come up with Bootstrap flavoured columns like:

<div id="cards" class="row">
  <div class="col col-sm-12 col-md-6 col-lg-4 d-block"></div>
  <div class="col col-md-6 col-lg-4 d-none d-sm-none d-md-block"></div>
  <div class="col col-lg-4 d-none d-sm-none d-md-none d-lg-block"></div>
  <% @cards.each do |card| %>
    <div class="card">
      <div class="card-body">
        <div class="card-title"><%= card.title %></div>
        <p class="card-text"><%= card.description %></p>
     </div>
    </div>
  <% end %>
</div>

This example would generate 3 columns on lg, 2 columns on md, and 1 column on sm. And then in my Coffeescript I now have:

gridCardsReady = ->
  if $('#cards').length > 0
    $('#cards').colcade({
      columns: '.col',
      items: '.card'
    });
$(document).ready(gridCardsReady);
$(document).on('turbolinks:load', gridCardsReady);

That’s it! I liked this approach a lot better than defining a bunch of my own floats and widths in CSS, instead just use Bootstrap’s col class. This makes it easy to your columns, for example you might want two columns on small width. And the good news is it also works with lazysizes, so elements with lazy loaded images still maintain their order!

Comments
Cialis E Risultati [url=http://apcialisle.com/#]Buy Cialis[/url] Zithromax Side Effects Sleep Buy Cialis Amoxicillin For Children Trouble Keeping Down
Cod Levaquin Cheapeast With Free Shipping [url=https://bbuycialisss.com/]Buy Cialis[/url] Cod Provera Ralovera Best Website In Internet Cialis Kamagra Anwendung
440931 buying cialis in durban http://cialisuqpor.com/ is 5 mg cialis enough http://cialisuqwc.com/
a7a621 usa generic brand viagra [url=http://viagrauyr.com/]viagra cheap[/url] prix viagra pharmacie algerie [url=http://viagrayqdd.com/]viagra canada[/url] viagra 10mg price [url=http://vigraweuy.com/]buy viagra[/url]
6f5621 compren cialis [url=https://cialishowmuch.com/]buy cialis[/url]
Одновременно с этим существует и другая сторона формирования и организации высказывания.
Одновременно с этим существует и другая сторона формирования и организации высказывания.
Одновременно с этим существует и другая сторона формирования и организации высказывания.
Одновременно с этим существует и другая сторона формирования и организации высказывания.
Одновременно с этим существует и другая сторона формирования и организации высказывания.
purchase ivermectin - where to buy stromectol stromectol tablets 3 mg
ivermectin 12mg - ivermectin iv stromectol order online
ivermectin 1 topical cream - can you buy stromectol over the counter ivermectina online
clopidogrel 75 mg price uk - plavix 75 mg tablet price clopidogrel 75 mg price
over the counter ed pills extends - urethral medication erectile dysfunction over the counter ed pills duane reade
http://00-tv.com/video/1.html xqahdzcvcxbnyrohus
montezumas ed pill - best place to buy ed pills online can you buy ed pills at walgree
viagra comprar sin receta - viagra 100 mg alternativas a viagra
where can i buy bactrim over the counter - babactr purchase bactrim ds
cheap buy cialis - profescial.com cialis without prescription canada
tadalafil 20mg buy - how fast does cialis super active works cheap buy cialis
duloxetine 200mg - cymbalta in mexico cymbalta price 60 mg
cost of synthroid 75 mcg - levothyroxine 0.088 mg tab synthroid buy
amoxil 250 - amoxil 54372 amoxicillin pill
tadalafil dosage https://nextadalafil.com/ tadalafil dosage tadalafil pills 20mg
tadalafil 40 mg daily https://superactivetadalafil.com/ buy tadalafil tadalafil pills 20mg
vardenafil pills - cheapest generic vardenafil online valif 20mg generic vardenafil
order tadalafil https://tadalafilgenc.com/ tadalafil 40 mg daily tadalafil max dose
cheapest vardenafil online uk - buy vardenafil vardenafil generic vardenafil without prescription
tadalafil 40 mg from india https://tadalafilgenc.com/ tadalafil 30 mg tadalafil online
tadalafil 40 mg daily https://nextadalafil.com/ generic tadalafil 40 mg tadalafil pills 20mg
ordering drugs from canada - rx pharmacy canadian pharmacy review
tadalafil daily use https://superactivetadalafil.com/ order tadalafil tadalafil generic
tadalafil 10mg online - bitcoin pharmacy online american pharmacy
levitra vardenafil - vardenafil generic levitra vardenafil medication
generic vardenafil reviews - buy vardenafil no prescription uk buy vardenafil australia
Хрустальный смотреть онлайн bit.ly/35mqcNm
ivermectin uk - ivermectin cost in usa oral ivermectin cost
can i buy viagra in canada over the counter - can i buy viagra over the counter in australia sildenafil for daily use
buy prednisone 40 mg - generic over the counter prednisone generic prednisone over the counter
http://clck.ru/Vhqmh dlvkdzcvcxbnpbqmdv
lyrica 25mg capsules - lyrica rx lyrica 25 mg price
generic zithromax 500mg - generic zithromax 250mg buy zithromax 100mg online
buy prednisolone 5mg uk - site prednisolone 5mg prices
cost of ivermectin lotion - ivermectin new zealand ivermectin cream
buy viagra online cheap canada - 80 mg sildenafil sildenafil 50 mg online
furosemide tablets buy uk - lasix medication over the counter doxycycline 100mg tabs
priligy 90mg - gnpriligy economy pharmacy
price of prednisone 5mg - prednisone pack prednisone in mexico
order modafinil - buy provigil modafinil 200 mg
http://bit.ly/2VrAsSN
buy accutane online uk - cheap generic accutane accutane 60 mg
amoxicillin without a doctor‘s prescription - amoxicillin for sale for humans buy amoxicillin 500
buy vardenafil without prescription - vardenafil tablets levitra vs viagra
how to buy stromectol - ivermectin humans ivermectin tablet 1mg
tadalafil from india 5mg - cialis 20mg uk us pharmacy no prescription
ivermectina - buy stromectol 12mg oral ivermectin cost
accutane 40mg capsule - accutane pharmacy roaccutane
lyrica price australia - lyrica 50 canadian online pharmacy cialis
amoxicillin for sale - generic amoxil buy amoxicilin 500 mg online canada
generic viagra without rx - price of sildenafil citrate us online viagra
tadalafil india buy online - cialis pill how much is cialis in mexico
ivermectin cost canada - buy ivermectin tablets stromectol human
Нечестивые (фильм, 2021) — актеры, трейлер, фото смотреть онлайн. https://bit.ly/113xUZ
prednisone 5mg price - where to buy prednisone online without a script prednisone online paypal
https://bit.ly/gidonline-ok-google
modafinil for sale - modafinil 100mg provigil 200 mg
https://www.pinterest.com/pin/580753314454151506
azithromycin 500 mg - buy zithromax buy zithromax online zithropls
lasix price - furosemide 40 mg lasix 80 mg tablet
clomiphene online - where can i buy clomiphene clomid cost
buy sildenafil generic online - cheapest viagra in united states buy sildenafil in mexico
generic cialis otc - generic cialis for sale in usa where can i buy cialis in canada
ivermectin new zealand - stromectol pharmacy ivermectina online
slot machine games - online casino real money usa online casino gambling
prednisone cost - where can i order prednisone 20mg prednisone over counter
viagra from india for sale - viagra prescription nz viagra 200 mg
cialis daily australia - Mail order cialis cialis for sale cheap
buy oral ivermectin - oral stromectol cost stromectol tablet 3 mg
erectile dysfunction medications - ed clinics where can i buy ed pills
ventolin cost australia - can i buy ventolin over the counter in usa Albuterol online
doxycycline antibiotic - cost of doxycycline prescription 100mg where can i get doxycycline over the counter
neurontin 300 600 mg - buy neurontin canadian pharmacy levothyroxine 112 coupon
buy viagra pills from canada - generic viagra 100mg viagra 30 tablet
canadian family pharmacy - discount generic cialis 20mg buy 40 mg tadalafil online
http://buysildenshop.com/ - Viagra
vardenafil cheap - viagra vs cialis vardenafil vardenafil 10mg
side effects of ivermectin - stromectol tablets 3 mg
deltasone 50 mg cost - price of deltasone how to get prednisone prescription
where to buy amoxicilin 500 mg - amoxicillin prices at walmart amoxicillin price at cvs
https://buytadalafshop.com/ - Cialis
cheap custom essay - write my essay cheap buying a research paper for college
https://buypropeciaon.com/ - Propecia
viagra Chicago - Canadian viagra and healthcare how to viagra tablet
buy 40 mg tadalafil online - Buy discount cialis online generic tadalafil 5mg cost
Если вы решили пробрести товары в рассрочку, то предложение вас действительно заинтересует вы можете получить карту Совесть в любом отделении банка. Вам не нуно будет ждать завтра!
ivermectin buy - ivermectin ireland stromectol sales
https://buystromectolon.com/ - stromectol over the counter
generic prednisone - prednisone 250 mg order prednisone 5mg
lasix for sale - furosemide brand name uk cost of lasix 20 mg
ventolin 90 - generic ventolin price ventolin over the counter
best canadian pills
cytotec tablet online - cytotec price in india cytotec pills price in south africa
Chief Watch. A great suspicion! Stay the friar too.
http://buylasixshop.com/ - furosemide dosage in heart failure
stromectol pill price - ivermectin tablets for humans ivermectin human
https://buyplaquenilcv.com/ - Plaquenil
ivermectin lotion for lice - ivermectin 3 mg tablet dosage stromectol tab 3mg
cheap sildenafil - sildenafil without doctor sildenafil prices
http://buyzithromaxinf.com/ - acheter azithromycin canada
Viagra With Dapoxetine
tadalafil 5mg - tadalapills.com generic tadalafil 20mg
help with essay writing - edit letter academic writing blog
https://buypriligyhop.com/ - Priligy
stromectol for humans - generic stromectol cost of stromectol medication
buy viagra online in us - Cialis by mail generic cialis cheap canada
Cialis Hypertension Arterielle
fildena 120mg - cenforcep vilitra 40
https://buyneurontine.com/ - neurontin 300 mg
ivermectin 400 mg - generic stromectol for humans ivermectin 6mg online
female viagra sale - vigrxps.com online viagra store
online pharmacy non prescription drugs - how much are cialis pills buy cialis lowest price
https://prednisonebuyon.com/ - define prednisolone
Buy Kamagra Soft Tablets
over the counter prednisone cream - prednipill.com order prednisone without prescription
canada cialis otc - 5mg cialis canadian pharmacy cost daily cialis
ivermectin lotion 0.5 - ivermectin coronavirus ivermectin buy australia
viagra pill cost canada - free viagra samples purchase viagra in australia
buy cheap viagra online - viagra online usa compare viagra prices
cialis 5mg daily use - otc cialis generic cialis capsules
ivermectin pill cost - ivermectin buy canada stromectol 12 mg