---
title: Astro Dot Pattern
description: A dot background that fills its container.
seo:
    title: "Astro Dot Pattern: examples and source"
sidebar:
    label: "Dot Pattern"
related:
    - /patterns
    - /patterns/hexagon-pattern
    - /themes
---

```astro
---
import { CardFrame, Card } from "@/components/ui/card"
import { DotPattern } from "@/components/patterns/dot-pattern"
---

<div class="bg-background relative flex h-72 w-full items-center justify-center overflow-hidden rounded-lg border">
  <DotPattern />
  <p class="relative z-10 text-sm font-medium">Dot pattern</p>
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/dot-pattern
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/patterns/dot-pattern/dot-pattern.astro

```astro
---
import type { HTMLAttributes } from "astro/types"
import type { DotPatternProps } from "./types.ts"

import { cn } from "cn"
import { mergeStyles } from "@/lib/merge-styles.ts"

/**
 * Dot background.
 *
 * The pattern fills its nearest positioned parent.
 * Each dot is one tile of a radial-gradient background.
 */
type Props = HTMLAttributes<"div"> & DotPatternProps

const {
  dotSize = 1,
  dotSpace = "1.5rem",
  dotColor = "color-mix(in srgb, var(--foreground) 16%, transparent)",
  dotBg = "transparent",
  class: className,
  style: styleProp,
  ...props
} = Astro.props

const style = mergeStyles(
  {
    "--dot-color": dotColor,
    "background-color": dotBg,
    "background-image": `radial-gradient(circle, var(--dot-color) ${dotSize}px, transparent ${dotSize * 1.2}px)`, // 1.2 multiple adds a small amount of blur around the dots
    "background-size": `${dotSpace} ${dotSpace}`,
    "background-position": "center",
  },
  styleProp,
)
---

<div
  aria-hidden="true"
  data-slot="dot-pattern"
  class={cn("pointer-events-none absolute inset-0 size-full", className)}
  style={style}
  {...props}
></div>
```

#### src/components/patterns/dot-pattern/index.ts

```ts
export { default as DotPattern } from './dot-pattern.astro'
export type { DotPatternProps } from './types.ts'
```

#### src/components/patterns/dot-pattern/types.ts

```ts
export type DotPatternProps = {
    /**
     * Radius of each dot, as a CSS size.
     * @default 1
     */
    dotSize?: number
    /**
     * Distance from one dot to the next, as a CSS size.
     * The tile is square, so this sets both axes.
     * @default "1.5rem"
     */
    dotSpace?: string
    /**
     * Valid CSS color string. Use an opaque color for better contrast.
     * @default `color-mix(in srgb, var(--foreground) 16%, transparent)`
     */
    dotColor?: string
    /**
     * Valid CSS color string.
     * @default "transparent"
     */
    dotBg?: string
}
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

Place the pattern inside a positioned parent. The element uses `absolute inset-0`
and fills that parent.

```ts
import { DotPattern } from "@/components/patterns/dot-pattern"
```

```astro
<div class="relative h-72 overflow-hidden">
  <DotPattern />
</div>
```

## Examples

### Larger dots

`dotSize` sets the dot radius in pixels. `dotSpace` sets the distance from one
dot to the next.

```astro
---
import { DotPattern } from "@/components/patterns/dot-pattern"
---

<div class="bg-background relative flex h-72 w-full items-center justify-center overflow-hidden rounded-lg border">
  <DotPattern dotSize={2} dotSpace="2.5rem" />
  <p class="relative z-10 text-sm font-medium">Larger dots</p>
</div>
```

### Custom colors

`dotColor` and `dotBg` accept any CSS color. The default dot color mixes
`muted-foreground` to 18 percent opacity.

```astro
---
import { DotPattern } from "@/components/patterns/dot-pattern"
---

<div class="bg-background relative flex h-72 w-full items-center justify-center overflow-hidden rounded-lg border">
  <DotPattern
    dotBg="var(--color-muted)"
    dotColor="color-mix(in srgb, var(--color-primary) 55%, transparent)"
    dotSize={1.5}
    dotSpace="2rem"
  />
  <p class="relative z-10 text-sm font-medium">Custom colors</p>
</div>
```

### Card background

Put the pattern inside a `CardFrame` + `Card` to have a subtle raised effect.

```astro
---
import { CardFrame, Card } from "@/components/ui/card"
import { DotPattern } from "@/components/patterns/dot-pattern"
---

<CardFrame class="h-72 w-full">
  <Card class="items-center justify-center grow">
    <DotPattern />
    <p class="relative z-10 text-sm font-medium">Dot pattern card</p>
  </Card>
</CardFrame>
```

## API Reference

### DotPatternProps

<AutoTypeTable
    path="../../packages/ui/src/components/patterns/dot-pattern/types.ts"
    name="DotPatternProps"
/>

See the [registry source](/ui/r/dot-pattern.json) for the component source.
