Grid and Dot Backgrounds

A simple grid and dots background to make your sections stand out.

Backgrounds

Installation

Install Dependencies

npm i svelte-motion clsx tailwind-merge

Add util file

src/lib/utils/cn.ts
import { type ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
        
export function cn(...inputs: ClassValue[]) {
    return twMerge(clsx(inputs));
}

Add Tailwind CSS plugin for backgrounds

tailwind.config.ts
import flattenColorPalette from 'tailwindcss/lib/util/flattenColorPalette';
import aspectRatio from '@tailwindcss/aspect-ratio';
import svgToDataUri from 'mini-svg-data-uri';

const config = {
	// ... other properties
	plugins: [
		// ...other plugins
		aspectRatio,
		addVariablesForColors,
		function ({ matchUtilities, theme }: any) {
			matchUtilities(
				{
					'bg-grid': (value: any) => ({
						backgroundImage: `url("${svgToDataUri(
							`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="32" height="32" fill="none" stroke="${value}"><path d="M0 .5H31.5V32"/></svg>`
						)}")`
					}),
					'bg-grid-small': (value: any) => ({
						backgroundImage: `url("${svgToDataUri(
							`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="8" height="8" fill="none" stroke="${value}"><path d="M0 .5H31.5V32"/></svg>`
						)}")`
					}),
					'bg-dot': (value: any) => ({
						backgroundImage: `url("${svgToDataUri(
							`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="16" height="16" fill="none"><circle fill="${value}" id="pattern-circle" cx="10" cy="10" r="1.6257413380501518"></circle></svg>`
						)}")`
					})
				},
				{ values: flattenColorPalette(theme('backgroundColor')), type: 'color' }
			);
		}
	]
};

// This plugin adds each Tailwind color as a global CSS variable, e.g. var(--gray-200).
function addVariablesForColors({ addBase, theme }: any) {
	let allColors = flattenColorPalette(theme('colors'));
	let newVars = Object.fromEntries(
		Object.entries(allColors).map(([key, val]) => [`--${key}`, val])
	);

	addBase({
		':root': newVars
	});
}

Copy the source code

src/lib/components/ui/GridAndDotBackground/GridAndDotBackgrounds.svelte
<script lang="ts">
	export let showFade: boolean = true;
</script>

<div
	class="relative flex h-96 w-[50vw] items-center justify-center bg-white bg-grid-black/[0.2] dark:bg-black dark:bg-grid-white/[0.2]"
>
	<!-- Radial gradient for the container to give a faded look -->
	{#if showFade}
		<div
			class="pointer-events-none absolute inset-0 flex items-center justify-center bg-white [mask-image:radial-gradient(ellipse_at_center,transparent_20%,black)] dark:bg-black"
		></div>
	{/if}
	<slot />
</div>
src/lib/components/ui/GridAndDotBackground/GridAndDotBackgroundsSmallGrid.svelte
<script lang="ts">
	export let showFade: boolean = true;
</script>

<div
	class="relative flex h-96 w-[50vw] items-center justify-center bg-white p-8 bg-grid-small-black/[0.2] dark:bg-black dark:bg-grid-small-white/[0.2]"
>
	<!-- Radial gradient for the container to give a faded look -->
	{#if showFade}
		<div
			class="pointer-events-none absolute inset-0 flex items-center justify-center bg-white [mask-image:radial-gradient(ellipse_at_center,transparent_20%,black)] dark:bg-black"
		></div>
	{/if}
	<slot />
</div>
src/lib/components/ui/GridAndDotBackground/DotBackground.svelte
<script lang="ts">
	export let showFade: boolean = true;
</script>

<div
	class="relative flex h-96 w-[50vw] items-center justify-center bg-white bg-dot-black/[0.2] dark:bg-black dark:bg-dot-white/[0.2]"
>
	<!-- Radial gradient for the container to give a faded look -->
	{#if showFade}
		<div
			class="pointer-events-none absolute inset-0 flex items-center justify-center bg-white [mask-image:radial-gradient(ellipse_at_center,transparent_20%,black)] dark:bg-black"
		></div>
	{/if}
	<slot />
</div>
src/lib/components/ui/GridAndDotBackgrounds/index.ts
import GridAndDotBackgrounds from './GridAndDotBackgrounds.svelte';
import GridAndDotBackgroundsSmallGrid from './GridAndDotBackgroundsSmallGrid.svelte';
import DotBackground from './DotBackground.svelte';

export { GridAndDotBackgrounds, GridAndDotBackgroundsSmallGrid, DotBackground };

Examples

Grid Background

Backgrounds

Grid Small Background

Backgrounds

Dot Background

Backgrounds

Props

GridAndDotBackgrounds
Prop Type Description
showFade boolean Default: true. Apply a vignette fade to the background.