Columnizer

Arguments

NameTypeDescription
$columnsnumber

Sets the number of the hypothetical columns.

$gutternumber (with unit)

Sets the value of the space between the columns.

$fillboolean

It is for orphans to fill the remaining gap at the end of the list. This argument is optional and should always be at the end. The default value is set to false.

Examples

Suppose you have a group of items like in the example below and you want to make them look lined up in columns (There are predefined style rules for items to show the differences more easily).

<div class="parent-element">
  <div class="item">01</div>
  <div class="item">02</div>
  <div class="item">03</div>
  <div class="item">04</div>
  <div class="item">05</div>
  <div class="item">06</div>
  <div class="item">07</div>
  <div class="item">08</div>
</div>

Now let’s get cracking. Just call the mixin in the .parent-element{} selector and pass a value for the number of columns.

.parent-element{
  @include columnizer(3);
}
//CSS Output
.parent-element {
  display: flex;
  flex-wrap: wrap;
}
.parent-element > * {
  flex: 0 0 calc(100% / 3);
  margin-bottom: 0;
}
.parent-element > *:not(:last-child) {
  margin-right: 0;
}
01
02
03
04
05
06
07
08

Let’s pass a value for $gutter argument to separate every item from each other.

.parent-element{
  @include columnizer(3, 20px);
}
//CSS Output
.parent-element {
  display: flex;
  flex-wrap: wrap;
}
.parent-element > * {
  flex-grow: 0;
  flex-shrink: 0;
  flex-basis: calc((100% - (3 - 1) * 20px) / 3);
  margin-bottom: 20px;
}
.parent-element > *:not(:last-child) {
  margin-right: 20px;
}
.parent-element > *:nth-child(3n) {
  margin-right: 0;
}
01
02
03
04
05
06
07
08

To make the orphans fill the gap let’s pass true value at the end.

.parent-element{
  @include columnizer(3, 20px, true);
}
//CSS Output
.parent-element {
  display: flex;
  flex-wrap: wrap;
}
.parent-element > * {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: calc((100% - (3 - 1) * 20px) / 3);
  margin-bottom: 20px;
}
.parent-element > *:not(:last-child) {
  margin-right: 20px;
}
.parent-element > *:nth-child(3n) {
  margin-right: 0;
}
01
02
03
04
05
06
07
08

You can use either $gutter or $fill arguments optionallt. But remember, the $fill argument should always be at the end.

.parent-element{
  @include columnizer(3, true);
}
//CSS Output
.parent-element {
  display: flex;
  flex-wrap: wrap;
}
.parent-element > * {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: calc(100% / 3);
  margin-bottom: 0;
}
.parent-element > *:not(:last-child) {
  margin-right: 0;
}
01
02
03
04
05
06
07
08

Now let’s try it with the breakpoint mixin to show you how these mixins work harmoniously together.

.parent-element{
  @include columnizer(2, 10px, true);
  @include breakpoint(min, large) {
    @include columnizer(4, 20px, false);
  }
}
//CSS Output
.parent-element {
  display: flex;
  flex-wrap: wrap;
}
.parent-element > * {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: calc((100% - (2 - 1) * 10px) / 2);
  margin-bottom: 10px;
}
.parent-element > *:not(:last-child) {
  margin-right: 10px;
}
.parent-element > *:nth-child(2n) {
  margin-right: 0;
}
@media (min-width: 992px) {
  .parent-element {
    display: flex;
    flex-wrap: wrap;
  }
  .parent-element > * {
    flex-grow: 0;
    flex-shrink: 0;
    flex-basis: calc((100% - (4 - 1) * 20px) / 4);
    margin-bottom: 20px;
  }
  .parent-element > *:not(:last-child) {
    margin-right: 20px;
  }
  .parent-element > *:nth-child(4n) {
    margin-right: 0;
  }
}
01
02
03
04
05
06
07