Angular Bootstrap Masonry

Angular Masonry - Bootstrap 4 & Material Design

Angular Bootstrap masonry is a grid layout based on columns. Unlike other grid layouts, it doesn’t have fixed height rows. Basically, Masonry layout optimizes the use of space inside the web page by reducing any unnecessary gaps. Without this type of layout, certain restrictions are required to maintain the structure of layout.


Basic layout

Display of half page intro.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Within style tags in your html file body { margin: 0; padding: 1rem; } // SCSS // Masonry layout vertical .masonry-with-columns { display: flex; flex-direction: column; flex-wrap: wrap; max-height: 1000px; div { flex: 1 0 auto; background: #00997B; color: white; margin: 0 1rem 1rem 0; text-align: center; font-weight: 900; font-size: 2rem; } @for $i from 1 through 36 { div:nth-child(#{$i}) { $h: (random(400) + 100) + px; height: $h; line-height: $h; } } } import {AfterViewInit, Component, ElementRef, Renderer2, ViewChild} from '@angular/core'; @Component({ selector: 'masonry', templateUrl: './masonry.component.html', styleUrls: ['./masonry.component.scss'] }) export class MasonryComponent implements AfterViewInit { @ViewChild('masonry', { static: true }) masonry: ElementRef; constructor(private renderer: Renderer2) { } ngAfterViewInit() { const numCols = 3; const colHeights = Array(numCols).fill(0); Array.from(this.masonry.nativeElement.children).forEach((child: any, i) => { const order = i % numCols; this.renderer.setStyle(child, 'order', order); colHeights[order] += parseFloat(child.clientHeight); }) this.renderer.setStyle(this.masonry.nativeElement, 'height', `${Math.max(...colHeights)}px`) } }

Horizontal option

Display of half page intro.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Within style tags in your html file body { margin: 0; padding: 1rem; } // SCSS // Masonry layout horizontal .masonry-with-columns-2 { display: flex; flex-wrap: wrap; div { height: 150px; line-height: 150px; background: #9B1B30; color: white; margin: 0 1rem 1rem 0; text-align: center; font-weight: 900; font-size: 2rem; flex: 1 0 auto; } @for $i from 1 through 36 { div:nth-child(#{$i}) { $h: (random(400) + 70) + px; width: $h; } } }

Flexbox option

Display of half page intro.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Within style tags in your html file body { margin: 0; padding: 1rem; } // SCSS // Masonry layout flex .masonry-with-flex { display: flex; flex-direction: column; flex-wrap: wrap; max-height: 1000px; div { width: auto; background: #975A58; color: white; margin: 0 1rem 1rem 0; text-align: center; font-weight: 900; font-size: 2rem; } @for $i from 1 through 36 { div:nth-child(#{$i}) { $h: (random(400) + 100) + px; height: $h; line-height: $h; } } }