Slick Lazy Load Photo Grid Using Webpack 3

How to layout and lazy load images in a flexible grid similar to how facebook displays them in a post. Selected images open a lightbox for previewing within a carousel. Image alt text is converted into a caption below the image.


  • Lazy image loading
  • ES6 transpiler
  • JavaScript source maps
  • Sass CSS preprocessor
  • PostCSS Autoprefixer
  • CSSnano
  • Webpack 3
uiCookbook Photogrid
Source Code

Getting Started

If you don’t already have Node.js installed, then that needs to be the first order of business. Head on over to Node.js and get that taken care of.

Navigate to the project root in your CLI, such as Terminal, Cygwin or PowerShell.

Enter npm init to interactively create a package.json file. Accepting the default options is okay for now. The metadata in this file can be updated later as needed.

npm init

A package.json file should now exist in the root of the project. This file will be used later with the npm command when installing modules and running scripts.

Create an index.html file in the root of the project. Add an unordered list of images to the body with the .photogrid style class. For lazy image loading, instead of using a src attribute, put the image path in a data attribute named data-src. Also include the dist/style.css link in the document head and dist/app.js link before the closing body tag.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" media="all" href="dist/style.css">
    <ul class="photogrid">
            <img data-src="" alt="Sea breeze and splashing waves. A photo by @dankapeter on Unsplash"/>
            <img data-src="" alt="Above it All. A photo by @anthonyintraversato on Unsplash" />
            <img data-src="" alt="Found this beauty while being lost in the streets of CancĂșn, Mexico. A photo by @odiin on Unsplash" />
            <img data-src="" alt="Touring NYC. A photo by @freddymarschall on Unsplash" />
            <img data-src="" alt="Wind turbines, Greece. A photo by @jeisblack on Unsplash" />
    <script async src="dist/app.js"></script>
  • Using Emmet, which is built into VS Code, you can create the index.html content by entering an exclamation mark on the first line then select the tab key.

Lazy Image Loading

This is accomplished using David Walsh’s Simple Image Lazy Load and Fade method.

Create a folder named src. In that folder, create both a js folder and a sass folder.

Within the js folder, create an index.js app entry point file and a lazyimage.js module file.

Within the sass folder, create a style.scss file that will be used as the entry point for Sass processing. Add three Sass partials, _base.scss, _lazyimage.scss and _photogrid.scss. The leading underscore in the filename denotes that the file is a Sass partial and therefore will only be processed if imported.

  • photogrid
    • index.html
    • package.json
    • src
      • js
        • index.js
        • lazyimage.jsc
      • sass
        • style.scss
        • _base.scss
        • _lazyimage.scss
        • _photogrid.scss

In the lazyimage.js module, export this Lazyimage ES6 class.

export default class Lazyimage {
  constructor(options) {


  init() {

    []'img[data-src]'), function(img) {
      img.setAttribute('src', img.getAttribute('data-src'));
      img.onload = function() {

Import both the entry point Sass, style.scss and the lazyimage module into the index.js app entry point file.

import style from '../sass/style.scss'
import Lazyimage from './lazyimage'

new Lazyimage();

Add this Sass to the base partial for the unordered list and image element default style.

ul {
  padding: 0;

img {
  border-style: none;
  height: auto;
  max-width: 100%;

Add this Sass to the photogrid partial to apply the photogrid styling with flexbox to the list of images.

ul.photogrid {
  margin: 0.5vw 0.5vw 0.5vw -0.5vw;
  font-size: 0;
  flex-flow: row wrap;
  display: flex;

  li {
    flex: auto;
    width: 200px;
    margin: 0.5vw;

Add this Sass to the lazyimage partial to fade the image in when the image has loaded and the data-src attribute has been removed.

img {
  opacity: 1;
  transition: opacity 0.3s;

img[data-src] {
  opacity: 0;

Import the three partials into the style Sass file.

@import "base";
@import "photogrid";
@import "lazyimage";

The next page covers adding Webpack and the build configurations, installing dependencies, running the first build and verifying the application output.

Loading Disqus Comments ...
Loading Facebook Comments ...