Docs
Grid Pattern Background

Grid Pattern Background

A customizable grid pattern background component with support for dots and lines, perfect for minimal and professional designs.

Dot Grid Pattern
Line Grid Pattern
Square Grid Pattern
Animated Crosshatch Pattern
Diamond Grid Pattern

Installation

Copy and paste the following code into your project.

components/ui/grid-pattern-background.tsx

import React from "react";
import { cn } from "@/lib/utils";
 
export interface GridPatternBackgroundProps
  extends React.HTMLAttributes<HTMLDivElement> {
  gridType?: "dots" | "lines" | "squares" | "crosshatch" | "diamonds";
  gridSize?: number;
  opacity?: number;
  color?: string;
  animate?: boolean;
  className?: string;
}
 
export function GridPatternBackground({
  gridType = "dots",
  gridSize = 20,
  opacity = 0.2,
  color = "currentColor",
  animate = false,
  className,
  ...props
}: GridPatternBackgroundProps) {
  const patternId = React.useId();
 
  return (
    <div
      className={cn("relative h-full w-full overflow-hidden", className)}
      {...props}
    >
      <svg className="absolute inset-0 h-full w-full">
        <defs>
          <pattern
            id={patternId}
            patternUnits="userSpaceOnUse"
            width={gridSize}
            height={gridSize}
            className={cn(
              animate && [
                gridType === "dots"
                  ? "animate-[dots-shift_20s_linear_infinite]"
                  : gridType === "lines"
                    ? "animate-[lines-shift_20s_linear_infinite]"
                    : gridType === "squares"
                      ? "animate-[squares-shift_20s_linear_infinite]"
                      : gridType === "crosshatch"
                        ? "animate-[crosshatch-shift_20s_linear_infinite]"
                        : "animate-[diamonds-shift_20s_linear_infinite]",
                "origin-center",
              ],
            )}
          >
            {gridType === "dots" ? (
              <circle
                cx={gridSize / 2}
                cy={gridSize / 2}
                r={1}
                fill={color}
                style={{ opacity }}
              />
            ) : gridType === "lines" ? (
              <path
                d={`M ${gridSize} 0 L 0 0 L 0 ${gridSize}`}
                fill="none"
                stroke={color}
                strokeWidth="0.5"
                style={{ opacity }}
              />
            ) : gridType === "squares" ? (
              <rect
                x={gridSize / 4}
                y={gridSize / 4}
                width={gridSize / 2}
                height={gridSize / 2}
                fill={color}
                style={{ opacity }}
              />
            ) : gridType === "crosshatch" ? (
              <>
                <path
                  d={`M ${gridSize} 0 L 0 ${gridSize}`}
                  fill="none"
                  stroke={color}
                  strokeWidth="0.5"
                  style={{ opacity }}
                />
                <path
                  d={`M 0 0 L ${gridSize} ${gridSize}`}
                  fill="none"
                  stroke={color}
                  strokeWidth="0.5"
                  style={{ opacity }}
                />
              </>
            ) : (
              // diamonds
              <path
                d={`M ${gridSize / 2} ${gridSize / 4} L ${(gridSize * 3) / 4} ${gridSize / 2} L ${gridSize / 2} ${(gridSize * 3) / 4} L ${gridSize / 4} ${gridSize / 2} Z`}
                fill={color}
                style={{ opacity }}
              />
            )}
          </pattern>
        </defs>
        <rect
          width="100%"
          height="100%"
          fill={`url(#${patternId})`}
          style={{
            transformBox: "fill-box",
            transformOrigin: "center",
          }}
        />
      </svg>
    </div>
  );
}

Add the following code to your globals.css file:

@theme {
  --animate-dots-shift: dots-shift 20s linear infinite;
  --animate-lines-shift: lines-shift 20s linear infinite;
  --animate-squares-shift: squares-shift 20s linear infinite;
  --animate-crosshatch-shift: crosshatch-shift 20s linear infinite;
  --animate-diamonds-shift: diamonds-shift 20s linear infinite;
 
  @keyframes dots-shift {
    0%,
    100% {
      transform: rotate(0deg);
    }
    50% {
      transform: rotate(180deg);
    }
  }
  @keyframes lines-shift {
    0% {
      transform: translateX(0) translateY(0);
    }
    50% {
      transform: translateX(50%) translateY(50%);
    }
    100% {
      transform: translateX(0) translateY(0);
    }
  }
  @keyframes squares-shift {
    0% {
      transform: translateX(0) translateY(0) rotate(0deg);
    }
    50% {
      transform: translateX(25%) translateY(25%) rotate(180deg);
    }
    100% {
      transform: translateX(0) translateY(0) rotate(360deg);
    }
  }
  @keyframes crosshatch-shift {
    0% {
      transform: rotate(0deg);
    }
    50% {
      transform: rotate(45deg);
    }
    100% {
      transform: rotate(0deg);
    }
  }
  @keyframes diamonds-shift {
    0% {
      transform: translateX(0) translateY(0) scale(1);
    }
    50% {
      transform: translateX(25%) translateY(25%) scale(0.8);
    }
    100% {
      transform: translateX(0) translateY(0) scale(1);
    }
  }
}

Update the import paths to match your project setup.

import GridPatternBackground from "@/components/ui/grid-pattern-background";

Usage

import GridPatternBackground from "@/components/ui/grid-pattern-background";
 
export default function Example() {
  return (
    <div className="relative h-[200px]">
      <GridPatternBackground
        gridType="dots" // choose from 'dots', 'lines', 'squares', 'crosshatch', 'diamonds'
        gridSize={24} // Adjust grid size
        opacity={0.3} // Adjust opacity
        color="#6d28d9" // Use a hex color
        animate={true} // Enable animation
      />
      <div className="absolute inset-0">Your content here</div>
    </div>
  );
}

Props

PropTypeDefaultDescription
gridType'dots' | 'lines' | 'squares' | 'crosshatch' | 'diamonds''dots'Type of grid pattern to display
gridSizenumber20Size of the grid cells in pixels
opacitynumber0.2Opacity of the grid pattern
colorstring'currentColor'Color of the grid pattern
animatebooleanfalseEnable subtle animation
classNamestring""Additional CSS classes