Have you ever explored the power of grid-auto-flow: dense? When combined with randomly generated values (constrained within a set range), you can use grid-column and grid-row properties to create cool, grid-based art. Let’s dive in!
First, let’s create a base grid structure:
main { display: grid; grid-auto-flow: dense; grid-template-columns: repeat(auto-fill, minmax(var(--w, 4cqi), 1fr)); }
- display: grid: Establishes a CSS grid layout.
- grid-auto-flow: dense: Automatically fills gaps in the grid by repositioning items to minimize empty spaces.
- grid-template-columns: Defines a responsive column layout. Here, columns are created automatically to fill the available space (auto-fill), each with a minimum width of --w (defaulting to 4cqi) and a maximum width of 1fr.
At this point, --w is undefined, so the default value (4cqi) is used. Here’s what the initial grid looks like:
Next, we’ll fill the grid with a bunch of -nodes. As we render these with JavaScript, we add 2 variables per node:
- --gc: Number of columns to span.
- --gr: Number of rows to span.
Here’s how we generate random values in JavaScript:
const random = (min, max) => Math.random() * (max - min) + min const column = () => `--gc:${Math.floor(random(0, 4))};` const row = () => `--gr:${Math.floor(random(0, 3))};`
In CSS, we apply these custom properties:
b { background: oklch(var(--l) var(--c) var(--h) / var(--a)); grid-column: span var(--gc); grid-row: span var(--gr, 1); }
Let’s also add some random colors in oklch, using a small helper method:
const color = () => `--l:${ random(0, 100)}%;--c:${ random(0, 0.5)};--h:${ random(0, 60)};--a:${ random(0.2, 1)};`
Now we get:
Thank you, grid-auto-flow: dense!
To make things more dynamic, let’s add transformations like rotation and scaling:
b { rotate: var(--r); scale: var(--s); }
In JavaScript, we generate random transformation values:
const transform = () => `--r:${random(-2, 3)}deg; --s:${random(0.8, 1.2)};`
Let’s check it out:
Fancy! Now, to take it up a notch, let’s add some cool SVG filters.
In JavaScript, we add another small helper method to pick a random filter per -node:
const filter = () => `--url:url(#${ [ "pencilTexture", "pencilTexture2", "pencilTexture3", "pencilTexture4", ][Math.floor(random(0, 4))] });`
This gives us:
Now, by simply adjusting the --w property and the number of elements, we can generate vastly different artworks:
Or:
We can also adjust the start- and stop values in the color method:
Demo
Here’s a Codepen demo. I've added controls below the artwork, so you can easily tweak the properties:
The above is the detailed content of Art with CSS Grid and SVG filters. For more information, please follow other related articles on the PHP Chinese website!

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
