Gallery Grid With CSS Grid

CSS Grid Layout is great. My site layout is fairly simple, so had no need for lots of grid—I only used it for lining up comments and avatars on old blog posts.
Working on my Notes section, I realised I’d sometimes be adding multiple images (dog photos)…


This content originally appeared on Laura Kalbag’s Blog Posts and was authored by Laura Kalbag

CSS Grid Layout is great. My site layout is fairly simple, so had no need for lots of grid—I only used it for lining up comments and avatars on old blog posts.

Working on my Notes section, I realised I’d sometimes be adding multiple images (dog photos) in one note. The default style for my images is big and full-width, but two or three full-width images in rows would likely be overwhelming. It’d be much tidier if all the images auto-filled across the row. Regardless of how many images there are in the notes.

My HTML for these notes looks something like this:

<figure class="note-image">
	<img src="2018/06/14/21/osky1.jpg" alt="Oskar with his head on a cushion and begging eyes."/>
	<img src="2018/06/14/21/osky2.jpg" alt="Oskar with his head on a cushion and closed eyes."/>
	<img src="2018/06/14/21/osky3.jpg" alt="Oskar with his head on a cushion and begging eyes open again."/>
    <figcaption><p>How could you say no to this face?</p></figcaption>
</figure>

Sometimes there is one image, sometimes two or three. I concluded there would rarely be more than three, though I’ll adjust the CSS accordingly if that is ever the case. Sometimes there is a caption, sometimes there isn’t.

The CSS is remarkably minimal:

figure {
    display: grid;
    grid-template-areas:
        "image1 image2 image3"
        "figcaption figcaption figcaption";
}

figcaption {
    grid-area: figcaption;
}

The images auto-fill across the first row, filling all available space. If there’s no image2 or image3, image1 fills 100% of the available width.

The figcaption always fills the whole width, regardless of whether there is one, two, or three images above it.

Four sets of images showing single, double, and triple image layouts.

The results. Note the two side-by-side portrait images do not fill the width of the viewport as the max-height of the images is set to 100vh.

Isn’t that fabulous?

As always, everything I’m learning about CSS Grid Layout is from work by Rachel Andrew and Jen Simmons. Resources I used to inform my decisions/cobble this together were from:

Read the original post, 'Gallery Grid With CSS Grid'.


This content originally appeared on Laura Kalbag’s Blog Posts and was authored by Laura Kalbag


Print Share Comment Cite Upload Translate Updates
APA

Laura Kalbag | Sciencx (2018-07-02T18:25:46+00:00) Gallery Grid With CSS Grid. Retrieved from https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/

MLA
" » Gallery Grid With CSS Grid." Laura Kalbag | Sciencx - Monday July 2, 2018, https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/
HARVARD
Laura Kalbag | Sciencx Monday July 2, 2018 » Gallery Grid With CSS Grid., viewed ,<https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/>
VANCOUVER
Laura Kalbag | Sciencx - » Gallery Grid With CSS Grid. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/
CHICAGO
" » Gallery Grid With CSS Grid." Laura Kalbag | Sciencx - Accessed . https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/
IEEE
" » Gallery Grid With CSS Grid." Laura Kalbag | Sciencx [Online]. Available: https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/. [Accessed: ]
rf:citation
» Gallery Grid With CSS Grid | Laura Kalbag | Sciencx | https://www.scien.cx/2018/07/02/gallery-grid-with-css-grid/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.