avatarEmily Y Leung

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4622

Abstract

ute">left</span>: <span class="hljs-number">0</span>; <span class="hljs-attribute">display</span>: none; <span class="hljs-attribute">z-index</span>: <span class="hljs-number">2</span>; }</pre></div><div id="7e8d"><pre> <span class="hljs-selector-class">.overlay</span><span class="hljs-selector-class">.open</span> { <span class="hljs-attribute">display</span>: grid; }</pre></div><div id="7d09"><pre> <span class="hljs-selector-class">.overlay-inner</span>{ <span class="hljs-attribute">background</span>: white; <span class="hljs-attribute">width</span>: <span class="hljs-number">700px</span>; <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span>; }</pre></div><div id="8999"><pre> <span class="hljs-selector-class">.overlay</span> <span class="hljs-selector-tag">img</span> { <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>; } </style></pre></div><h1 id="6ac6">1 — Generate Image Tiles</h1><p id="5965">Using JavaScript, we can randomly generate our image tiles.</p><p id="7f37">Inside our script tags, we need to select our <code>gallery</code> and <code>overlay</code> objects that were defined in our html.</p><div id="b78b"><pre><span class="hljs-tag"><<span class="hljs-name">script</span>></span><span class="language-javascript"> <span class="hljs-keyword">const</span> gallery = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">'.gallery'</span>); <span class="hljs-keyword">const</span> overlay = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">querySelector</span>(<span class="hljs-string">'.overlay'</span>); </span><span class="hljs-tag"></<span class="hljs-name">script</span>></span></pre></div><p id="5e58">Now we’ll also need to select the image and button from the <code>overlay</code> objects.</p><div id="2e47"><pre><span class="hljs-keyword">const</span> overlayImage = overlay.<span class="hljs-built_in">querySelector</span>(<span class="hljs-string">'img'</span>); <span class="hljs-keyword">const</span> overlayButton = overlay.<span class="hljs-built_in">querySelector</span>(<span class="hljs-string">'.close'</span>);</pre></div><p id="0ed2">The grid itself is made up of <code>100px</code> grid cells that <code>auto-fill</code> as the window is resized.</p><figure id="bfbc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jxG2LsxbFw9NPx77nvAgyQ.png"><figcaption></figcaption></figure><p id="5ab7">The way in which those images will come through is based on its given <code>width</code> and <code>height</code> values assigned in its class. Below is an example of an image that spans horizontally 2 columns and vertically 3 rows:</p><div id="4f9b"><pre><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item h2 v3"</span>></span> <span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"images/4.jpg"</span>></span> <span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item__overlay"</span>></span> <span class="hljs-tag"><<span class="hljs-name">button</span>></span>View -> <span class="hljs-tag"></<span class="hljs-name">button</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></pre></div><p id="5794">Those classes are then referenced via custom CSS to be sized for spanning.</p><p id="e5d4">In order to assign those values, we first need a function that generates the grid items as HTML.</p><p id="dc7c">Using a destructuring function, we can return a <code>div</code> with whatever inputs we provide.</p><div id="e37e"><pre><span class="hljs-keyword">function</span> generateHTML([h, v]) { <span class="hljs-keyword">return</span> ` <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item h{h} v{v}"</span>></span> </span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"images/1.jpg"</span>></span> </span><span class="language-xml"><span class

Options

="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item__overlay"</span>></span> </span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">button</span>></span>View -><span class="hljs-tag"></<span class="hljs-name">button</span>></span></span><span class="language-xml"> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span><span class="language-xml"> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span><span class="language-xml"> ; }</span></pre></div><p id="0dbc">Additionally, since we are referencing images from a folder, the limited amount would benefit from randomising the selection order.</p><p id="6fbb">We determine the order through a random value generator that takes in the highest random number it should return:</p><div id="c016"><pre><span class="hljs-keyword">function</span> randomNumber(<span class="hljs-keyword">limit</span>) { <span class="hljs-keyword">return</span> Math.floor(Math.random() * <span class="hljs-keyword">limit</span>) + <span class="hljs-number">1</span>; }</pre></div><p id="8149">Then amending the <code>generateHTML</code> function to include the random number for sourcing the image.</p><div id="6312"><pre><span class="hljs-keyword">function</span> generateHTML([h, v]) { <span class="hljs-keyword">return</span> <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item h{h} v{v}"</span>></span> </span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"images/${randomNumber(12)}.jpg"</span>></span> </span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"item__overlay"</span>></span> </span><span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">button</span>></span>View -><span class="hljs-tag"></<span class="hljs-name">button</span>></span></span><span class="language-xml"> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span><span class="language-xml"> <span class="hljs-tag"></<span class="hljs-name">div</span>></span></span><span class="language-xml"> `; }</span></pre></div><p id="9228">To test the image generation, we can create our own dataset using the <code>randomNumber</code> function we defined earlier.</p><p id="b69e">Using <code>Array.from()</code> we can provide a length for how many arrays as well as what those arrays would return.</p><div id="0c10"><pre>const digits = <span class="hljs-built_in">Array</span>.<span class="hljs-keyword">from</span>({ length: <span class="hljs-number">50</span> }, <span class="hljs-function"><span class="hljs-params">()</span> =></span> [randomNumber(<span class="hljs-number">4</span>), randomNumber(<span class="hljs-number">4</span>)]);</pre></div><p id="2655">Using <code>console.log()</code> we can see what is generated — an array of arrays.</p><figure id="b3db"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zXTI7lyz_OsyV6HqYEQ12Q.png"><figcaption></figcaption></figure><p id="bedf">For each of the arrays, let’s generate a grid item.</p><div id="aabe"><pre>const html = digits.<span class="hljs-built_in">map</span>(generateHTML).join(<span class="hljs-string">''</span>); <span class="hljs-built_in">console</span>.<span class="hljs-built_in">log</span>(html);</pre></div><p id="96e2">Each time you refresh the page, the height and width value randomises.</p><figure id="9696"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*R-tFNDbq3kWPbEQVvYDh1Q.png"><figcaption></figcaption></figure><p id="439c">To see our images, we grab the <code>gallery</code> which we’ve query selected earlier and assign its innerHTML as the html string we created:</p><div id="19a6"><pre><span class="hljs-attr">gallery.innerHTML</span> = html<span class="hljs-comment">;</span></pre></div><p id="9058">Now we have all our images appearing in one column with a button below.</p><figure id="b4f6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*zO0cMED13P3eE-JmgbAcQw.png"><figcaption></figcaption></figure><p id="924c">Part 2 looks into applying CSS onto the generated images.</p></article></body>

