Repeating Square Dots Backgrounds in CSS
Repeating Square Dots Backgrounds in CSS êŽë š
I saw this reasonable ask for help the other day.
Pawel Grzybek (@pawelgrzybek) from Mastadon
Hey all CSS wizards. I need your help.
I would love this bg pattern to be squares instead of little circles (radial-gradient). Is that even possible using pure CSS?
I have seen that on the ATProto website, but they use SVG for it.
@kevinpowell @css @bramus @chriscoyier đ
Note that the example above has little circular dots, and what PaweĆ is asking about are little square dots. This is the example look from atproto.com:

Repeating a Small Area
My first thought is weâre obviously not drawing all these dots in one big graphic. Weâre drawing one, leaving empty space, and using our olâ pal background-repeat: repeat;
In order for the repeating to work, we need to define a smaller space via background-size. So letâs say itâsâŠ
html {
background-size: 100px 100px;
background-repeat: repeat;
}
Now weâve got a 100px square that will repeat over the entire background. We just need to fill that 100px square with a smaller square that repeats.

Drawing the Square Dot
Again my first thought was that we could make it look like weâre drawing a small square dot by actually letting a background-color show through and covering up everywhere else. So like a three-layer system:

html {
background:
linear-gradient(to bottom, transparent 10px, white 10px),
linear-gradient(to right, transparent 10px, white 10px),
black;
}
If we flatten out that visual, you can see the three shapes smoosh down into a small square, which we then repeat.

Thatâs whatâs going on here exactly:
Uh Oh â Transparency?
The problem with the above is that we need a solid color to be the âmaskâ that covers all the area except the mask. This means we canât have, for example, one big image behind the dots or a gradient or anything (without more exotic trickery). The problem is we donât have proper transparency.
What we want to be doing is drawing the dot with one âgradientâ, if we can, and leaving the rest of the area empty/transparent.
Enter Conic Gradients
I didnât think of this! Eric Meyer had the clever idea (@Meyerweb). The idea is that you can use conic-gradient to describe what we want in one go. âHard stopâ color stops are in use here, the classic trick to make a gradient just bands of solid color, not actually gradients. But in this case, three-quarters of the area is transparent, and the last bit is the dot color.
html {
background-image:
conic-gradient(
from 0deg at 5px 5px,
transparent 75%, black 75% 100%
);
}
This took me a sec to understand, but itâs essentially setting the center of the conic gradient at a specific spot, then forcing most of the way around to be transparent and leaving the last bit as the dot.

Then we use the same concept as before where we set a smaller background-size and let it naturally repeat, and weâve got square dots with a transparent background!