[DEMO] griddery v3.0

https://dub.sh/griddery

by glenthemes

HTML

<!--✻✻✻✻✻✻  griddery by @glenthemes  ✻✻✻✻✻✻-->
<script src="//griddery.github.io/grid.js"></script>
<link href="//griddery.github.io/basics.css" rel="stylesheet">
<script>
document.addEventListener("DOMContentLoaded", () => {
	/*----- 🐱 CATS GRID 🐱 -----*/
	// for the column count, I used css variables, :root
	gridderyV3({
		items: "img[cat]",
		columns: "var(--Cat-Columns)"
	})
	
	/*----- 🐶 DOGS GRID 🐶 -----*/
	// for the column count, I directly assigned a number.
	// both work!
	gridderyV3({
		items: "img[dog]",
		columns: 2
	})
	
	// for demo use only
	// to showcase their names
	// to reflect that the left-to-right order is kept.
	// safely ignore this part 👍
	setTimeout(() => {
		document.querySelectorAll("[griddery-item] img[name]")?.forEach(img => {
			let name = img.getAttribute("name");
			if(name.trim() !== ""){
				let label = document.createElement("div");
				label.classList.add("label");
				label.textContent = name;
				img.after(label)
			}
		})
	},0)
})
</script>

<h3>Images in this demo are all from Unsplash.</h3>

<img src="//static.tumblr.com/snld4jp/CMPsi4o4i/malachi-burford-linakdr4k6i-unsplash.jpg" cat name="Alexander" alt="Cat">
<img src="//static.tumblr.com/snld4jp/2IAsi4nhk/benigno-hoyuela-g3mgoqytwd0-unsplash.jpg" cat name="Bumblebee" alt="Cat">
<img src="//static.tumblr.com/snld4jp/fJlsi4nm5/joao-alexandre-paulo-uyp-qfkg0nq-unsplash.jpg" cat name="Cheshire" alt="Cat">
<img src="//static.tumblr.com/snld4jp/sYusi4nnw/ramy-kabalan-k0oehfhx2pw-unsplash.jpg" cat name="Derrick" alt="Cat">
<img src="//static.tumblr.com/snld4jp/Bc8si4nqa/larissa-dupont-tfklqm0e5ri-unsplash.jpg" cat name="Emilia" alt="Cat">
<img src="//static.tumblr.com/snld4jp/qCcsi4nse/michael-chiara-cme2nemxtlo-unsplash.jpg" cat name="Francis" alt="Cat">
<img src="//static.tumblr.com/snld4jp/Gq8si4ntr/sven-mieke-7r7uopb86jg-unsplash.jpg" cat name="Goose" alt="Cat">

<h3>
(some text as a separator. this won't be...

CSS

:root {
	--Cat-Columns:3;
}

/*----- 🐱 CATS GRID 🐱 -----*/
/* cats grid column spacing */
[griddery-id="img[cat]"]
[griddery-col] + [griddery-col]{
    padding-left:15px;
}

/* cats grid "row" spacing */
[griddery-id="img[cat]"]
[griddery-item] + [griddery-item]{
    margin-top:15px;
}

/*----- 🐶 DOGS GRID 🐶 -----*/
/* dogs grid column spacing */
[griddery-id="img[dog]"]
[griddery-col] + [griddery-col]{
    padding-left:15px;
}

/* dogs grid "row" spacing */
[griddery-id="img[dog]"]
[griddery-item] + [griddery-item]{
    margin-top:15px;
}

/* everything below here is not necessary, it's just for the purpose of the demo. however, you may consider following the rules I wrote for imgs so that they don't appear super huge. */

body {
	margin:15px;
	font-family:sans-serif;
	font-size:0.75rem;
}

img {
	vertical-align:middle;
	max-width:100%;
	width:100%;
}

[griddery-item]{
	position:relative;
	border-radius:4px;
	box-shadow:4px 4px 14px rgba(0,0,0,15%);
	overflow:hidden;
	z-index:0;
}

[griddery-item] .label {
	position:absolute;
	top:0;left:0;
	padding:4px 6px;
	border-radius:0 0 4px 0;
	background:rgba(0,0,0,69%);
	color:white;
	font-family:monospace;
	z-index:1;
}

JavaScript

/*-----------------------------------------

    griddery by @glenthemes [tumblr]
    https://git.new/griddery-v3
    
-----------------------------------------*/