CSS Grid (Part 19A)— Image Gallery

Project case study — Responsive Dense Image Gallery

Live dev notes taken throughout the CSS Grid course by Wesbos — cssgrid.io

Today we explore the implementation of grid-auto-flow: dense in the creation of an image gallery.

This image gallery design isn’t something you would normally see on sites that require images to be laid out, but a creative idea that pushes the boundary of what’s possible.

This project will also require a bit of JavaScript.

The base elements we need are:

Overlay

When we click into any of the tiles, a transparent overlay (transluscent black covers the site) with the image sized at 100% sitting in front.

<div class="overlay">
    <div class="overlay-inner">
        <button class="close">× Close</button>
        <img>
    </div>
</div>

Gallery

Where we place our image tiles. Instead of manually referencing these images, we’ll use JavaScript to generate them inside of the empty section.

<section class="gallery">
</section>

Image files

A separate folder stores 500 x 500 pixel images which we’ll pull from.

CSS

The base CSS file which has been used throughout the CSS Grid fundamentals is not used in this project. We will start from scratch with our own.

<style>
    * {
        box-sizing: border-box;
    }
    body {
        padding: 50px;
        font-family: sans-serif;
    }
    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
        margin: 0 0 5px 0;
    }
    p {
        margin: 0 0 20px 0;
    }
    .close {
        background: none;
        color: white;
        border: 0;
    }
    .gallery {}
    .overlay {
        position: fixed;
        background: rgba(0, 0, 0, 0.7);
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        display: none;
        z-index: 2;
    }
    .overlay.open {
        display: grid;
    }
    .overlay-inner{
        background: white;
        width: 700px;
        padding: 20px;
    }
    .overlay img {
        width: 100%;
    }
</style>

1 — Generate Image Tiles

Using JavaScript, we can randomly generate our image tiles.

Inside our script tags, we need to select our gallery and overlay objects that were defined in our html.

<script>
    const gallery = document.querySelector('.gallery');
    const overlay = document.querySelector('.overlay');
</script>

Now we’ll also need to select the image and button from the overlay objects.

const overlayImage = overlay.querySelector('img');
const overlayButton = overlay.querySelector('.close');

The grid itself is made up of 100px grid cells that auto-fill as the window is resized.

The way in which those images will come through is based on its given width and height values assigned in its class. Below is an example of an image that spans horizontally 2 columns and vertically 3 rows:

<div class="item h2 v3">
    <img src="images/4.jpg">
    <div class="item__overlay">
        <button>View -> </button>
    </div>
</div>

Those classes are then referenced via custom CSS to be sized for spanning.

In order to assign those values, we first need a function that generates the grid items as HTML.

Using a destructuring function, we can return a div with whatever inputs we provide.

function generateHTML([h, v]) {
   return `
       <div class="item h${h} v${v}">
            <img src="images/1.jpg">
            <div class="item__overlay">
                <button>View -></button>
            </div>
        </div>
    `;
}

Additionally, since we are referencing images from a folder, the limited amount would benefit from randomising the selection order.

We determine the order through a random value generator that takes in the highest random number it should return:

function randomNumber(limit) {
    return Math.floor(Math.random() * limit) + 1;
}

Then amending the generateHTML function to include the random number for sourcing the image.

function generateHTML([h, v]) {
   return `
       <div class="item h${h} v${v}">
            <img src="images/${randomNumber(12)}.jpg">
            <div class="item__overlay">
                <button>View -></button>
            </div>
        </div>
    `;
}

To test the image generation, we can create our own dataset using the randomNumber function we defined earlier.

Using Array.from() we can provide a length for how many arrays as well as what those arrays would return.

const digits = Array.from({ length: 50 }, () => [randomNumber(4), randomNumber(4)]);

Using console.log() we can see what is generated — an array of arrays.

For each of the arrays, let’s generate a grid item.

const html = digits.map(generateHTML).join('');
console.log(html);

Each time you refresh the page, the height and width value randomises.

To see our images, we grab the gallery which we’ve query selected earlier and assign its innerHTML as the html string we created:

gallery.innerHTML = html;

Now we have all our images appearing in one column with a button below.

Part 2 looks into applying CSS onto the generated images.

Griddyup
Css Grid
CSS
Web
Web Development
Recommended from ReadMedium