Sierpinski Triangle in CSS
source link: https://yuanchuan.dev/single-div-sierpinski-triangle
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Today I found another way to draw the Sierpinski triangle using pure CSS.There are no recursive divs corresponding to the fractal structure, so it's simpler and performs better.
The method is to use multiple background gradients to draw triangles that are controlled by background-size
.
Here is how
Actually, the whole code is pretty simple, but I'll try to explain it step by step.
Step 1
Let's add a linear-gradient
tilted with a degree to make it a triangle.
div { width: 200px; height: 200px; --lg: linear-gradient(135deg, #000 50%, transparent 0); background: var(--lg) 0 0 / 100% 100%; background-color: #f5f5f5; }
Step 2
Add another one with background-size
of 50% 50%
.
div { width: 200px; height: 200px; --lg: linear-gradient(135deg, #000 50%, transparent 0); background: var(--lg) 0 0 / 100% 100%, var(--lg) 0 0 / 50% 50%; background-color: #f5f5f5; }
The previous background has a higher z-index value than the latter one when there are multiple backgrounds. And that's what we wanted: using smaller triangles to fill the holes.
Step 3
Then add enough levels of gradients. The background-size
is always half the size of the previous one.
div { width: 200px; height: 200px; --lg: linear-gradient(135deg, #000 50%, transparent 0); background: var(--lg) 0 0 / 100% 100%, var(--lg) 0 0 / 50% 50%, var(--lg) 0 0 / 25% 25%, var(--lg) 0 0 / 12.5% 12.5%, var(--lg) 0 0 / 6.25% 6.25%; background-color: #f5f5f5; }
Step 4
Swap background and gradients (triangles) colors.
div { width: 200px; height: 200px; --lg: linear-gradient(135deg, #f5f5f5 50%, transparent 0); background: /* ... */; background-color: #000; }
Final step
Remove the top left part with mask
and adjust angle with transform
. See also the live example on CodePen.
div { width: 200px; height: 200px; --lg: linear-gradient(135deg, #fff 50%, transparent 0); background: var(--lg) 0 0 / 100% 100%, var(--lg) 0 0 / 50% 50%, var(--lg) 0 0 / 25% 25%, var(--lg) 0 0 / 12.5% 12.5%, var(--lg) 0 0 / 6.25% 6.25%; background-color: #000; mask: linear-gradient(-45deg, #000 50%, transparent 0); transform: skew(27deg) translateX(-25%); }
How deep it can go
Chrome can render as many triangles as possible but Safari and Firefox don't work well as the triangle is getting smaller.
Hmmm
CSS is kind of different from any other tools. Sometimes you really need to think in CSS in order to develop an elegant solution.
The technique described in this article can be used to generate many interesting patterns too, together with radial-gradient
or conic-gradient
.
That would be another journey.
Recommend
-
36
-
49
-
47
“If you have a Mac with Go and Metal, you can try this example I've just created. It renders a basic triangle that follows your mouse at 60+ FPS. #golang https://t.co/R4r5WiTbZ9”
-
59
Graph algorithms provide the means to understand, model and predict complicated dynamics such as the flow of resources or information, the pathways through which contagions or network failures spread, and the influences o...
-
55
Did you learn that the OpenGL API is being deprecated in macOS 10.14? Are you interested in giving Apple's
-
17
Following on from our recent announcement that Raspberry Pi 4 is OpenGL ES 3.1 conformant, we have some more news to share on the graphics front. We...
-
14
·Learning gfx-hal This is Part 1 of a series of tutorials on graphics programming in Rust, using gfx-hal . If you’re reading this, I assume you want to learn gfx-hal . I will also cal...
-
15
Databases are commonly used as dumb storage bins for CRUD application data. However, this doesn’t do them justice: database systems such as
-
14
WebGPU is a modern graphics API for the web, in development by the major browser vendors. When compared to WebGL, WebGPU provides more direct control over the GPU to allow applications to leverage the hardware mo...
-
2
Inverted Triangle Architecture for CSS (ITCSS) Learn abou...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK