CSS Square Pattern Background
A square pattern background in pure CSS: rows of outlined squares drawn with two conic gradients. Set tile size, square size and line width live, then copy it.
.bg-squares {
--bg: #4b9c41;
--fg: #8fc188;
--size: 48px;
--square: 28px;
--line: 4px;
background-color: var(--bg);
background-image:
conic-gradient(at calc(var(--square) - var(--line) * 2) calc(var(--square) - var(--line) * 2), transparent 75%, var(--bg) 0),
conic-gradient(at var(--square) var(--square), transparent 75%, var(--fg) 0);
background-position:
calc((var(--size) - var(--square)) / 2 + var(--line)) calc((var(--size) - var(--square)) / 2 + var(--line)),
calc((var(--size) - var(--square)) / 2) calc((var(--size) - var(--square)) / 2);
background-size: var(--size) var(--size);
}<div class="
[--bg:#4b9c41]
[--fg:#8fc188]
[--size:48px]
[--square:28px]
[--line:4px]
[background-color:var(--bg)]
[background-image:conic-gradient(at_calc(var(--square)_-_var(--line)_*_2)_calc(var(--square)_-_var(--line)_*_2),_transparent_75%,_var(--bg)_0),_conic-gradient(at_var(--square)_var(--square),_transparent_75%,_var(--fg)_0)]
[background-position:calc((var(--size)_-_var(--square))_/_2_+_var(--line))_calc((var(--size)_-_var(--square))_/_2_+_var(--line)),_calc((var(--size)_-_var(--square))_/_2)_calc((var(--size)_-_var(--square))_/_2)]
[background-size:var(--size)_var(--size)]
"></div><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Squares — CSS background</title>
<style>
html, body { height: 100%; margin: 0; }
.bg-squares {
--bg: #4b9c41;
--fg: #8fc188;
--size: 48px;
--square: 28px;
--line: 4px;
background-color: var(--bg);
background-image:
conic-gradient(at calc(var(--square) - var(--line) * 2) calc(var(--square) - var(--line) * 2), transparent 75%, var(--bg) 0),
conic-gradient(at var(--square) var(--square), transparent 75%, var(--fg) 0);
background-position:
calc((var(--size) - var(--square)) / 2 + var(--line)) calc((var(--size) - var(--square)) / 2 + var(--line)),
calc((var(--size) - var(--square)) / 2) calc((var(--size) - var(--square)) / 2);
background-size: var(--size) var(--size);
}
</style>
</head>
<body class="bg-squares"></body>
</html>Save the pattern as an image, with your current colors and settings.
Squares in other colors
Click a color to try it in the preview above, then copy the code.
Browse by color: Red · Orange · Yellow · Green · Teal · Blue · Purple · Pink
How the squares pattern works
Each square comes from a conic-gradient() trick. A conic gradient sweeps around a point like a clock hand. With at var(--square) var(--square), transparent for the first 75% of the turn and --fg for the last quarter, only the quarter above and to the left of that point gets painted. That’s a filled square, --square pixels wide, in the corner of the tile.
The second layer does the same with the background color and a square smaller by twice --line. It’s shifted in by --line, so it covers the middle and leaves only an outline.
background-position moves both layers in by half the leftover space, (var(--size) - var(--square)) / 2, which centers each square in its tile. --size sets the spacing, --square the square, and --line the outline thickness. Set --line to half of --square for solid squares.
Use squares for tech and grid-based layouts, game interfaces and minimal geometric backgrounds. The Tailwind tab outputs the same layers for Tailwind v4.